Sunday, August 6, 2017

Send Email Using PHP Mailer Script via. Gmail SMTP Server

Email is a method of exchanging information between people over communications networks. Today we are going to learn how we can send email using php script via. Gmail SMTP Server. SMTP is stands for Simple Mail Transfer Protocol. It is a basic standard for email communications. 

Here, we are using PHP mailer library script for email communications. Follow the below steps to configure the email application scripts in Xampp Application.

1. Edit the php.ini file in the php\ subdirectory of your XAMPP installation directory (usually, C:\xampp). Within this file, find the [mail function] section and replace it with the following directives:
[mail function]

SMTP =  //set smtp server name
smtp_port = 587 //port
sendmail_from = // set from email address
sendmail_path ="\"C:\xampp\sendmail\sendmail.exe\" -t"

2.Edit the sendmail.ini file in the sendmail\ subdirectory of your XAMPP installation directory. Within this file, find the [sendmail] section and replace it with the following directives:

3.Restart the Apache server using the XAMPP control panel.

4. Download the PHP mailer library Scrips from below Link.

5.Place the php mailer script to localhost directory, where we placed php mailer libaray after download and run the below php script to send email to specific users.

You need to set few mandatory details, in below script to make email communication.


require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 1;                               // Enable verbose debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = '';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               
$mail->Username = 'your_gmail_email_adress';                 // set gmail email id
$mail->Password = 'Gmail_email_Password';                           // gmail email password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->setFrom('', 'Mailer');
$mail->addAddress('friend_one_email_Addess', 'Joe User');     // Add a recipient
$mail->addAddress('friend_two_email_Addess');               // Name is optional
$mail->addReplyTo('friend_one_email_Addess', 'Information');

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';


Apart from above, you have to make few changes in google account. Lets complete the below steps, otherwise you would not able to send mail and scripts will not work. It will throw following error message.

Message could not be sent.Mailer Error: SMTP connect() failed.

Step -1 : Login to the google account.
Step -2 : Visit the below link and turn "ON" the Less secure apps indicator by allowing.


Finally we have completed the all the mandatory steps and scripts are ready to use. Please do comments if you have any queries.

No comments:

Post a Comment