Home‎ > ‎PHP‎ > ‎

Send e-mail from PHP with SMTP, SSL, TLS support

Method: Using PHPMailer library

  • use to send e-mail with SMTP over SSL/TLS 
  • use to send using your Google Apps Account or Gmail
  • use to send using your Amazon AWS SES account
Download the PHPMailer library from here, https://github.com/Synchro/PHPMailer. It is free, easy to use library that supports sending e-mail using secure connection directly form SMTP. If you run you own virtual or dedicated server and you are not into setting up mailserver application, this is the right PHP library for you.

This library works perfectly with plaint text and HTML or you may create HTML mail and insert an plain text alternative to support older mail clients with no HTML support.

Example, if you have your website on http://www.example.com/ and your document root is /var/www/example.com, upload the phpmailer directory up to /var/www/example.com/scripts/phpmailer. 

TIP
Do not forget to escape your $_POST and $_GET variables, always put e-mailing scripts into if statement or at least use some protection to prevent "hackers" from using your page with script mailer as a e-mail spam gateway. 

require_once('scripts/phpmailer/class.phpmailer.php');

$mail = new PHPMailer();

$mail->IsSMTP();                       // telling the class to use SMTP

$mail->SMTPDebug = 0;                  
// 0 = no output, 1 = errors and messages, 2 = messages only.

$mail->SMTPAuth = true;                // enable SMTP authentication
$mail->SMTPSecure = "tls";              // sets the prefix to the servier
$mail->Host = "smtp.gmail.com";        // sets Gmail as the SMTP server
$mail->Port = 587;                     // set the SMTP port for the GMAIL

$mail->Username = "info@example.com";  // Gmail username
$mail->Password = "yourpassword";      // Gmail password

$mail->CharSet = 'windows-1250';
$mail->SetFrom ('info@example.com', 'Example.com Information');
$mail->AddBCC ( 'sales@example.com', 'Example.com Sales Dep.');
$mail->Subject = $subject;
$mail->ContentType = 'text/plain';
$mail->IsHTML(false);

$mail->Body = $body_of_your_email; 
// you may also use $mail->Body = file_get_contents('your_mail_template.html');

$mail->AddAddress ('your.recipient@domain.com', 'Recipients Name');     
// you may also use this format $mail->AddAddress ($recipient);

if(!$mail->Send())
{
        $error_message = "Mailer Error: " . $mail->ErrorInfo;
} else 
{
        $error_message = "Successfully sent!";
}

// You may delete or alter these last lines reporting error messages, but beware, that if you delete the $mail->Send() part, the e-mail will not be sent, because that is the part of this code, that actually sends the e-mail. 

HTML vs. PLAIN TEXT

This above example is to send plain text e-mail. 

To send plain text, here are few tips:

  1. Do not use $mail->MsgHTML($body_of_your_email); instead use $mail->Body = $body_of_your_email; 
  2. Do not use $mail->AltBody = 'Your alternative message to HTML';
  3. Set $mail->IsHTML(false);

To send HTML e-mail, here are few tips:

  1. Of course set $mail->IsHTML(true); or you don't have to set this variable at all, by default it is set to true.
  2. For e-mail body use only MsgHTML, for example: $mail->MsgHTML(file_get_contents('your_mail_template.html'));, do not use $mail->Body = $body_of_your_email; 

Other TIPs

You may use optional variables like:
  • $mail->AddReplyTo("reply-to@example.com","First Last");
  • $mail->AddAttachment("path/to/a/file/attachment.gif");
  • $mail->IsSendmail(); // telling the class to use SendMail transport
  • If not set IsSendmail() and IsSMTP, it uses standard PHP mail();
  • If sending bulk mail, for example many e-mails from a database, use $mail->SMTPKeepAlive = true; for keeping SMTP connection alive, to view a example PHP code, see test_db_smtp_basic.php in examples directory of PHPmail library.
  • To set language, use $mail->SetLanguage('de', 'phpmailer/language/'); - for list of available languages, look into /language folder of the PHPMailer library.

Why use PHPMailer

I decided to use PHPMailer and not to install mailserver like Postfix, because of security issue, SPAM fighting and also this is a time saver, because I use Google Apps, I set up my Google Apps account to automatically sign e-mails with DKIM, I setup SPF DNS record and the best of all, as I host multiple sites as Virtual Hosts on my web server, I also want to sometimes use different e-mail accounts on different parts of my website, for example part of my website uses Amazon AWS SES, part of my website uses my company SMTP server and part uses Google Apps e-mail accounts, all set in PHP code, nothing to setup on server side.