In this article, we're going to explore the Symfony Mailer library, which allows you to send emails from PHP applications. Starting with installation and configuration, we'll go through a real-world example that demonstrates various aspects of sending emails using the Symfony Mailer library.
What Is Symfony Mailer?
When it comes to sending emails in PHP applications, you have a plethora of options to choose from. You might even end up creating your own wrapper to set up email features quickly. However, you're always in luck if you're using a well-maintained and feature-rich library.
Symfony Mailer is a popular library for sending emails from PHP applications, and it's widely accepted by the PHP community. It's a feature-rich library in the sense that it covers almost every aspect of sending emails, from setting up different transports to customizing the message that's being sent. Also, if you've heard of the Swift Mailer library, it's the predecessor of the Symfony Mailer library—Symfony Mailer is the new and improved version.
In fact, it's a pretty straightforward process to send emails using the Symfony Mailer library.
- initialize the transport (SMTP or sendmail) object
- initialize the mailer object with that transport
- initialize the email object
- format and send the message
In the next section, we'll go through a real-world example to demonstrate each of the aforementioned steps.
1. Installation and Configuration
In this section, I'll show you how to install and configure the Symfony Mailer library. The installation is pretty straightforward, as it's already available as a Composer package. Before we go ahead, make sure you've installed Composer because we'll need it to install the Symfony Mailer library.
Once you've installed Composer, grab the Symfony Mailer library using the following command.
1 |
$composer require symfony/mailer
|
With that, the Symfony Mailer library should be installed, along with the necessary dependencies in the vendor directory. And the contents of the newly created composer.json should look like this:
1 |
{
|
2 |
"require": { |
3 |
"symfony/mailer": "^5.4" |
4 |
}
|
5 |
}
|
So that's the installation part, but how are you supposed to use it? It's just a matter of including the autoload.php file created by Composer in your application, as shown in the following snippet.
1 |
<?php
|
2 |
require_once './vendor/autoload.php'; |
3 |
|
4 |
// your application code...
|
5 |
?>
|
2. Create an Emailer Script
We've explored how to install the Symfony Mailer library using Composer. Now let's start implementing a real-world example.
Go ahead and create the email.php file with the following contents.
1 |
<?php
|
2 |
require_once './vendor/autoload.php'; |
3 |
|
4 |
use Symfony\Component\Mailer\Transport; |
5 |
use Symfony\Component\Mailer\Mailer; |
6 |
use Symfony\Component\Mime\Email; |
7 |
|
8 |
// Create a Transport object
|
9 |
$transport = Transport::fromDsn('smtp://username:password@hostname:port'); |
10 |
|
11 |
// Create a Mailer object
|
12 |
$mailer = new Mailer($transport); |
13 |
|
14 |
// Create an Email object
|
15 |
$email = (new Email()); |
16 |
|
17 |
// Set the "From address"
|
18 |
$email->from('sender@example.test'); |
19 |
|
20 |
// Set the "From address"
|
21 |
$email->to('recepient@example.test'); |
22 |
|
23 |
// Set a "subject"
|
24 |
$email->subject('Demo message using the Symfony Mailer library.'); |
25 |
|
26 |
// Set the plain-text "Body"
|
27 |
$email->text('This is the plain text body of the message.\nThanks,\nAdmin'); |
28 |
|
29 |
// Set HTML "Body"
|
30 |
$email->html('This is the HTML version of the message.<br>Example of inline image:<br><img src="cid:nature" width="200" height="200"><br>Thanks,<br>Admin'); |
31 |
|
32 |
// Add an "Attachment"
|
33 |
$email->attachFromPath('/path/to/example.txt'); |
34 |
|
35 |
// Add an "Image"
|
36 |
$email->embed(fopen('/path/to/mailor.jpg', 'r'), 'nature'); |
37 |
|
38 |
// Send the message
|
39 |
$mailer->send($email); |
40 |
Let's go through how this code works.
Step 1: Initialize Symfony Mailer
The Symfony Mailer library supports different transports like SMTP and Sendmail while sending an email. So the first thing that you need to do is to initialize the Transport
object.
In the above example, I've used the SMTP transport to send emails.
1 |
$transport = Transport::fromDsn('smtp://sajal@aum.bz:aumaspl123@smtp.gmail.com:587'); |
Of course, if you would like to use the Sendmail protocol, you'll need to initialize the corresponding SendmailTransport
object.
1 |
$transport = new SendmailTransport(); |
Once the transport is created, we need to initialize a Mailer
object and pass the transport that we've created already.
1 |
$mailer = new Mailer($transport); |
Step 2: Create a Message
After creating the transport and mailer objects, the only remaining thing is to instantiate the Email
object and decorate it with the necessary attributes.
1 |
$email = (new Email()); |
Now, we'll use the $email
object to prepare the contents of our message. To start with, the setSubject
method allows you to set the subject of the email.
1 |
$email->setSubject('Demo message using the Symfony Mailer library.'); |
The from
method is used to set the "From" address of the email.
1 |
$email->from('sender@example.test'); |
Moving ahead, let's set the "To" address of the email.
1 |
$email->to('recepient@example.test'); |
Step 3: Attaching Files
Next, let's have a look at how you can attach a file to an email.
You can use the attachFromPath
method to attach a file. It's recommended to use the absolute path of the file to make sure the file is available in all circumstances.
1 |
$email->attachFromPath('/home/sajal/project/ajaydave/training-project/symfonymailer/example.txt'); |
Along with regular file attachments, sometimes you want to embed images in the message text. You can do that by using the embed
method, as shown in the following snippet. The embed
method allows you to attach the unique ID of the embedded object, which you can use later on in the message while referencing the image via the src
property.
1 |
$email->embed(fopen('/home/sajal/project/ajaydave/training-project/symfonymailer/mailor.jpg', 'r'), 'nature'); |
Step 4: Create the Message Body
Next, let's set the email body by using the text
method.
1 |
$email->text('This is an important message!'); |
If you want to set the HTML version of the message, you can use the html
method, as shown in the following snippet. As you can see, we're using cid
to reference the image we embedded earlier.
1 |
$email->html('This is the HTML version of the message.<br>Example of inline image:<br><img src="cid:nature" width="200" height="200"><br>Thanks,<br>Admin'); |
Step 5: Send the Message!
Finally, we'll use the send
method of the Mailer
object to send the email.
1 |
$mailer->send($email); |
Try running the script, and you should receive an email!
Conclusion
Today, we looked at one of the most popular PHP libraries for sending emails: Symfony Mailer. With this library, you can effortlessly send emails from your PHP scripts.