Send Email using PHPMailer and Gmail


I have been using PHPMailer to send emails for some time now. PHPMailer is a fully featured email transfer class for PHP and its popularity has grown rapidly over the years. Recently I had to send e-mail messages using the GMail SMTP server for one of my clients.

Up to now I had only sent email messages using SMTP servers provided by the hosting company. The difference here is that I had to use SSL encryption to send emails. Using hosting company SMTP server has its drawbacks as there is a chance that your website IP address is already on a blacklist simply because someone else had abused the service before you.


  • Latest version of PHPMailer
  • A web host having PHP 5 / 6 enabled with Open SSL
  • GMail or Google Apps account

Sending Email

  • Check with your web hosting provider if port 465 (TCP out) is open, if not check with your hosting provider. Port 465 is needed to connect with the GMail SMT server.
  • Include PHPMailer class in your mailing script as follows
  • Add the following code to your mailing script
$mail  = new PHPMailer();

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

$mail->Host         = “”; // GMAIL SMTP server

$mail->Port         = 465;                   // set the SMTP port for the GMAIL server

$mail->SMTPDebug    = 0;                     // debugging: 0 = none, 1 = errors and messages,
                                             // 2 = messages only

$mail->SMTPAuth     = true;                  // enable SMTP authentication

$mail->SMTPSecure   = 'ssl';                 // secure transfer enabled and REQUIRED for GMail

$mail->Username     = ‘[email protected]’; // SMTP account username
$mail->Password     = ‘yourpassword’;        // SMTP account password

$mail->AddAddress('[email protected]'); // receiver mail address

$mail->SetFrom(‘your name’, ‘[email protected]’); // from address

$mail->Subject = ‘Test mail’;               // mail subject

$mail->MsgHTML(“Message Body”);             // HTML message body
    $msg = "Mail could not be sent. <br />Error Description : " . $mail->ErrorInfo;
}   else
    $msg = 'Email Message sent successfully!';

echo $msg;                        

As a final note please do check the GMail SMTP limitations mentioned here.

2 comments to Send Email using PHPMailer and Gmail

  • Dilshan Perera

    Wow this is great. This is one of the solutions that I have been looking for :). Thanks a lot for sharing & keep the good work going on well!

  • legal

    I have been exploring for a little for any high-quality articles or blog posts in this sort of house . Exploring in Yahoo I eventually stumbled upon this website. Reading this information So i am happy to exhibit that I’ve an incredibly good uncanny feeling I came upon exactly what I needed. I so much undoubtedly will make sure to do not forget this site and give it a look regularly.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>





This site uses Akismet to reduce spam. Learn how your comment data is processed.