How to Send E-Mail Using PHPMailer and Gmail SMTP

In this tutorial, I will show you how you can use PHPMailer library and speed up the mail sending. To make it easier we will be using Gmail SMTP to deliver our mail.

Overview

To continue in this you will need to save the following files with given code in the document below

  1. PHP
  2. PHPMailer Library

Sendmail.php

This file contains the imports to PHPMailer library as well as all the configurations that are needed for mail server in order to deliver the emails efficiently. In this file, we set all the attributes that are needed to send a mail and we configure the Gmail SMTP server with the configuration as needed and when the code is executed, it will make use of the PHPMailer library to send the file.

<?php

require_once(‘PHPMailer/class.phpmailer.php’);

$email = new PHPMailer();
$email->CharSet = “utf-8”;
$email->isSMTP();
$email->SMTPAuth= true;
$email->Username = “your_gmail@gmail.com”;
$email->Password = “your_gmail_password”;
$email->SMTPSecure = “ssl”;
$email->Host = “smtp.gmail.com”;
$email->Port = “587”;

$email->setFrom(‘your_gmail@gmail.com’, ‘your name’);
$email->AddAddress(‘to_mail@mail.com’, ‘receivers name’);

$email->Subject  =  ‘PHPMailer Mailing API Test’;
$email->IsHTML(true);
$email->Body    = ‘Hi,
<br />
Email was generated using PHPMailer with Google SMTP
<br />
Welcome to PHPMailer 😀 :)’;

if($email->Send())
{
echo “Email Successfully sent”;
}
else
{
echo “Error in Sending Mail”.$email->ErrorInfo;
}

?>

 PHPMailer Library

This library contains some built-in functions that are used to deliver emails easily.

You need to make sure that you always use an actual server in order to try the code out because in localhost most of the time SendMail functions do not work.

You can download the completing working source code here,

I hope I was able to help you with setting up Send E-Mail function using PHPMailer and Gmail SMTP.

Let me know your experience with the above functions in the comment below.

Download the complete source code.

Grab the the free working demo Intantly!

You have Successfully Suscribed!

5 thoughts on “How to Send E-Mail Using PHPMailer and Gmail SMTP

    1. Hello sahil, Did you try in the local host ? Try on an actual server and let me know if you have the problem again.
      Thanks.

  1. This did not work for me either. After digging in the class, it appears you need to set the following

    $email->Mailer=’gmail’

    The following code works :
    require_once(‘PHPMailer/class.phpmailer.php’);

    $email = new PHPMailer();
    $email->CharSet = “utf-8”;
    $email->isSMTP();
    $email->SMTPAuth= true;
    $email->Username = “your_gmail@gmail.com”;
    $email->Password = “your_gmail_password”;
    $email->SMTPSecure = “ssl”;
    $email->Host = “smtp.gmail.com”;
    $email->Port = “587”;
    $email->Mailer = “gmail”;

    $email->setFrom(‘your_gmail@gmail.com’, ‘your name’);
    $email->AddAddress(‘to_mail@mail.com’, ‘receivers name’);

    $email->Subject = ‘PHPMailer Mailing API Test’;
    $email->IsHTML(true);
    $email->Body = ‘Hi,

    Email was generated using PHPMailer with Google SMTP

    Welcome to PHPMailer :)’;

    if($email->Send())
    {
    echo “Email Successfully sent”;
    }
    else
    {
    echo “Error in Sending Mail”.$email->ErrorInfo;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *