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 = smtp.gmail.com  //set smtp server name
smtp_port = 587 //port
sendmail_from =  your_email_adress@gmail.com // 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:
smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=tls
error_logfile=error.log
auth_username=your-gmail-username@gmail.com
auth_password=your-gmail-password

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

4. Download the PHP mailer library Scrips from below Link.
     https://github.com/PHPMailer/PHPMailer

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.

Mail_Script.php

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

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

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // 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('your_email_adress@gmail.com', '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. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

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

              https://myaccount.google.com/lesssecureapps


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