【问题标题】:PHP mail returns true (Message accepted for delivery), but the message is not sentPHP 邮件返回真(邮件接受传递),但邮件未发送
【发布时间】:2013-06-19 04:47:39
【问题描述】:

一个多月以来,我一直在尝试让 php 发送邮件。我正在从运行良好的 000webhost 转移到我朋友的服务器。

发送邮件的php代码为:

$subject = $u.", your infomation";  
$message = "Your password is ".$p;  
$from = "me@gmail.com";  
$headers = "From:" . $from;  
if(mail($e,$subject,$message,$headers))  
$_SESSI ON['message']="message sent";  
else $_SESSION['message']="error";

php.ini 中的 sendmail 路径为“/usr/sbin/sendmail -t -i”


等/主机:

000.000.000.000 inspiron-1000 inspiron-1000.
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

和mail.log:

Jun 9 22:05:07 inspiron-1000 sendmail[24552]: r5A357t5024552: from=www-data, size=144, class=0, nrcpts=1, msgid=<201306100305.r5A357t5024552@inspiron-1000.>, relay=www-data@localhost
Jun 9 22:05:07 inspiron-1000 sm-mta[24553]: r5A357A8024553: from=<www-data@inspiron-1000>, size=367, class=0, nrcpts=1, msgid=<201306100305.r5A357t5024552@inspiron-1000.>, proto=ESMTP, daemon=MTA-v4, relay=ip6-localhost [127.0.0.1]
Jun 9 22:05:08 inspiron-1000 sendmail[24552]: r5A357t5024552: to=user@gmail.com, ctladdr=www-data (33/33), delay=00:00:01, xdelay=00:00:01, mailer=relay, pri=30144, relay=[127.0.0.1] [127.0.0.1], dsn=2.0.0, stat=Sent (r5A357A8024553 Message accepted for delivery)

这是mailq: MSP 队列状态...

/var/spool/mqueue-client is empty
        Total requests: 0
MTA Queue status...
        /var/spool/mqueue (5 requests)
-----Q-ID----- --Size-- -----Q-Time----- ------------Sender/Recipient-----------
r5M3LmZV023863*      19 Fri Jun 21 22:21 <www-data@inspiron-1000>
                     <user@gmail.com>
r5M3HicX023780*      19 Fri Jun 21 22:17 <www-data@inspiron-1000>
                     <user@gmail.com>
r5M3BSDF023465       19 Fri Jun 21 22:11 <www-data@inspiron-1000>
                 (Deferred: Connection timed out with alt4.gmail-smtp-in.l.goo)
                     <user@gmail.com>
r5M36Tjx023175       19 Fri Jun 21 22:06 <www-data@inspiron-1000>
                 (Deferred: Connection timed out with alt4.gmail-smtp-in.l.goo)
                     <user@gmail.com>
r5M33YQf023137*      19 Fri Jun 21 22:03 <www-data@inspiron-1000>
                 (Deferred: Connection timed out with alt4.gmail-smtp-in.l.goo)
                     <user@gmail.com>
        Total requests: 5

【问题讨论】:

    标签: php email ubuntu-server


    【解决方案1】:

    这就是我修复它的方法: 安装phpmailer

    这里有教程how to send mail with PHP mailer

    这是我的代码:

    <?php
    require 'PHPMailer-master/class.phpmailer.php';
    
    function sendmail($to,$subject, $body) 
    { 
        return sendmailfrom($to,"myemail@gmail.com","from me", $subject, $body);
    }
    function sendmailfrom($to, $from, $from_name, $subject, $body) 
    {
        $mail = new PHPMailer();  // create a new object
        $mail->IsSMTP(); // enable SMTP
        $mail->SMTPDebug = 0;  // 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';//required for gmail
        $mail->Port = 465; 
        $mail->Username = 'myemail@gmail.com';//the email I want to send from  
        $mail->Password = 'mypassword';  //my password         
        $mail->SetFrom($from, $from_name);
        $mail->Subject = $subject;
        $mail->Body = $body;
        $mail->AddAddress($to);
        if(!$mail->Send()) return false;
        else return true;
    }
    ?>
    

    当我想发送邮件时,我通过include("filename.php") 包含此代码,然后运行sendmail($to,$subject, $body);

    【讨论】:

      【解决方案2】:

      这不是来自 mail.log 的全部信息。您会看到本地服务器确实接受了您的电子邮件,但没有提及尝试将该电子邮件发送到 GMail。您可以从命令行使用$ mailq 检查排队等待投递的邮件。不过,该日志文件中可能还有几行包含更多信息。

      【讨论】:

      • 如您所见,您尝试发送的消息已排队等待超时。这可能意味着防火墙阻止了您与 alt4.gmail-smtp-in.l.goo 的连接。再次检查这些日志,并检查 sendmail 是否配置为向外部发送(它可能已设置为仅用于内部)。你也可以试试connecting to that server through telnet,如果失败了,那就是防火​​墙/连接问题。
      • 所以问题肯定出在 sendmail 而不是主机上?
      • 好吧,您的 /etc/hosts 可能不应该将您的主机名指向 0.0.0.0,但我认为这不是问题所在。可能有很多事情,所以你应该检查我在上一条评论中提到的每一种可能性。
      • 当我使用 telnet localhost 25 时,我得到状态码 220,但它卡在 '500 5.5.1 Command unrecognized: ""' 上。我仍然无法发送邮件。
      • 收到 220 的事实表明 SMTP 未被阻止,您可以从服务器发送电子邮件。是时候查看您的邮件服务器设置了。询问您的朋友是如何配置的,并检查邮件服务器黑名单和 DNS 设置等内容。既然我们已经回答了为什么 PHP 声称要发送一封永远不会到达的电子邮件,那么您应该花几天时间了解一下电子邮件的复杂性。您可能会在没有更多帮助的情况下解决问题:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      • 1970-01-01
      • 2011-10-18
      • 2013-07-18
      • 2016-11-18
      • 1970-01-01
      相关资源
      最近更新 更多