无法使用gmail帐户使用php邮件发送电子邮件

时间:2016-01-27 11:37:06

标签: php phpmailer

在这里,我无法使用我的Gmail帐户向PHPMailer发送电子邮件。 示例代码:

<?php
require_once('phpmailerclass/class.phpmailer.php');

$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Mailer = 'smtp';
$mail->SMTPAuth = true;
$mail->Host = 'smtp.gmail.com'; 
$mail->Port = 568;
$mail->SMTPSecure = 'ssl';

$mail->Username = "myEmail@gmail.com";
$mail->Password = "myPassword";

$mail->IsHTML(true); // if you are going to send HTML formatted emails

$mail->From = "ramalingam.p@pickzy.com";
$mail->FromName = "Ramalingam";

$mail->addAddress("yoursramalingam@gmail.com","Luu Van Minh");

$mail->Subject = "Testing PHPMailer with localhost";
$mail->Body = "Hi,<br /><br />This system is working perfectly.";

if(!$mail->Send())
{
     echo "Message was not sent <br />PHPMailer Error: " . $mail->ErrorInfo;
}   
else
{
    echo "Message has been sent";
}      
?>

响应错误:

Message was not sent

PHPMailer Error: The following From address failed: myEmail@gmail.com

我也在gmail设置中启用“不太安全”。 但它会超越错误。指定我的VALID Gmail来自地址。 请纠正我错在哪里。

1 个答案:

答案 0 :(得分:6)

如果您使用ssl,则将端口587更改为465

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

https://support.google.com/a/answer/176600?hl=en