php swiftmailer用smtp传输超时发送邮件

时间:2014-06-26 09:14:34

标签: php email smtp centos swiftmailer

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

$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软件

3 个答案:

答案 0 :(得分:2)

我刚刚用完全相同的问题做过战斗,但是用smtp.gmail.com。它只是不起作用,即使用户名/密码等都是正确的。

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

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

因此,找出smtp.mandrillapp.com的IPv4地址是什么,并尝试那个 IP代替代码中的主机名。现在连接和发送吗?它确实适合我。

它在IP地址中不是理想的编码 - 假设他们可以随时更改它 - 但至少你会收到一些电子邮件发送

答案 1 :(得分:1)

当我增加超时时,它实际上有效:

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

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

答案 2 :(得分:0)

您可以在laravel框架中的以下文件中更改超时参数。 供应商/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php

添加以下带有$ timeout变量的注释,并以秒为单位进行超时。

#if ...