Provides a simple, chainable wrapper for creating and sending emails using the PHP mail() function.
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.
$ composer require eoghanobrien/php-simple-mail
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()
php $mailer = SimpleMail::make();The static constructor can be useful when you want to continue chaining methods after instantiating.
php SimpleMail::make() ->setTo($email, $name) ->setFrom($fromEmail, $fromName) ->setSubject($subject) ->setMessage($message) ->send();
Toheader
The
Toheader can be called multiple time, in order to pass more than one
Toaddress, simply call the
setTomethod 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);
Fromheader
You can carbon copy one or more addresses using the
setBccmethod. 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');
Ccheader
You can carbon copy one or more addresses using the
setCcmethod. It takes an array of
$name => $emailpairs. 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]');
Bccheader
You can blind carbon copy one or more addresses using the
setBccmethod. It takes an array of
$name => $emailpairs. 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]');
Subjectheader
You can set the subject using
setSubjectmethod. It takes a string as the only parameter.
SimpleMail::make() ->setSubject("Important information about your account");
Messageheader
You can set the message using
setMessagemethod. It takes a string as the only parameter.
SimpleMail::make() ->setMessage("My important message!");
HTMLemails
If you want to include HTML in your email. Simply call the
setHtml()method. It takes no parameters.
SimpleMail::make() ->setMessage("My important message!") ->setHtml();
sendemails
Once you've set all your headers. Use the
send()method to finally send it on it's way.
SimpleMail::make() ->setMessage("My important message!") ->send();
$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';
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';
php-simple-mail is free and unencumbered public domain software. For more information, see http://opensource.org/licenses/MIT or the accompanying MIT file.