PHP Mail Scripts using SMTP transport, a guide for beginners
PHP has a very simple mail function which is used very often for basic text mail messages. What if you need to attach files or if you need to send your e-mail messages via SMTP? Than it’s time to use a more advanced script. This is because the standard mail function has only limited standard capabilities. There are many reasons to use SMTP transport for sending e-mail messages from your web application, some of them are:
- Many shared hosting providers doesn’t allow to use the PHP mail() function for security reasons
- Your web application is more flexible if you use the Simple Message Transfer Protocol (SMTP). This way your e-mail function is not limited to the servers port or e-mail configuration anymore.
- SMTP is much more powerful and secure (using SSL)
In this article we will compare three of the bigger PHP projects which allow to send e-mail messages via SMTP, including attachments.
- The Mail class included in the Zend Framework (http://framework.zend.com/)
- Swift Mailer (http://swiftmailer.org/)
- PHPMailer (http://phpmailer.worxware.com)
We reviewed these three PHP classes because they’re written for PHP5 and they are updated frequently. For our review we tried the provided examples and the documentation. We’re sure that all three classes are very powerful and offer many functions for almost every type of web application. Because this review should help the beginning PHP developer, this article is sho-case for a few functions only.
Our test case for this review
For our example we tested all three classes to send a plain text mail message with a single image attachment, using SMTP transport with authentication. Here is the good news; as a more experienced PHP user, I was able to use the provided examples within several minutes for each of the classes.
Zend Mail Class
I don’t like the documentation from the Zend framework, you need to check many pages to get all the required code for the snippet above. You need to create a second object to send your message via SMTP. I’m missing the information on their site about how-to test the “send” function to create use a success or error message. Using the class is not very difficult, but installing the Zend Framework might be a hard job for the beginner. This class is a great solution for people already using the Zend Framework or where the the library is provided by the hosting provider.
The snippet looks similar to the code from the Zend mail class, but you have to create 4 different objects:
- An object for the SMTP transport (Swift_SmtpTransport)
- The object that will send the message (Swift_Mailer)
- The message object for all mail parts (Swift_Message)
- An object for the attachment (Swift_Attachment)
The configuration/installation is much easier than for the Zend Framework, just include one single file and you’re ready to use the class. If you like this OOP (Object Oriented Programming) style used in this class, this script might be for you.
The coding style from this example looks very different from the two others and some PHP developer would say this is not really OOP code. Right PHPMailer doesn’t have one file for each method and for the example above you need to upload only two files. I like the PHPMailer class because of all the information you get on the project’s website.
The three examples above should help you to make the decision which script will work the best for you. Check the links below for the documentation related to the snippets I have used in this artikel:
- Sending e-mails via SMTP with PHPmailer and Gmail
- jQuery Contact form for your website
- Integrate your MailChimp Newsletter Subscription