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.
  1. The Mail class included in the Zend Framework (http://framework.zend.com/)
  2. Swift Mailer (http://swiftmailer.org/)
  3. 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

ini_set('include_path', '.:/path2directory/ZendFramework/library/');
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Mail');
Zend_Loader::loadClass('Zend_Mail_Transport_Smtp');
 
$config = array('auth' => 'login', 
              'username' => 'smtpUser',
              'password' => 'smtpPassword');
 
$transport = new Zend_Mail_Transport_Smtp('smtp.server.com', $config);
$mail = new Zend_Mail();
$at = $mail->createAttachment(file_get_contents('path/logo.png'));
$at->filename = 'logo.png';                                  
$mail->setBodyText('This is the text inside the mail send by Zend_Mail using SMTP transport.');
$mail->setFrom('you@mail.com', 'Your Name');
$mail->addTo('contact@mailservice.us', 'Your friend');
$mail->setSubject('Mail Subject');
$mail->send($transport);
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.

Swift Mailer

require_once 'Swift/lib/swift_required.php';
 
$transport = Swift_SmtpTransport::newInstance('smtp.server.com', 25)
  ->setUsername('smtpUser')
  ->setPassword('smtpPassword');
 
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('you@mail.com' => 'Your Name'))
  ->setTo(array('contact@mailservice.us' => 'Your friend'))
  ->setBody('This is the text of the mail send by Swift using SMTP transport.');
$attachment = Swift_Attachment::newInstance(file_get_contents('path/logo.png'), 'logo.png');  
$message->attach($attachment);
$numSent = $mailer->send($message);
printf("Sent %d messages\n", $numSent);
The snippet looks similar to the code from the Zend mail class, but you have to create 4 different objects:
  1. An object for the SMTP transport (Swift_SmtpTransport)
  2. The object that will send the message (Swift_Mailer)
  3. The message object for all mail parts (Swift_Message)
  4. 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.

PHPMailer

require_once 'PHPMailer5/class.phpmailer.php';
 
$mail = new PHPMailer();
$mail->IsSMTP(); 
$mail->Host = 'smtp.server.com';
$mail->Port = 25;
$mail->SMTPAuth = true;
$mail->Username = 'smtpUser';
$mail->Password = 'smtpPassword';
$mail->SetFrom('you@mail.com', 'Your Name');
$mail->AddAddress('contact@mailservice.us', 'Your friend');
$mail->Subject = 'PHPMailer Message';
$mail->Body = 'This e-mail is sent through PHPMailer.';
$mail->AddAttachment('path/logo.png', 'logo.png');
if(!$mail->Send()) {
  echo 'Mailer error: '.$mail->ErrorInfo;
} else {
  echo 'Message has been sent.';
}
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.