Login

In this sixth article of a series I discuss how to handle file attachments and sending HTML email messages with the Swift Mailer library. As you’ll soon see, performing these tasks is a very straightforward process.

If you’re an experienced PHP developer, then you know that coding functions or classes that send MIME email is one of those things that you want to do only once in your lifetime. This process often requires dealing indirectly (or directly, when programming TCP sockets) with the complexities of the SMTP protocol, which admittedly isn’t exactly fun.

To help you avoid this annoying task, there are generous souls that have developed a nice variety of libraries that let you build email applications very quickly and with only minor effort. At this point, it’s quite probable that you’ve tried PEAR Mail, or a similar package, which does a good job of sending MIME email with PHP.

However (and this is only my personal opinion), there’s another email library that’s much better than PEAR’s, since it combines a flat learning curve with a great API, plus a clever implementation of some popular design patterns. Yes, as you might have guessed from this article’s title, I’m talking about Swift Mailer, a powerful package created by Chris Corbin that allows you to develop email programs with ease, using a strict object-oriented interface.

Naturally, if you’ve been a patient reader and have already gone through all of the articles that precede this one, then you now have a clear idea of how to utilize some of the classes provided by Swift Mailer to send basic email messages using different SMTP transports, or how to reach multiple recipients by means of Bcc and Cc MIME headers.

Nonetheless, the real fun is just about to start, since Swift Mailer allows you to manipulate file attachments in all sorts of clever ways, not to mention its remarkable capabilities for sending email messages in HTML format. Thus, in this part of the series, I’m going to explain how to use the library for handling attachments in a basic way, and show you how to send HTML email with it as well.

{mospagebreak title=Sending copies with the setCc() and setBcc() methods}

Before I explain how to handle attachments and send HTML email messages with Swift Mailer, I’d like to reintroduce the script created in the previous tutorial of this series, which demonstrated how to send a blind carbon copy via the “setBcc()” method offered by the library.

With that point clarified, here’s the small program that performs the aforementioned task:

->setBody(‘Hey, how are you? I am sending you a message with the cool Swift Mailer library’);

// send the email message

if ($mailer->send($message))

{

echo ‘The message was sent successfully!’;

}

else

{

echo ‘Error sending email message’;

}

As you can see from the above script, both the “setCc()” and “setBcc()” methods make it really easy to send copies of an original message to a number of predefined recipients. Actually, the process is reduced to calling the methods in question with the proper incoming arguments, prior to creating the message itself. It’s nothing too complex to grasp, really.

Well, now that you’re hopefully familiar with using the methods discussed before, it’s time to explore other useful features provided by Swift Mailer. So, in accordance with the concepts that I expressed in the introduction, over the course of the upcoming section I’m going to explain how to handle file attachments in a truly approachable fashion.

As you may guess, this process will be performed with the help of a brand new class included with the library, called “Swift_Attachment.” But I’m getting ahead of myself, so if you wish to learn more about this class, click on the link displayed below and keep reading.

{mospagebreak title=Handling file attachments with Swift Mailer}

Obviously, any modern email library must be able to attach files to messages, and naturally Swift Mailer isn’t an exception. But what’s really exceptional in this particular case is that the library includes a separate class that does this, not surprisingly called “Swift_Attachment.”

Nevertheless, to help you understand how to use this class a bit more clearly, below I developed another simple script. This time, it not only uses the methods that you learned before for sending the email message, but attaches an image file to it. Take a look at it:

->setBody(‘Hey, how are you? I am sending you a message with the cool Swift Mailer library. Make sure to check the attached file!’)

->attach(Swift_Attachment::fromPath(‘image1.jpg’));

// send the email message

if ($mailer->send($message))

{

echo ‘The message was sent successfully!’;

}

else

{

echo ‘Error sending email message’;

}

The above example definitely looks much more interesting, right? As you can see, it uses the “Swift_Attachment” class along with its “fromPath()” method to attach a fictional image file to the message before sending it.

In this particular example, the file being attached resides in the same location of the script, but it’s also possible to specify a full path for the file. However, this feature will be covered in the next tutorial, so for the moment, focus your attention on the previous script and grasp its driving logic.

Done? Good. Now that you’ve learned how to compose and send a message with an attached image file, the next thing I’m going to show you is Swift Mailer’s capabilities for dispatching email in HTML format.

This topic will be discussed in the last section of this tutorial. Therefore, click on the link that appears below and read the next few lines.

{mospagebreak title=Sending email messages in HTML with the addPart() method}

Email messages in HTML format can be sent in several ways with Swift Mailer. However, the simplest way to do this is by means of another method of the “Swift_Message” class, called “addPart(),” which permits you to add a new part to a message — in this case, specifying that the content of that section will be delivered in HTML.

To have a clearer idea of how the “addPart()” method works, I coded another example, which demonstrates its usage. Here it is:

->setBody(‘Hey, how are you? I am sending you a message with the cool Swift Mailer library. Make sure to check the attached file!’)

->addPart(‘<p>This part of the message has been formatted in HTML to make it look nicer</p>’, ‘text/html’)

->attach(Swift_Attachment::fromPath(‘image1.jpg’));

// send the email message

if ($mailer->send($message))

{

echo ‘The message was sent successfully!’;

}

else

{

echo ‘Error sending email message’;

}

Do you realize how simple it is to send email messages in HTML? I bet you do! What’s more, the previous example not accomplishes this task via the pertinent “addPart()” method, but it also attaches an image file to it. What else can you ask for?

Finally, with this example, I’m concluding this sixth installment of the series on using the Swift Mailer library. As always, feel free to tweak all of the code samples shown in this article, so you can sharpen your skills in working with this powerful email package.

Final thoughts

That’s all for now. In this sixth part of the series I discussed how to handle file attachments and sending HTML email messages with the Swift Mailer library. As you saw a few moments ago, performing these tasks is a very straightforward process that doesn’t require any further explanation due to its simplicity.

In the upcoming tutorial, I’m going to extend my discussion of the previous “Swift_Attachment” class. It also allows you to attach files by using different paths. Since this specific feature will be explored in depth in the aforementioned article, you don’t have any excuses to miss it!