$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = ‘ssl’; // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com”;
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "email@gmail.com”;
$mail->Password = “password”;
$mail->SetFrom("example@gmail.com”);
$mail->Subject = “Test”;
$mail->Body = “hello”;
$mail->AddAddress("email@gmail.com”);
if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message has been sent"; }上面的代码已经过测试并为我工作。
可能是您需要
$mail->SMTPSecure = 'ssl';
另外,请确保您没有为该帐户启用两步验证,因为这也会引起问题。
更新
您可以尝试将$ mail-> SMTP更改为:
$mail->SMTPSecure = 'tls';
值得注意的是,某些SMTP服务器会阻止连接。某些SMTP服务器不支持
SSL(或
TLS)连接。



