Advertisement

# Send Emails in PHP Using Symfony Mailer

Read Time: 5 mins

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.

1. initialize the transport (SMTP or sendmail) object
2. initialize the mailer object with that transport
3. initialize the email object
4. 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.

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:

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.

## 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.

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.

Of course, if you would like to use the Sendmail protocol, you'll need to initialize the corresponding SendmailTransport object.

Once the transport is created, we need to initialize a Mailer object and pass the transport that we've created already.

### 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.

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.

The from method is used to set the "From" address of the email.

Moving ahead, let's set the "To" address of the email.

### 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.

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.

### Step 4: Create the Message Body

Next, let's set the email body by using the text method.

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.

### Step 5: Send the Message!

Finally, we'll use the send method of the Mailer object to send the 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.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.