[英]Swift Mailer email sending issue
我從他們的網站下載了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);
?>
一旦我運行頁面,它就會出錯
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和密碼在我的文件中是正確的。
謝謝
它正在尋找服務器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()行以捕獲異常並將其記錄到某處或忽略它
錯誤告訴您需要知道的一切:
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主機,然后重試。
請控制您使用的端口是否真的是您的郵件服務器使用的端口。 我已經處理了類似的問題,最后看到我使用的是端口25與雅虎而不是465。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.