HomePHP Handling Attachments and Sending HTML Email Messages with Swift Mailer
Handling Attachments and Sending HTML Email Messages with Swift Mailer
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.
Now, let's leave the boring theory behind and continue exploring the numerous features of Swift Mailer. Let's go!