Setting up a web application to send plain text email is easy. What if you need the email to handle other content, such as images or special document formats? This article explains how to design a PHP class for sending email with attachments.
Email has become an important part of life these days. More and more web applications are expected to be capable of using email to communicate. Online stores in particular need a way of confirming orders, sending gifts certificates and distributing information about specials among other things. You may want to send an HTML formatted greeting to the registered visitors of your blog, publish a newsletter in an automated fashion or even code a custom email application for your own use on a personal web server. You may even want to provide basic web email services to users of a small web community.
Sending plain text email is pretty easy in most web application environments. Within a PHP application you can use the "mail()" function. Where things get more interesting is when your requirements extend to other content such as images, HTML, PDF, RTF, CSV or other document formats
This article outlines the design of a PHP class for sending email with attachments. Everything you need to do this is supplied by the core PHP system, so nothing additional (such as non-bundled extensions) should be needed save for the required set up and connection between PHP and a SMTP mail package such as Sendmail or Postfix. For details on setting up Sendmail and PHP, read through the appropriate section of the PHP manual. If you are already using the mail() function in PHP, you are ready to go right now.
The Benefits of the Design
Of late I have had my fill of code written with a single application and purpose in mind. The kind of code I mean is long procedural code, hardcoded with names and string values specific to that project, and almost always spread out over multiple documents. Want to modify it? Good luck! Want to use it somewhere else? Yeah right! Many times it might seem quicker to write something in this way, but when it comes time to alter it in any way you've got a problem. If you didn't write it in the first place, it makes it that much worse.
This class was designed based on the functionality of some code written in that very way. Rather than labor for hours trying to modify this code I decided to encapsulate the operation in a class. What I ended up with is a piece of code that can be used in any project and never has to be written again. For those that haven't used classes, coding in this manner allows you to assemble larger applications out of smaller widgets. How simple or complex in function you make your classes are up to you, but you may find that keeping it simple is the best approach. The PHP language may not have all of the features of a completely object oriented language such as Java or C# but what it does have can save you hours of frustration if you let it.
The mime_mailer class has three methods (including the constructor) that can be considered "public" and is all you need to reliably send email with attachments from a PHP application. You could easily extend this class with other classes that produce files of various types, provide shopping, checkout and payment services, process input from forms, work with the local file system and a long list of other useful functions.