【问题标题】:Sending Mail using PHP : SMTP ERROR使用 PHP 发送邮件:SMTP 错误
【发布时间】:2016-08-30 19:50:56
【问题描述】:

我正在尝试使用 php 发送邮件。但它给了我错误,

" SMTP 错误:无法连接到服务器:连接被拒绝 (111)
SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
“邮件程序错误:SMTP 连接()失败。https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting”
“

所以我搜索了很多以找到问题。我有一个解决方案,我需要更改 $mail->IsMail();来自 $mail->IsSMTP();

我做到了,邮件已发送... 但是当我检查我的邮件时,

我明白了, “此邮件可能不是由 sender@gmail.com 发送的”

作为一名开发人员,我理解电子邮件不应包含此类行或问题。

我想知道,如果Receiver在Email中显示这样的行可以吗?如果不是,我该怎么办?

我的意思是我应该对我的代码进行哪些更改。

这是我的 php 代码:

**

date_default_timezone_set('Etc/UTC');
include 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->IsSMTP();
// $mail->Mailer = "smtp";
$mail->SMTPDebug = 1;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "sender@gmail.com";
$mail->Password = 'senderPassword';
$mail->setFrom("sender@gmail.com", 'sender name');
$mail->addReplyTo('sender@gmail.com', '');
$mail->addAddress($receiver, '');
$mail->Subject = 'Welcome';
$mail->Body    = 'body';
$mail->AltBody = 'This is a plain-text message body';
if (!$mail->send())
{
    return "Mailer Error: " . $mail->ErrorInfo;
}
else
{
    return array('flag' => "1"); 
}

**

【问题讨论】:

  • 您为 tls 使用 465 端口,但 gmail 的 smtp tls 端口为 587。试试这个并检查您是否收到相同的消息。
  • 我已经试过了.. :( 为什么我使用 IsMail(); 而不是 IsSMTP(); 时发送邮件; ??
  • 我有点困惑这与 phpMyAdmin 有什么关系?
  • 与它无关。但我正在使用 PHPMailer 并使用 phpmyadmin 或 sql db.. 但问题仍未解决。我必须在应该使用 IsSMTP() 的地方使用 IsMail()。

标签: php smtp phpmailer sendmail


【解决方案1】:

这是用于邮寄目的的代码。尝试设置 SMTPDebug 模式 3 并检查输出。

$mail = new PHPMailer;

$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.office365.com';
$mail->SMTPAuth = true;
$mail->Username = 'xxxxxxxxxxxxxxx';
$mail->Password = 'xxxxxxxxxxxxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->From = 'xxxxxxxxxxxxxx';
$mail->FromName = 'xxxxxxxxxxxxxxxxx';
$mail->addAddress(xxxxxxxxxxxxxx);
$mail->addReplyTo('xxxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxx');

$mail->isHTML(true);

$mail->Subject = '';
$mail->Body    = "";
$mail->send();

【讨论】:

  • 连接:打开 smtp.gmail.com:587, timeout=300, options=array ()
    SMTP 错误:无法连接到服务器:连接被拒绝 (111)
    SMTP连接失败。 github.com/PHPMailer/PHPMailer/wiki/Troubleshooting<br> "Mailer 错误:SMTP connect() 失败。https:\/\/github.com\/PHPMailer\/PHPMailer\/wiki\/Troubleshooting"
  • 在这种情况下,请检查您用于发送邮件的电子邮件的 gmail 帐户设置。您需要允许发送邮件。
  • 我已经完成了。帐户设置正确。我告诉过你不,它只适用于 IsMail();我不明白为什么它不适用于 IsSMTP();
【解决方案2】:

isMail 和isSMTP 使用两种不同的发送机制。 isMail 通过 PHP mail() 函数提交消息,该函数通过 sendmail 二进制文件将邮件传递到本地邮件服务器。然后,此本地邮件服务器尝试将邮件传递给其最终收件人。此本地服务器可能会接受稍后被拒绝的消息,而这对于您的脚本来说已经太迟了。

与isMail:

script -> local mail server -> gmail

与isSMTP:

script -> gmail

使用isMail,您无需进行身份验证(通常允许本地主机中继),消息会从您的服务器发送到 gmail。使用isSMTP,您的消息是从 gmail 到 gmail 发送的,并且确实需要身份验证。

直接通过 gmail 发送时,您需要使用 gmail 进行身份验证,这有其自身的一系列问题(这将是您的脚本无法正常工作的原因),在 SO 上的 PHPMailer 文档、示例和问题中进行了全面介绍。

通过您的服务器发送时,您是说您是从 gmail 用户发送的,但它是由您的服务器发送的,而不是由 gmail 的 SPF 记录中列出的服务器发送的。这是伪造的,这就是为什么您会看到“此消息可能不是由...发送”消息的原因。如果您从自己域中的地址发送,则不会这样说。

解决方案是修复您的 gmail 身份验证并直接通过 gmail 发送。您的代码基于the gmail example provided with PHPMailer,而不是您正在使用的旧的过时代码和read the docs。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-16
    • 2018-09-10
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多