【问题标题】:PHPMailer unable to send mail - The following From address failedPHPMailer 无法发送邮件 - 以下发件人地址失败
【发布时间】:2017-10-25 21:22:32
【问题描述】:

我在发送邮件时收到此错误。

Message could not be sent.Mailer Error: The following From address failed: sample@mydomain.com : MAIL FROM command failed,Temporary system failure. Please try again later. ,451,4.3.0SMTP server error: MAIL FROM command failed Detail: Temporary system failure. Please try again later. SMTP code: 451 Additional SMTP info: 4.3.0SMTP server error: MAIL FROM command failed Detail: Temporary system failure. Please try again later. SMTP code: 451 Additional SMTP info: 4.3.0

这是我的代码:

require 'phpmailer/PHPMailerAutoload.php';

$html = "<b>Hello world!</b>";
$email = "recipient@example.com";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->setFrom('sample@mydomain.com');
$mail->addAddress($email);
$mail->isHTML(true);

$mail->Subject = 'Sample Subject';
$mail->Body = $html;

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Email sent';
}

我还尝试删除 $mail-&gt;isSMTP(); 并收到回显消息 Email sent 问题是当我检查收件箱和垃圾邮件文件夹时,因为我将个人电子邮件设置为电子邮件的收件人,所以电子邮件是不在那里。这是服务器问题吗?

我还检查了/var/log/mail.err,我可以看到一个错误日志,上面写着collect: Cannot write ./dfv4PAGY73013302 (bfcommit, uid=0, gid=119): No such file or directory - 那是什么?

我已经头疼了 2 天了,但我不知道问题出在哪里。我在我的服务器中安装了一个新的 LAMP 堆栈,还配置了 postfix(只是这么说,可能会帮助您了解)。

这里似乎有什么问题?

您的帮助将不胜感激!谢谢!

【问题讨论】:

  • 嗯...也可能是您的 SMTP 服务器配置不正确。也许它无法在 spool 目录中写入文件...
  • @axiac 如何在 spool 目录中写入文件?抱歉这个愚蠢的问题:\
  • SMTP 服务器将其处理的电子邮件内容保存到工作目录中。您的似乎无法在该目录中写入文件,因为它不存在。此目录的确切路径在 SMTP 服务器的配置中指定。我从未配置过postfix :-(

标签: php phpmailer sendmail ubuntu-16.04


【解决方案1】:

您的代码很好。它有效,证明是您从 SMTP 服务器收到错误消息。问题在于它处理的数据。

正确配置的 SMTP 服务器不接受中继电子邮件。 From: 地址或To:Cc:Bcc: 中的至少一个地址必须由其托管,才能接受电子邮件进行处理。

作为From: 使用由您用于发送的 SMTP 服务器托管的真实电子邮件地址。例如,如果您使用 smtp.gmail.com,那么您必须使用您的 Gmail 地址作为 From:(在这种情况下还要使用 SMTP 身份验证)。

您的代码未指定 SMTP 服务器;在这种情况下,PHPMailer 使用localhost,它很可能将电子邮件中继到您的 ISP(或者如果此代码在公司的 Intranet 中运行,则为您的公司)的 SMTP 服务器。使用您的 ISP 提供的电子邮件地址(如果代码适用于您的工作,则使用您的工作电子邮件地址)作为 From:,它应该可以工作。

【讨论】:

  • 感谢您的回答。实际上,当我因为不想设置 smtp 而尝试删除 $mail-&gt;isSMTP(); 时,我收到消息“已发送电子邮件”,但是当我检查我的邮件收件箱和垃圾邮件文件夹时,电子邮件不存在。这是服务器问题吗?再次感谢。
  • 如果您删除$mail-&gt;isSMTP(),PHPMailer 默认为$mail-&gt;isMail(),这意味着它使用PHP 函数mail() 发送电子邮件。默认情况下,PHP 配置为使用本地 sendmail 程序(在 macOS 和 Linux 上)或 Windows 上的 localhost SMTP 服务器。默认配置可能会以多种方式失败,并且使其工作更加困难(因为它涉及系统配置,在某些情况下,您无法修改)。在 PHP 代码中使用 SMTP 服务器(如您所做的那样)更容易。但是,错误消息清楚地表明问题在于您使用的电子邮件地址为From:
  • 具有讽刺意味的是,不调用 isSMTP 将与您想要的相反。 mail() 要求您配置本地邮件服务器; isSMTP 没有。在不指定Host 的情况下使用 SMTP 将尝试使用 localhost,在您的情况下,您有一个本地服务器正在运行,但它没有配置,因此它给您的错误消息。通过不同的服务器发送或修复您的本地邮件服务器。
【解决方案2】:

更新您的 PHPMailer。通过 TLS SMTP 连接时,旧版本的 PHPMailer 似乎不支持自签名证书。

我花了两天时间试图解决这个问题,最后我更新了 PHPMailer,问题就消失了!

我的电子邮件服务器正在 Debian 9 上运行 iRedMail 包。

下载最新的 PHPmailer 压缩包并解压到您当前文件夹中的 PHPMailer 文件夹并执行以下操作:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
require 'PHPMailer/src/Exception.php';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPOptions = ['ssl'=> ['allow_self_signed' => true]];
$mail->SMTPSecure = 'tls';
//etc etc......

【讨论】:

    【解决方案3】:

    在 php 中尝试使用 ssl 套接字(以 tls/ssl 为例):

    // Show errors
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);
    // Your hostname
    $helloHostname = "localhost";
    
    // Emails
    $toEmail = "to@email.com";
    $fromEmail = "from@email.com";
    
    // Set mx hostname from dns for recipient !!!
    $hostnameMX = "mxhost.boo.xx";    
    
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
    stream_context_set_option($ctx, 'ssl', 'verify_peer_name', false);
    try{
        // echo $socket = stream_socket_client('ssl://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
        echo $socket = stream_socket_client('tcp://'.$hostnameMX.':25', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
        if (!$socket) {
            print "Failed to connect $err $errstr\n";
            return;
        }else{
            // Http
            // fwrite($socket, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
            // Smtp
            echo fread($socket,8192);
            echo fwrite($socket, "EHLO ".$helloHostname."\r\n");
            echo fread($socket,8192);
    
            // Start tls connection
            echo fwrite($socket, "STARTTLS\r\n");
            echo fread($socket,8192);
    
            echo stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
    
            // Send ehlo
            echo fwrite($socket, "EHLO ".$helloHostname."\r\n");
            echo fread($socket,8192);
    
            echo fwrite($socket, "MAIL FROM: <".$fromEmail.">\r\n");
            echo fread($socket,8192);
    
            echo fwrite($socket, "rcpt to: <".$toEmail.">\r\n");
            echo fread($socket,8192);
    
            echo fwrite($socket, "DATA\n");
            echo fread($socket,8192);
    
            echo fwrite($socket, "Date: ".time()."\r\nTo: <".$toEmail.">\r\nFrom:<".$fromEmail.">\r\nSubject:Hello from php socket tls\r\n.\r\n");
            echo fread($socket,8192);
    
            echo fwrite($socket, "quit \n");
            echo fread($socket,8192);
    
            /* Turn off encryption for the rest */
            // stream_socket_enable_crypto($fp, false);
    
            fclose($socket);
        }
    }catch(Exception $e){
        echo $e;
    }
    

    如果您需要从 dns 获取收件人电子邮件域的主机名:

    function getMX($hostname = "boo.xx", $show = 0){
        if(dns_get_mx($hostname, $mxhosts, $weights)) {
            $i = 0;
            $mxList = NULL;
            foreach($mxhosts as $key => $host) {
                if($show == 1) echo "Hostname: $host (Weight: {$weights[$key]}) <br>";
                $ip = gethostbyname($host);
                if($show == 1) echo "IP " . $ip . "\n<br>";
                if($show == 1) echo "IP " . gethostbyaddr($ip) . "\n<br>";
                $mxList[$i]['host'] = $host;
                $mxList[$i]['ip'] = $ip;
                $mxList[$i]['weight'] = $weights[$key];
                $i++;
            }
            return $mxList;
        } else {
            echo "Could not find any MX records for $hostname\n";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      • 2023-03-20
      相关资源
      最近更新 更多