GithubHelp home page GithubHelp logo

eoghanobrien / php-simple-mail Goto Github PK

View Code? Open in Web Editor NEW
206.0 21.0 88.0 293 KB

Provides a simple, chainable wrapper for creating and sending emails using the PHP mail() function.

License: MIT License

PHP 100.00%
php emails attachments chainable-methods

php-simple-mail's Introduction

README

Build Status Latest Stable Version Scrutinizer Quality Score Code Coverage Total Downloads License PHP 7 ready

Introduction

Simple Mail Class provides a simple, chainable wrapper for creating and sending emails using the PHP mail() function. There are better options out there for sending SMTP email, which are more secure and more reliable than the mail() function. However, sometimes you just need to send a simple email. That's what we cover.

Installation via Composer

$ composer require eoghanobrien/php-simple-mail

Usage

Instantiating the class.

You have two options, you can 'new up' the class in the traditional way:

$mailer = new SimpleMail();

or instantiate it using the named static constructor make()

$mailer = SimpleMail::make();

The static constructor can be useful when you want to continue chaining methods after instantiating.

SimpleMail::make()
->setTo($email, $name)
->setFrom($fromEmail, $fromName)
->setSubject($subject)
->setMessage($message)
->send();

To header

The To header can be called multiple time, in order to pass more than one To address, simply call the setTo method as many times as needed. It takes two string parameters. The first parameter is for the email address, the second is for the name.

SimpleMail::make()
 ->setTo($email1, $name1)
 ->setTo($email2, $name2);

From header

You can carbon copy one or more addresses using the setBcc method. It takes two string parameters. The first parameter is for the email address, the second is for the name.

SimpleMail::make()
  ->setFrom('[email protected]', 'John Smith');

Cc header

You can carbon copy one or more addresses using the setCc method. It takes an array of $name => $email pairs. Alternatively, you can pass a simple numerically keyed array an the value is assumed to be the email.

SimpleMail::make()
  ->setCc(['John Smith', '[email protected]');

Bcc header

You can blind carbon copy one or more addresses using the setBcc method. It takes an array of $name => $email pairs. Alternatively, you can pass a simple numerically keyed array an the value is assumed to be the email.

SimpleMail::make()
  ->setBcc(['John Smith', '[email protected]');

Subject header

You can set the subject using setSubject method. It takes a string as the only parameter.

SimpleMail::make()
    ->setSubject("Important information about your account");

Message header

You can set the message using setMessage method. It takes a string as the only parameter.

SimpleMail::make()
    ->setMessage("My important message!");

HTML emails

If you want to include HTML in your email. Simply call the setHtml() method. It takes no parameters.

SimpleMail::make()
    ->setMessage("<strong>My important message!</strong>")
    ->setHtml();

send emails

Once you've set all your headers. Use the send() method to finally send it on it's way.

SimpleMail::make()
    ->setMessage("<strong>My important message!</strong>")
    ->send();

Full example of sending an email

$send = SimpleMail::make()
    ->setTo($email, $name)
    ->setFrom($fromEmail, $fromName)
    ->setSubject($subject)
    ->setMessage($message)
    ->setReplyTo($replyEmail, $replyName)
    ->setCc(['Bill Gates' => '[email protected]'])
    ->setBcc(['Steve Jobs' => '[email protected]'])
    ->setHtml()
    ->setWrap(100)
    ->send();
    
echo ($send) ? 'Email sent successfully' : 'Could not send email';

Example of sending an email with attachments

If you are sending an attachment there is no need to add any addGenericHeader()'s. To properly send the attachments the necessary headers will be set for you. You can also chain as many attachments as you want (see example).

$send = SimpleMail::make()
    ->setTo($email, $name)
    ->setFrom($fromEmail, $fromName)
    ->setSubject($subject)
    ->setMessage($message)
    ->setReplyTo($replyEmail, $replyName)
    ->setCc(['Bill Gates' => '[email protected]'])
    ->setBcc(['Steve Jobs' => '[email protected]'])
    ->setHtml()
    ->setWrap(100)
    ->addAttachment('example/pbXBsZSwgY2hh.jpg', 'lolcat_finally_arrived.jpg')
    ->addAttachment('example/lolcat_what.jpg')
    ->send();
    
echo ($send) ? 'Email sent successfully' : 'Could not send email';

License

php-simple-mail is free and unencumbered public domain software. For more information, see http://opensource.org/licenses/MIT or the accompanying MIT file.

php-simple-mail's People

Contributors

dweingart avatar eoghanobrien avatar fzmaster avatar gabalis avatar iammerrick avatar md2perpe avatar ndt avatar shoxty avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

php-simple-mail's Issues

fix $this->please

please FIX your site OR update site OR DELETE link FROM description;

Outlook untitled attachment

If message is with attachments, an open it in outlook. I have attachments, and untitled attachment ***.txt

No license

Hi !

Thanks for sharing your work.
I really like it !

But I can use it easily in projects because there is no clear license.
I recommand you to use the (unlicence)[http://unlicense.org/], because I think it is the simplest one.

Thanks again.
Best regards,
Thomas.

safe

is this form safe? does it protect form hacking?

Gmail and Yahoo not receiving messages

I had to change the encodeUtf8() function to receive messages on Gmail and Yahoo:

public function encodeUtf8($value) {
	$value = trim($value);
	return mb_detect_encoding($value) === 'ASCII' ? $value : $this->encodeUtf8Word($value);
}

Add attachment with setHtml() not work

When I use the code below:
php $email->setFrom('[email protected]', 'From') ->setTo('[email protected]', 'TO') ->setSubject('Mail \'subject') ->setReplyTo('[email protected]') ->setHtml() ->setMessage('My message') ->addAttachment('htaccess.txt') ->send();
I get the mail like this:
This is a multi-part message in MIME format. --27a6f9f7396604b2e7d9002567a15c0b Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable My message --27a6f9f7396604b2e7d9002567a15c0b Content-Type: application/octet-stream; name="=?UTF-8?B?aHRhY2Nlc3MudHh0?=" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="=?UTF-8?B?aHRhY2Nlc3MudHh0?=" PElmTW9kdWxlIG1vZF9yZXdyaXRlLmM+CglSZXdyaXRlRW5naW5lIE9uCglSZXdyaXRlQ29uZCAl e1JFUVVFU1RfRklMRU5BTUV9ICEtZgoJUmV3cml0ZUNvbmQgJXtSRVFVRVNUX0ZJTEVOQU1FfSAh LWQKCVJld3JpdGVSdWxlIF4oLiopJCBpbmRleC5waHAvJDEgW0xdCjwvSWZNb2R1bGU+ --27a6f9f7396604b2e7d9002567a15c0b--

But if I remove the method setHtml()

php $email->setFrom('[email protected]', 'From') ->setTo('[email protected]', 'TO') ->setSubject('Mail \'subject') ->setReplyTo('[email protected]') //->setHtml() ->setMessage('My message') ->addAttachment('htaccess.txt') ->send();

I get the mail with attchment

attachment problem

Hi
when I add attachment in localhost see attached file in my email as text only

This is a multi-part message in MIME format. --11ad93eaf86ba31d942b41449e2dcf94 Content-type:text/html; charset="utf-8" Content-Transfer-Encoding: 7bit This is a test message. --11ad93eaf86ba31d942b41449e2dcf94 Content-Type: application/octet-stream; name="test.jpg" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="test.jpg" /9j/4AAQSkZJRgABAQEAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/9sAQwABAQEBAQEBAQEBAQEB AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB/9sAQwEB AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB AQEBAQEB/8AAEQg

Quotation marks in subject lines are entity-encoded

If you try to use a subject line that contains a quotation mark it is entity-encoded.

E.g. $mail->setSubject("Amy O'Neil"); becomes

Subject: Amy O'Neil in the resulting email message.

setSubject() runs the subject string through filterOther().

filterOther() uses FILTER_SANITIZE_STRING which will encode quotes by default (as it's meant for sanitizing HTML, not plain text).

A quick fix would be to add the FILTER_FLAG_NO_ENCODE_QUOTES flag as you've done in filterName()

That doesn't necessarily fix the underlying problem of filtering subject lines as if they are HTML though. E.g. you still can't have as a subject line anything that looks like an HTML tag as it will be stripped.

Send mail after form validation.

Hi, I need help. I want send a mail with php-simple-mail. After form validation I get the setTo mail, but what is the correct setFrom mail?
Is a mail server required? Can it test it locally?

Attachments in default mail client Mac OS

Sending email with attachments:

$send = SimpleMail::make()
    ->setTo($email_to, "")
    ->setFrom($email_form, "")
    ->setSubject("Test attachments")
    ->setMessage("Text")
    ->setHtml()
    ->setWrap(80)
    ->addAttachment('1.png')
    ->addAttachment('2.png')
    ->send();

Open in "Mail" and see this:

This is a multi-part message in MIME format. --ab6e7f92212c31cf6e8a62f3505b117e Content-type:text/html; charset="utf-8" Content-Transfer-Encoding: 7bit 
Text
--ab6e7f92212c31cf6e8a62f3505b117e Content-Type: application/octet-stream; name="1.png" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="1.png" iVBORw0KGgoAAAANSUhE...
--ab6e7f92212c31cf6e8a62f3505b117e Content-Type: application/octet-stream; name="2.png" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="2.png" JVBERi0xLjMK...
--ab6e7f92212c31cf6e8a62f3505b117e--

Tested in other programs and problems did not arise.

The make() function is redundant

This works just as well

   $mail = new SimpleMail();
   $mail
	    ->setTo($to,$name)
	    ->setFrom($fromUser, $fromName)
	    ->setSubject($subject)
	    ->setMessage($body)
	    ->setReplyTo($to,$name);

$send = $mail->send();

Bcc header not being set

Hi,

Currently i think the Bcc is not working as the header is not being set.
And as parameter addMailHeade for Bcc:
->addMailHeader('BCC', '[email protected]', 'Steve Jobs')
should be an array maybe
->addMailHeader('BCC', $recipients)

Ionut

Problems with sending attachments

Hello,

first of all a great thank you for your lib! But I have an problem sending a mail with attachments. I use the following code:

$mailer->setTo( '[email protected]', 'Name' )
->setFrom('[email protected]', 'Domain.com')
->setSubject( 'This is a test message' )
->addAttachment( DIR . '/test.txt' )
->setMessage('HALLO');
$mailer->send();

And I get the following mail. but can't see the attachment:

From: multipart/[email protected] 1.0Content-Type:
To:
Date: Tue, 27 Aug 2013 12:16:32 +0200
Subject: This is a test message

"
813545f572c12c8738dfd0d63b9ee74dThis is a multi-part message in MIME format.
--813545f572c12c8738dfd0d63b9ee74d
Content-type:text/html; charset="utf-8"
Content-Transfer-Encoding: 7bit
HALLO

--813545f572c12c8738dfd0d63b9ee74d
Content-Type: application/octet-stream; name="test.txt"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="test.txt"

aGFsbG8K

--813545f572c12c8738dfd0d63b9ee74d

I have testet it with different attachments (txt, pdf, docx) and different mail addresses. Nothing works. Any ideas?

Email not secure

Anyone can help me? I have an issue about "secure email", how do I encrypt using this plugin?

encodeUtf8Word() should prefer ASCII when possible

When sending an email only containing ASCII:

    require_once("./class.simple_mail.php");

    $from = '[email protected]';

    $email = new SimpleMail();
    $email->setFrom($from, 'Me')
        ->setParameters('-f'.$from)
        ->setReplyTo($from, '')
        ->setTo('[email protected]', 'Someone')
        ->setTo('[email protected]', 'Another one')
        ->setSubject('Subject')
        ->setMessage('Check your email.');
    $email->send();

I got the following error in my e-mail Inbox:

host mailfilter.hostnet.nl[91.184.19.251] said: 550
    Subject contains invalid characters. (in reply to end of DATA command)

So I changed the encodeUtf8Word() to:

    /**
     * encodeUtf8Word
     *
     * @param string $value The word to encode.
     *
     * @return string
     */
    public function encodeUtf8Word($value)
    {
        $isAscii = true;
        for ($i=0; $i<strlen($value); $i++) {
            $ch = ord(substr($value, $i, 1));
            if ($ch >= 128) {
                $isAscii = false;
                break;
            }
        }
        
        if ($isAscii) {
            return $value;
        } else {
            return sprintf('=?UTF-8?B?%s?=', base64_encode($value));
        }
    }

After this change, the email sends correctly, no error anymore.

So encodeUt8Word() should prefer plain ASCII, without encoding.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.