1. Products
  2.   Email
  3.   PHP
  4.   Mailer
 
  

Open Source PHP  Library to Send Email Messages 

PHP API that allows sending messages with attachment, sending messages to multiple users, Twig integration, UTF-8 characters support and so on.

The Mailer and Mime components are very useful parts of the Symfony framework for creating and sending email messages. It is open source and is available under the MIT License. The component helps software programmers to send emails from their PHP applications as well as offers easy integration with other popular mailing services. There are two main parts of the Mailer component; the Transport and Mailer itself.

The great thing about Symfony's mailer component is the high availability. It uses a technique known as "failover" which makes sure that emails are sent even if one mailer server fails. The failover transport is configured with two or more transports. So if one fails it will automatically switch to the other transport to complete the sending task. It also very effectively manages load balancing and uses the "round-robin" technique to distribute the mailing workload across multiple transporters.

The Mailer component has included support for several important features related to email message handling such as sending messages with attachments, sending messages to multiple users, multipart messages support, Twig integration, UTF-8 characters support, embed images inlining CSS Styles, messages encryption, and many more.

Previous Next

Getting Started with Mailer 

The recommended way to install the Mailer component is via Composer, please use the following command for easy installation.

Install Mailer via Composer

 $ composer require symfony/mailer

Generate and Send Emails via PHP Library

The open source Mailer library has included support for email message creation and sending via PHP code. Developers need to create email objects and provide the required information. Once ready the message will be sent to the recipients via the configured transport. You can easily send emails to multiple users by selecting fields like From, To, BCC, and Cc fields. You can also easily attach files, embed images, and include other contents inside your email messages. Here is an example that shows how software developers can create and send an email messages using PHP commands.

How to Send Email Messages inside PHP Applications?

$ composer require symfony/mailer
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mime\Email;

$transport = Transport::fromDsn('smtp://localhost');
$mailer = new Mailer($transport);

$email = (new Email())
    ->from('hello@example.com')
    ->to('you@example.com')
    //->cc('cc@example.com')
    //->bcc('bcc@example.com')
    //->replyTo('fabien@example.com')
    //->priority(Email::PRIORITY_HIGH)
    ->subject('Time for Symfony Mailer!')
    ->text('Sending emails is fun again!')
    ->html('

See Twig integration for better HTML integration!

'); $mailer->send($email);

Attach Files and Images to Emails via PHP

Sharing documents and photos as email attachments is easy and cost-effective. Moreover, it is often required to share up-to-date data and files for the timely completion of collaborative tasks. One fast and easy way to do this is via email attachments. The Mailer library enables software developers to easily attach and send documents like PDF, Microsoft Word, Images, and many more. Here is an example that shows how software developers can add attachments to an email messages inside PHP applications.

How to Add Attachment to Email Messages inside PHP Applications?

use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\File;
// ...

$email = (new Email())
    // ...
    ->addPart(new DataPart(new File('/path/to/documents/terms-of-use.pdf')))
    // optionally you can tell email clients to display a custom name for the file
    ->addPart(new DataPart(new File('/path/to/documents/privacy.pdf'), 'Privacy Policy'))
    // optionally you can provide an explicit MIME type (otherwise it's guessed)
    ->addPart(new DataPart(new File('/path/to/documents/contract.doc'), 'Contract', 'application/msword'));

Email Messages Encryption Support

Encryption helps users to protect their email messages from unwanted access and prevent hackers from accessing secure data and messages. The open source Mailer library has included complete support for email message encryption using PHP commands. A certificate is used while encrypting an email message. It encrypts the entire message including attachments, images, contents, etc. Once the message is delivered the recipients that have the corresponding key can access and read the message. The following example show how programmers can use a certificate to encrypt an email message and send to recipients that have the corresponding private key to read the original message contents.

How to Encrypt an Email Message and Send It using PHP Code?

use Symfony\Component\Mime\Crypto\SMimeEncrypter;
use Symfony\Component\Mime\Email;

$email = (new Email())
    ->from('hello@example.com')
    // ...
    ->html('...');

$encrypter = new SMimeEncrypter('/path/to/certificate.crt');
$encryptedEmail = $encrypter->encrypt($email);
// now use the Mailer component to send this $encryptedEmail instead of the original email
$firstEmail = (new Email())
    // ...
    ->to('jane@example.com');

$secondEmail = (new Email())
    // ...
    ->to('john@example.com');

// the second optional argument of SMimeEncrypter defines which encryption algorithm is used
// (it must be one of these constants: https://www.php.net/manual/en/openssl.ciphers.php)
$encrypter = new SMimeEncrypter([
    // key = email recipient; value = path to the certificate file
    'jane@example.com' => '/path/to/first-certificate.crt',
    'john@example.com' => '/path/to/second-certificate.crt',
]);

$firstEncryptedEmail = $encrypter->encrypt($firstEmail);
$secondEncryptedEmail = $encrypter->encrypt($secondEmail);

Use Tags and Metadata in Emails

The open source Mailer library has included support for adding tags and metadata to your email messages with ease. They are very useful for grouping emails together, tracking emails, and workflow. Please remember that your transport will convert them to their appropriate format if it supports headers otherwise it does not support tags and metadata, they will be added as custom headers inside your emails.

 

 English