- 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.
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.
$mail = new PHPMailer();
// 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 = "firstname.lastname@example.org";
// Gmail username
$mail->Password = "yourpassword";
// Gmail password
$mail->CharSet = 'windows-1250';
', 'Example.com Information');
$mail->AddBCC ( 'email@example.com
', 'Example.com Sales Dep.');
$mail->Subject = $subject;
$mail->ContentType = 'text/plain';
$mail->Body = $body_of_your_email;
// you may also use
$mail->AddAddress ('firstname.lastname@example.org', 'Recipients Name');
// you may also use this format
= "Mailer Error: " . $mail->ErrorInfo;
= "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.
This above example is to send plain text e-mail.
- Do not use
$mail->MsgHTML($body_of_your_email);instead use $mail->Body = $body_of_your_email;
- Do not use
$mail->AltBody = 'Your alternative message to HTML';
- Set $mail->IsHTML(false);
- Of course set $mail->IsHTML(true); or you don't have to set this variable at all, by default it is set to true.
- For e-mail body use only MsgHTML, for example:
);,do not use $mail->Body = $body_of_your_email;
You may use optional variables like:
- $mail->AddReplyTo("email@example.com","First Last");
- $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.
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.