【问题标题】:Swift Mailer ErrorSwift 邮件程序错误
【发布时间】:2011-02-05 09:54:48
【问题描述】:

我使用以下代码发送消息:

try
{   
    require_once "lib/Swift.php";
    require_once "lib/Swift/Connection/SMTP.php";
    $smtp =& new Swift_Connection_SMTP("mail.somedomain.net", 587);
    $smtp->setUsername("username");
    $smtp->setpassword("password");
    $swift =& new Swift($smtp);

    //Create the sender from the details we've been given
    $sender =& new Swift_Address($email, $name);
    $message =& new Swift_Message("message title");

    $message->attach(new Swift_Message_Part("Hello"));

    //Try sending the email
    $sent = $swift->send($message, "$myEmail", $sender);
    //Disconnect from SMTP, we're done
    $swift->disconnect();

    if($sent)
    {
        print 'sent';

    }
    else 
    {
        print 'not sent';
    }

}

catch (Exception $e) 
{
    echo"$e";
}

问题是它在我的本地服务器(我的 xampp 服务器)上运行良好,但在文件上传到真实服务器时无法运行。

它抛出这个错误:

'The SMTP connection failed to start [mail.somedomain.net:587]: fsockopen returned Error Number 110 and Error String 'Connection timed out''

请问我应该怎么做才能纠正这个错误。感谢阅读

【问题讨论】:

  • 谢谢,我的主机名是正确的,因为脚本仍在我的计算机(本地 apache 服务器)上时一切正常。请问有什么建议吗?

标签: php swiftmailer


【解决方案1】:
$smtp =& new Swift_Connection_SMTP("mail.somedomain.net", 587);

“587”是要连接的端口号吗?您有什么理由尝试使用它而不是普通的端口 25? 587(提交)端口通常用于本地用户发送邮件。一旦您在远程 Web 服务器上运行此脚本,它就不再是“本地”的,并且很可能已被防火墙关闭(或者邮件服务器没有在外部接口上侦听该端口)。

尝试切换到端口 25,看看是否有帮助。

更新:

连接被拒绝优于“连接超时”。这至少意味着初始数据包到达某个地方并被主动拒绝。超时意味着事情只是在途中的某个地方默默地丢弃了。

max_execution_time 只有在 php 脚本本身超过最大时间时才会发挥作用。如果是这种情况,您将不会收到 swiftmailer 错误,因为脚本会简单地终止。

您的网络服务器是否在运行 sendmail?将连接主机更改为“localhost”,看看是否有帮助。如果您只想发送电子邮件,那应该可以。您可能想要连接到远程 SMTP 服务器的唯一原因是正确设置 From: 标头,并且在接收端不会被标记为垃圾邮件。

【讨论】:

  • 感谢您的回答,我尝试了端口 25 并收到以下消息:'SMTP 连接无法启动 [mail.somedomain.net:25]:fsockopen 返回错误号 111 和错误字符串'连接拒绝'' 我认为问题可能出在服务器配置上,因为当我使用相同的 smtp 服务器域详细信息时,相同的脚本在我的本地 apache 服务器上运行良好。我运行了 phpinfo,我唯一能注意到的是 max_execution_time 在服务器上设置为 30,但在我的本地 apache 服务器上设置为 60。请有任何想法
【解决方案2】:

确保 smtp 服务器域有效。尝试 ping 它以确认响应。您也可以尝试跟踪路由以查看是否有任何开关返回缓慢的响应。

【讨论】:

  • 感谢您的回答,该域是有效的,并且当脚本文件仍在我的本地 apache 服务器上时它可以工作。我还尝试使用 ping mail.somedomain.com ping 服务器域,它运行良好。请我需要一些想法。感谢阅读和回答。
  • @Selom - 你的邮件提供商是谁?
猜你喜欢
  • 2019-02-02
  • 2012-02-15
  • 2011-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多