【问题标题】:The email wont sent to gmaill on php [duplicate]电子邮件不会在 php 上发送到 gmail [重复]
【发布时间】:2018-12-18 07:13:42
【问题描述】:

我是 php 的初学者,所以我编写了用于测试的代码,该代码将使用 php 发送电子邮件。 我正在使用“PHPMailer-5.2.27”在 php 上使用 SMTP 发送电子邮件,但是当我运行代码时,电子邮件未发送。我不知道是什么问题。谁能帮帮我!

<?php

require 'phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer();
$mail->Host = "smtp.gmail.com";
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Username = "example@gmail.com";
$mail->Password = "xxxxxxxx";
$mail->SMTPSecure = "ssl";
$mail->Port = 465;
$mail->Subject = "test mail";
$mail->Body = "just a test";
$mail->setFrom('example@gmail.com','aaaa');
$mail->addAddress('example@gmail.com');

if ($mail->send())
    echo "mail is sent";
else
    echo "something wrong ";
?>

【问题讨论】:

  • 请设置$mail->SMTPDebug = true,用于调试错误。请使用 $mail->ErrorInfo 打印错误
  • 启用 SMTP 调试模式并检查您遇到的错误。
  • 好的,请稍等!
  • 就是这样:...................................... ..................................................... ............................. SMTP 连接()失败。 github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 错了

标签: php email smtp phpmailer


【解决方案1】:

假设您已允许您的 Gmail 帐户授权您发送电子邮件的服务器。

使用$mail-&gt;ErrorInfo; 来调试您的问题,这将阐明发送电子邮件的实际失败原因是什么,您可以参考我分享的以下代码。

<?php
require "PHPMailer/PHPMailerAutoload.php";
echo !extension_loaded('openssl')?"Not Available":"Available"; //check openssl is available or not
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 2; // 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 = "mygmailaccount@gmail.com";
$mail->Password = "**********";
$mail->SetFrom("example@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("toaddress@gmail.com");

if(!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo;
} else {
        echo "Message has been sent";
}
?>

【讨论】:

  • 这是过时的代码。 SMTPDebug = 1 不会告诉你什么;将其设置为2
  • @Synchro 感谢您的更正
猜你喜欢
  • 1970-01-01
  • 2014-05-23
  • 2022-07-18
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
  • 2014-10-30
  • 2014-05-30
  • 1970-01-01
相关资源
最近更新 更多