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

时间:2015-02-25 15:35:05

标签: php email

我正在尝试使用SMTP发送邮件的代码。我正在使用xampp服务器来运行php代码。我发送邮件从 neelabhsingh1986@gmail.com neelabhsingh1000@gmail.com 。我从this sitegithub获得了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";
?>

2 个答案:

答案 0 :(得分:1)

在我的设置中,我也有:

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

请注意,您可能需要在帐户中对此操作进行授权。你会收到一封来自gmail的电子邮件,上面有一个链接。

答案 1 :(得分:0)

这是我的帖子的答案。请参阅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帐户:已启用对安全性较低的应用的访问。当您单击此链接时,他们要求获得权限,如果您愿意,可以访问不太安全的应用程序。感谢@rjdown的帮助。

相关问题