Select Page

Send Emails in PHP Using Swift Mailer

Send Emails in PHP Using Swift Mailer

In this article, we’re going to explore the Swift 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 Swift Mailer library.

What Is Swift 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.

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

In fact, it’s a pretty straightforward process to send emails using the Swift Mailer library.

  1. Initialize the Transport (SMTP/Sendmail) object.
  2. Initialize the Mailer object with that Transport.
  3. Initialize the Message 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.

Installation and Configuration

In this section, we’ll go through installation and configuration of the Swift 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 Swift Mailer library.

Once you’ve installed Composer, go ahead and grab the Swift Mailer library using the following command.

With that, the Swift 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? In fact, it’s just a matter of including the autoload.php file created by Composer in your application, as shown in the following snippet.

How to Send Emails

In the previous section, we explored how to install the Swift Mailer library using Composer. In this section, we’ll 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.

Initialize Swift Mailer

The Swift 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 Swift_SendmailTransport object.

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

Create a Message

After creating the transport and mailer objects, the only remaining thing is to instantiate the Swift_Message object and decorate it with the necessary attributes.

Now, we’ll use the $message object to prepare the contents of our message. To start with, the setSubject method allows you to set the subject of the email.

The setFrom method is used to set the “From” address of the email.

Moving ahead, let’s set the “To” address of the email. In fact, there are a couple of variations for setting the recipients of the email. If you want to set a single recipient, you can use the addTo method, and the setTo method, on the other hand, is used to set multiple recipients.

The addCc and addBcc methods are used to set the CC and BCC addresses of the email respectively.

Attaching Files

Next, let’s have a look at how you can attach a file to an email. 

You first need to instantiate the Swift_Attachment object with a valid filename. After creating the attachment object, you can add it to the email with the attach method. Also, you can use the setFilename method if you want to change the filename that will appear in the message attachment.

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 returns the unique ID of the embedded object, which you can use later on in the message while referencing the image via the src property.

The Message Body

Next, let’s set the email body by using the setBody method.

If you want to set the HTML version of the message, you can use the addPart method, as shown in the following snippet. As you can see, we’re using $cid to reference the image we embedded earlier.

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! Let me know in the comment section if you face any issues.

Conclusion

Today, we looked at one of the most popular PHP libraries for sending emails: Swift Mailer. With this library, you can effortlessly send emails from your PHP scripts.

Feel free to post your thoughts and queries using the form below.

Link To Source

About The Author

Leave a reply

Your email address will not be published. Required fields are marked *

000webhost logo