How to send email from localhost XAMPP server's gmail account to email address using PHPMailer

Trying to send email from localhost XAMPP server using gmail account to specified email address. Follow the following steps.

1. Download PHPmailer and extract it to applications home directory e.g.C:\xampp\htdocs\MyApp.

2. Copy the following program and save it with mail.php
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();                                      // Mailer uses SMTP to send mail
$mail->Host='smtp.gmail.com';                         // Host  
$mail->SMTPAuth = true;                               // Allowing SMTP authentication
$mail->Username = 'test@gmail.com';                   // SMTP username
$mail->Password = 'Test123!';                         // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption
$mail->Port = 587;                                    // TCP port to connect to
$mail->setFrom('test@gmail.com');                     // From address
$mail->addAddress('test2@gmail.net');                 // Add a recipient
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Mail Subject';
$mail->Body    = 'Body of email';
if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
This PHP script uses gmail's SMTP host to send mail with SMTP authentication. This SMTP authentication needs your gmail account's username and password. For secure transmission of mail use TLS or SSL. For TLS use 587 as port number and for SSL use 465. Then create message by setting sender's, receiver's email id along with subject and message body.

3. Now set "less secure app" to sender's email account so that account can send email to other email ids. For this first login in gmail account and then search "less secure app" in google. After clicking on less secure app link in search result just on "Allow less secure app" option

4. But if still not able to send mail then download this script and paste in extracted folder of phpmailer for allowing SMTP server to send mail.

Note : For sending e-mail make sure that network is proxy free.


EmoticonEmoticon