Swift Mailer 电子邮件发送问题

发布于 2024-08-25 23:37:23 字数 2603 浏览 8 评论 0 原文

我已经从他们的网站下载了 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('[email protected]' => 'John Doe'))
 ->setTo(array('[email protected]', '[email protected]' => 'A name'))
 ->setBody('Here is the message itself')
 ;

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

?>

一旦我运行页面,

      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 以及我的文件中的密码是正确的。

谢谢

i have downloaded Swift Mailer from their website and try to send simple email with following code

     <?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('[email protected]' => 'John Doe'))
 ->setTo(array('[email protected]', '[email protected]' => 'A name'))
 ->setBody('Here is the message itself')
 ;

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

?>

once i run the page it gives error

      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

if i remove the line

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

then page execute and no error message display, as soon as i add above line to send email, i got error.

my outgoing server, port and user id & passwords are correct in my file.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎夕旧梦 2024-09-01 23:37:23

它正在寻找服务器 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 服务器或将 send() 行包装在 try/catch 中以捕获异常要么将其记录在某处,要么忽略它

It's looking for the server smtp.domain.org but isn't able to resolve it.

If you look at the line the last step in the stack trade is calling, you can see it throwing an exception:

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 . ']'
    );
}

So you either need to input a valid smtp server or wrap the send() line in a try/catch to catch the exception and either log it somewhere or ignore it

等待我真够勒 2024-09-01 23:37:23

这些错误告诉您需要了解的一切:

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主机,然后重试。

The errors tell you everything you need to know:

getaddrinfo failed: No such host is known.

The specified SMTP server (smtp.domain.org) does not exist, so the mailer script can't connec to it to send the email. At least the domain.org domain DOES exist, so perhaps they've got the SMTP server named something else:

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.

Specify some other SMTP host that DOES exist and try again.

一袭水袖舞倾城 2024-09-01 23:37:23

请控制您使用的端口是否确实是您的邮件服务器使用的端口。我处理过类似的问题,最后发现我使用的是 yahoo 的端口 25,而不是 465。

Please control if the port you use is really the port used by your mail server. I have dealt with a similar problem and finally saw that I was using port 25 with yahoo instead of 465.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文