Composer library to handle emails with PHP

1.0.16 2017-03-27 08:27 UTC


Composer library to send emails with PHP

This package contains all classes required to easily build e-mails with PHP, in an object-oriented way.

Attachments and embeddings are supported.

Setup instructions

To install net-tools/mailing package, just require it through composer : require net-tools/mailing:^1.0.0.

How to use ?

Quick email sending

To send an email in an easy way, just get an instance of Mailer with default email sending strategy, and call expressSendmail method. If no attachments to send, omit the last parameter.

  '<b>This is a</b> test', 
  'This is a subject', 

Email technical parts (text/plain, text/html, multipart/alternative, multipart/mixed) will be created automatically ; the default email sending strategy send emails through PHP built-in Mail() function.

Build emails

If you want to have more control when sending emails, you may build them with Mailer :

$mail = Mailer::addTextHtmlFromText('\*\*This is a\*\* test');
$mail = Mailer::addAttachment($mail, '/home/tmp/invoice.pdf', 'your_invoice.pdf', 'application/pdf');
Mailer::getDefault()->sendmail($mail, '', '', 'This is a subject');

To send emails with SMTP protocol (or any other email sending strategy in the MailSenders subfolder), create the Mailer (instead of getting it throught getDefault) with the appropriate class name and an array of parameters :

$smtpmailer = new Mailer(MailSender::SMTP, array('host'=>'', 'username'=>'user', 'password'=>'1234'));

Parse an EML file/string to create a MailContent object

Sometimes you have an email and you want to display it on screen. However, you can't echo the raw content. You have to parse the email content to extract the appropriate part (generally, the text/html part) and if necessary the attachments (multipart/mixed part).

To parse the email, just use the EmlReader class and the fromString or fromFile static methods. They will return a MailContent object :

// assuming that $email is a very simple email with a multipart/alternative content
$mail = EmlReader::parseString($email);

// this line prints MailMultipart (class name of the MailContent object)
echo get_class($mail);

// the following lines extract the text/plain and text/html sub-parts
$textplain_content = $mail->getPart(0)->toString();
$htmlpart_content = $mail->getPart(1)->toString();

If your email contains attachments or embeddings, don't forget to call destroy static method to delete temporary files created during parsing to store attachments and embeddings :


API Reference

To read the entire API reference, please refer to the PHPDoc here :


To test with PHPUnit, point the -c configuration option to the /phpunit.xml configuration file.