Swift Mailer电子邮件发送问题

时间:2010-03-22 19:31:56

标签: php email swiftmailer

我已经从他们的网站下载了Swift Mailer,并尝试使用以下代码发送简单的电子邮件

     <?php
     require_once 'lib/swift_required.php';

    $transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
    ->setUsername('your username')
     ->setPassword('your password')
      ;


    $mailer = Swift_Mailer::newInstance($transport);

  //Create a message
  $message = Swift_Message::newInstance('Wonderful Subject')
  ->setFrom(array('john@doe.com' => 'John Doe'))
 ->setTo(array('receiver@domain.org', 'other@domain.org' => 'A name'))
 ->setBody('Here is the message itself')
 ;

 //Send the message
 $result = $mailer->send($message);

&GT;

一旦我运行页面就会出错

      Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in E:\web_sites\swift_mail\lib\classes\Swift\Transport\StreamBuffer.php  on line 233

    Warning: fsockopen() [function.fsockopen]: unable to connect to smtp.anyhost.com:25 (php_network_getaddresses: getaddrinfo failed: No such host is known. ) in E:\web_sites\swift_mail\lib\classes\Swift\Transport\StreamBuffer.php on line 233

   Fatal error: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host smtp.domain.com [php_network_getaddresses: getaddrinfo failed: No such host is known. #0]' in E:\web_sites\swift_mail\lib\classes\Swift\Transport\StreamBuffer.php:235 Stack trace: #0 E:\web_sites\swift_mail\lib\classes\Swift\Transport\StreamBuffer.php(70): Swift_Transport_StreamBuffer->_establishSocketConnection() #1 E:\web_sites\swift_mail\lib\classes\Swift\Transport\AbstractSmtpTransport.php(101): Swift_Transport_StreamBuffer->initialize(Array) #2 E:\web_sites\swift_mail\lib\classes\Swift\Mailer.php(74): Swift_Transport_AbstractSmtpTransport->start() #3 E:\web_sites\swift_mail\test.php(33): Swift_Mailer->send(Object(Swift_Message)) #4 {main} thrown in E:\web_sites\swift_mail\lib\classes\Swift\Transport\StreamBuffer.php on line 235

如果我删除该行

  $result = $mailer->send($message);

然后页面执行并且没有错误信息显示,只要我添加上面的行发送电子邮件,我就会收到错误。

我的传出服务器,端口和用户ID&amp;密码在我的文件中是正确的。

由于

3 个答案:

答案 0 :(得分:4)

它正在寻找服务器smtp.domain.org但无法解决它。

如果你看一下堆栈交易中最后一步的行,你可以看到它抛出异常:

if (!$this->_stream = fsockopen($host, $this->_params['port'], $errno, $errstr, $timeout))
{
  throw new Swift_TransportException(
    'Connection could not be established with host ' . $this->_params['host'] .
    ' [' . $errstr . ' #' . $errno . ']'
    );
}

所以你需要输入一个有效的smtp服务器或者在try / catch中包装send()行以捕获异常并将其记录到某处或忽略它

答案 1 :(得分:3)

错误告诉您需要知道的一切:

getaddrinfo failed: No such host is known.

指定的SMTP服务器(smtp.domain.org)不存在,因此邮件程序脚本无法连接到它以发送电子邮件。至少domain.org域名存在,所以也许他们已经将SMTP服务器命名为其他东西:

marc@panic:~$ host -t soa domain.org
domain.org has SOA record ns.domain.org. sales.domain.org. 1267596439 10800 3600 604800 3600
marc@panic:~$ host -t mx domain.org
domain.org mail is handled by 10 mail.domain.org.
marc@panic:~$ host domain.org
domain.org has address 208.109.97.130
domain.org mail is handled by 10 mail.domain.org.

指定其他存在的其他SMTP主机,然后重试。

答案 2 :(得分:0)

请控制您使用的端口是否真的是您的邮件服务器使用的端口。我已经处理了类似的问题,最后看到我使用的是端口25和雅虎而不是465。