如何使用 php 发送邮件到使用 SMTP 的 gmail 帐户

How to send mail using php to gmail account using SMTP

我正在尝试 php 中的代码使用 SMTP.I 发送邮件,我正在使用 xampp 服务器到 运行 php 代码。我正在从 neelabhsingh1986@gmail.comneelabhsingh1000@gmail.com 发送邮件。我从 this site and github 获得了 php 代码。但我收到类似

的消息

SMTP 错误:无法验证。无法发送消息。
邮件程序错误:SMTP 错误:无法验证。

Php 发送邮件的代码

<?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";
?>

在我的设置中还有这个:

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

请注意,您可能需要在您的帐户上授权此操作。您会收到一封来自 gmail 的电子邮件,其中包含 link.

这是我 post 的答案。请参阅link。下载 PHPMailer_5.2.0.zip 并解压。我正在使用 xampp 服务器,并在 D:\xampp\htdocs\ 中创建了名为 phpMail 的文件夹。我将这两个文件 (class.phpmailer.phpclass.smtp.php ) 从 PHPMailer_5.2.0 复制到 phpMail。现在制作文件 sendMail.php 并粘贴以下代码。

<?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";
?>

编写代码后,您必须 运行 服务器。在我的例子中是 http://localhost/phpMail/sendMail.php。当您从浏览器 运行 此代码时,您将收到一封关于此应用程序的邮件,例如 Google 帐户:已启用对安全性较低的应用程序的访问权限 。当您单击此 link 时,他们会请求许可,如果您愿意,请访问安全性较低的应用程序,然后单击“是”。感谢@rjdown 的帮助。