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.
What is Mailer Library?
The Symfony framework's Mailer and Mime components are highly helpful for composing and sending emails. It is accessible under the MIT License and is open source. The component provides simple connection with other well-known mailing providers and aids programmers in sending emails from PHP applications. The Mailer component consists of the Mailer itself and the Transport.
The high availability of Symfony's mailer component is its best feature. It employs a method called "failover" to ensure that emails are sent even in the event that one mailer server fails. Two or more transports are set up for the failover transport. In order to finish the sending task, it will immediately switch to the other transport if the first one fails. Additionally, it distributes the mailing effort among several transporters using the "round-robin" technique and handles load balancing quite well.
The Mailer component supports a number of crucial email message handling features, including sending attachments, sending messages to multiple recipients, supporting multipart messages, integrating Twig, supporting UTF-8 characters, embedding images inline with CSS styles, encrypting messages, and many more.
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
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.