简体   繁体   中英

How to send mail using php to gmail account using SMTP

I am trying code in php to send mail using SMTP.I am using xampp server to run php code. I am sending mail from neelabhsingh1986@gmail.com to neelabhsingh1000@gmail.com . I got the php code from this site and github . But I am getting message like

SMTP Error: Could not authenticate. Message could not be sent.
Mailer Error: SMTP Error: Could not authenticate.

Php code to send mail

<?php
require("D:xampp/htdocs/PHPMailer_5.2.0/class.PHPMailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->Host = "smtp.gmail.com";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "neelabhsingh1986@gmail.com";  // SMTP username
$mail->Password = "mypassword"; // SMTP password

$mail->From = "neelabhsingh1986@gmail.com";
$mail->FromName = "Neelabh Singh";

$mail->AddAddress("neelabhsingh1000@gmail.com");                  // name is optional


$mail->WordWrap = 50;                                 // set word wrap to 50 characters

$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. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
?>

In my setup I also have this:

$mail->SMTPSecure = "ssl";
$mail->Port = 465;

Note that you will probably need to authorise this action on your account. You'll get an email from gmail with a link.

This the answer for my post. Please see the link . Download PHPMailer_5.2.0.zip and unzip. I am using xampp server and I created folder in D:\\xampp\\htdocs\\ with name phpMail. I copied these two files( class.phpmailer.phpclass.smtp.php ) from PHPMailer_5.2.0 to phpMail . Now make file sendMail.php and paste following code.

<?php
require("class.PHPMailer.php");    
$mail = new PHPMailer();
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->Host = "smtp.gmail.com";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "yourEmailAddress@gmail.com";  // SMTP username, sender email address
$mail->Password = "yourGmailPassword"; // SMTP password

$mail->From = "yourEmailAddress@gmail.com";
$mail->FromName = "YourName";

$mail->AddAddress("Receiver@gmail.com");                  // Write the email of receiver. Who will get the mail.


$mail->WordWrap = 50;                                 // set word wrap to 50 characters
$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. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
?>

After writing the code your have to run server. In My case It was http://localhost/phpMail/sendMail.php . When You run this code from browser the you will get one mail regarding this app like Google Account: access for less secure apps has been enabled . When you click this link they asked for permission, Access for less secure apps click yes if you want. Thanks to @rjdown for help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM