【问题标题】:php swiftmailer sending mail with smtp transport timeoutphp swiftmailer 使用 smtp 传输超时发送邮件
【发布时间】:2014-08-17 01:53:14
【问题描述】:

我正在尝试使用 swift mailer 通过 mandrill 应用程序发送邮件。这是我的代码:

$transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com',587);
$transport->setUsername($username);
$transport->setPassword($password);

// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);

// Create the message
$message = Swift_Message::newInstance()

    // Give the message a subject
    ->setSubject('New Order '.$reservationNumber)

    // Set the From address with an associative array
    ->setFrom(array('noreply@domain.com' => 'domain.com'))

    // Set the To addresses with an associative array
    ->setTo('test@domain.com')

    // Give it a body
    ->setBody($body,'text/html');

$mailer->send($message);

凭据是 100% 好的。我收到超时错误:无法与主机 smtp.mandrillapp.com 建立连接 [连接超时 #110]。

好像有什么东西阻塞了连接。也许这是服务器配置的问题?我们在 centos 服务器上使用 WHM 软件

【问题讨论】:

    标签: php email smtp centos swiftmailer


    【解决方案1】:

    我刚刚解决了完全相同的问题,但使用的是 smtp.gmail.com。即使用户名/密码等都是正确的,它也不起作用。

    在我的情况下,似乎当 PHP 尝试连接到 smtp.gmail.com 时,它会取回 IPv6 地址 - 但他们的服务器似乎没有在监听它,因为 Swiftmailer 会以与您得到的相同超时错误进行响应.

    但是当我交换它的 IPv4 地址时(通过 ping 得到),它连接并发送电子邮件就好了。

    所以找出 smtp.mandrillapp.com 的 IPv4 地址是什么,并尝试 那个 IP 代替代码中的主机名。现在连接并发送吗?它对我有用。

    IP 地址中的编码并不理想——因为他们可以随时更改它——但至少你会收到一些电子邮件

    【讨论】:

      【解决方案2】:

      当我增加超时时它实际上起作用了:

              $transport = Swift_SmtpTransport::newInstance('mail_server', 'mail_port', 'tls')
                ->setUsername('mail_user')
                ->setPassword('mail_pass')
                ->setTimeout(120)
              ;
      

      我尝试访问的服务器使用 IPv6,可能存在与此相关的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-10
        • 2016-05-22
        • 2015-05-25
        • 1970-01-01
        • 1970-01-01
        • 2018-09-10
        • 2017-05-07
        相关资源
        最近更新 更多