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

Open Source PHP Library to Send Email Messages

Top PHP API that allows Creating and Sending Email Messages with Attachment, Sending Messages to Multiple Users, Twig Integration, UTF-8 Characters Support and so on.

What is Mailer Library?

Forging a reliable and robust email communication layer within a PHP application is streamlined by the Symfony framework's Mailer and Mime components. As an open-source project distributed under the permissive MIT License, this powerful toolkit enables developers to compose, format, and dispatch emails directly from their Symfony-based or standalone PHP projects with exceptional ease. At its core, the component is elegantly structured around the primary Mailer and its configurable Transport layer, designed from the ground up for simple integration with major third-party email service providers and SMTP servers, ensuring seamless connectivity.

A hallmark of the Symfony Mailer component is its enterprise-grade high availability and intelligent delivery management. It incorporates a sophisticated "failover" transport strategy, where two or more mailer transports are configured to guarantee dispatch even if a primary server fails; the system automatically switches to a backup transport without interrupting the workflow. For optimal performance and load distribution, it also utilizes a "round-robin" technique, effectively balancing the email-sending workload across multiple configured transports. Beyond resilience, the component is packed with essential features for modern email handling: it supports sending to multiple recipients with CC/BCC, managing file attachments, crafting multipart/alternative messages, and fully integrating Twig templates for dynamic content. Developers can also leverage capabilities like native UTF-8 support for global character sets, in-line image embedding with CSS styling, and message encryption, making the Symfony Mailer a comprehensively equipped solution for building scalable and fault-tolerant email functionality.

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

PHP code is now supported for creating and sending email messages thanks to the free source Mailer module. Email objects must be created by developers, who must also supply the necessary data. When the message is ready, the configured transport will deliver it to the recipients. Choosing sections like From, To, BCC, and Cc makes it simple to send emails to numerous recipients. Additionally, it's simple to embed photographs, attach files, and add other material to your emails. This example demonstrates how PHP commands can be used by software developers to produce and send email messages.

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 via PHP

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