A fast way to send SMTP HTML email by using php (PHPMailer)

  • Posted on: 30 November 2016
  • By: jmu

Yesterday, I worked on an email function in php. Since I would like to avoid spam filter, PHPMailer is the greatest choice for me to use. I also tried some other ways and will share in other articles if I have time. Although it is easy to set up and use, I still spent a lot of time on sending different emails to multiple receivers because I don't even know it can only send a same email to one same receiver by default. I will write another article to explain how to send different templates to multiple receivers in one php file.

PHPMailer Introduction:

The PHP mail() function usually sends via a local mail server, typically fronted by a sendmail binary on Linux, BSD and OS X platforms, however, Windows usually doesn't include a local mail server; PHPMailer's integrated SMTP implementation allows email sending on Windows platforms without a local mail server.

Benefits I was focusing on:

  1. Easy set up.
  2. Avoid spam filter by integrating SMTP setting
  3. Able to send HTML email
  4. Fast processing -- much much faster than standard php mail()
  5. Alow to add attachments
  6. Support different language -- "Error messages in 47 languages"

Time to start:

I saw there are a lot of tutorials mentioned two files: class.phpmailer.php and class.smtp.php. Right now, they are all linked by PHPMailerAutoload.php. Therefore, you can just simply contain only one  PHPMailerAutoload.php file instead of two.

Here are the steps if you don't have Composer/Packagist:

  1. Create a directory in your server, I used "/home/usr/emailTest" as my example folder path.
  2. Download phpMailer from github
  3. Extract "PHPMailer-master" to the folder you created, which is "/home/usr/emailTest" here
  4. Rename "PHPMailer-master" to "PHPMailer"
  5. Create a file which contain your email information under "/home/usr/emailTest" folder
  6. One last thing: (import PHPMailer in your code) 
    require 'PHPMailer/PHPMailerAutoload.php'

Here is a sample which from PHPMailer:

require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose debug output
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('from@example.com', 'Mailer');
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body in bold!';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

Here is a simple tutorial. More information is provided on PHPMailer Github Page. They provide more sample code for matching different situations. 

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.