HomePHP Embedding Attachments into Email Messages with Swift Mailer
Embedding Attachments into Email Messages with Swift Mailer
In this ninth part of a ten-part series, you'll learn how to use a couple of methods provided by Swift Mailer to directly embed attachments into email messages. These can be very helpful when sending newsletters that contain numerous images, which you don’t want to be filtered and discarded by popular web-based email systems such as Hotmail and Yahoo.
Among the huge variety of email libraries written in PHP 5, there's one that stands out from the rest due to its well-structured programming interface, strict use of the object-oriented paradigm, and (if all of these features aren't good enough for you), its incredibly flat learning curve.
Of course, as you might have guessed from this article's title, I'm speaking specifically of Swift Mailer. It's a powerful package that you can download freely at http://swiftmailer.org that will let you build complex email applications in a few simple steps. You'll make use of its intuitive API and clever implementation of some key concepts applied to software development, such as design patterns and object aggregation.
Naturally, this isn't breaking news for you if you've read the previous articles in this series. In them, I explored the most relevant features offered by Swift Mailer, including its ability to send email messages formatted in HTML, as well as its capacity for attaching files from multiple locations on local hosts and remote servers.
Nevertheless, at this point I'm only scratching the surface when it comes to demonstrating Swift Mailer's impressive capabilities for handling attachments. You see, so far, all of the examples developed in the previous parts of the series showed how to annex files to an email message by assigning an "attachment" value to the "Content-disposition" MIME header, which works well. But there are cases where this might not be good enough.
For example, what if you need to embed a bunch of images directly into a message? Say you're sending a newsletter and you want to make it much more appealing to your web site's visitors. This is a good but scary question, isn't it? Fear not, because Swift Mailer permits you to manipulate MIME headers in a straightforward fashion, thus allowing you to embed files directly into emails very easily.
So, in the next few lines I'm going to explain in depth how to use embedded attachments with the Swift Mailer library, which will hopefully be an instructive experience for you. Let's get going!