PHP: Sending E-mail with Gmail SMTP and PHPMailer – Update 2018

Previous version: PHP: Sending E-mail with Gmail SMTP and 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.


You need for this PHPMailer code example a PHP5 enabled web host (I did tests on Linux and Windows), the port 587 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. Go to: and turn on Allow less secure apps
  3. gmail allow less secure apps on

  4. Go to gmail settings and enable IMAP/POP
  5. gmail settings

    gmail imap pop

  6. Download the lastest version of PHPMailer in here: or (I’m using the version 6.05)
  7. Check with your web hosting provider that port 587 (TCP out) is open, if not ask him to open that port
  8. Put code in test folder: ex: C:\AppServ\www\code\PHPMailer
  9. Check openssl enabled:
    open the PHP.ini file, then search for “openssl”. Go ahead and remove ‘;’ comment symbol from the start of the line.
  10. extension=php_openssl.dll

  11. Restart apache
  12. Use the code sample below: file: test.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 = "tls";                 // sets the prefix to the servier
$mail->Host       = "";      // sets GMAIL as the SMTP server
$mail->Port       = 587;                   // set the SMTP port for the GMAIL server
$mail->Username   = "";  // GMAIL username
$mail->Password   = "pass";            // GMAIL password

$mail->SetFrom('', 'Sender name');


$mail->Subject    = "subject";

$address = "";

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


Disadvantage: Sending limits

Update 2018

You must go to page: to enable Account access

gmail allow access

Update 2
Go to to allow your new device

1 Comment

Leave a Reply