HomePHP Handling Attachments in MIME Email with PHP
Handling Attachments in MIME Email with PHP
In this second tutorial of the series, I’m going to teach you how to improve the initial structure of the MIME mailer class developed in the first article by giving it the ability to work directly with attachments. Sounds fairly interesting, right? Then don’t waste any more time in preliminaries and begin reading this article now!
Sending plain text email with PHP is a pretty simple task that can be tackled with minor efforts thanks mostly to the easy learning curve of its popular “mail()” built-in function. You know, you code a simple script that sends email messages to a bunch of recipients. It feeds this function with the proper email headers and the text of the message and… voila! The script works like a charm, and best of all, it only took a few minutes to be developed.
However, things get slightly more complicated when it comes to sending email messages in HTML, or when it’s necessary to work with attachments, since these tasks require the use of MIME, which is the mail extension that permits you to send messages formatted in HTML, include inline files, and so forth.
The good news concerning the use of MIME email with PHP is that you can develop your own reusable functions in order to send messages in HTML and handle a huge variety of attachments. You can also take advantage of the object-oriented paradigm and create a highly modular MIME mailer class that does all of this hard work for you.
In either case, in this group of tutorials you’ll find useful material regarding the construction of an expansible PHP class that can be utilized to send MIME-compliant messages very easily and work with different types of attachments.
And speaking of sending MIME-compliant email messages, in the last article of this series I went through the development of a basic mailer class in PHP 4. It was capable of dispatching plain text messages to a specified recipient by way of an intuitive API. Indeed, the functionality of this class was pretty limited, since it wasn’t capable of working with HTML messages or attachments.