SHARE

New version: PHP: Sending E-mail with Gmail SMTP and PHPMailer – Update 2015

Why use PHPMailer

There are 2 reasons to use PHPMailer:

1. Use email() built-in function, you’ll send email in spam box. It’s detrimental to your ad campaign.
2. If you use Windows server to send email, it’s difficult to configure SMTP server to use email() funtion.

Why using GMail for sending mail messages?

First of all it’s FREE! Sure most website owners can use their own SMTP server for sending email messages from their website, but it makes sense even than to use GMail for sending mail. The chance is big that your websites IP address is on a blacklist if your site is on hosted by a shared web hosting provider. If not or you host your site on your own server, there is always a risk that your IP address get blacklisted. Because of some limitations, the SMTP server from Google is a good choice applications with less than 500 recipients a day, check this information from the Google help pages.

Requirements

You need for this PHPMailer code example a PHP5 enabled web host (I did tests on Linux and Windows), the port 465 need to be open and of course you need a GMail account.

PHPMailer tutorial for GMail

  1. If you don’t have one, register a GMail account.
  2. Download the lastest version of PHPMailer (I’m using the version 5.02)
  3. Check with your web hosting provider that port 465 (TCP out) is open, if not ask him to open that port

D. Put code in test folder: ex: C:\AppServ\www\code\PHPMailer

E. Check openssl enabled:
open the PHP.ini file, then search for “openssl”. Go ahead and remove ‘;’ comment symbol from the start of the line.

extension=php_openssl.dll

Restart apache

F. Use the code sample below: file: test.php

<?php
 
error_reporting(E_STRICT);

date_default_timezone_set('Asia/Saigon');

require_once('PHPMailer/class.phpmailer.php');
 
$mail             = new PHPMailer();
$body             = 'Content';
 
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug  = 0;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "account@gmail.com";  // GMAIL username
$mail->Password   = "pass";            // GMAIL password

$mail->SetFrom('account@gmail.com', 'Sender name');

$mail->AddReplyTo("to@gmail.com");

$mail->Subject    = "subject";
 
$mail->MsgHTML($body);

$address = "to@gmail.com";
$mail->AddAddress($address);
 

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

?>

Disadvantage: Sending limits

Recent search terms:

  • send emails from PHPMailer using proxies IP addresses