我刚刚开始学习如何使用SwiftMailer,我无法从本地主机发送简单的测试消息.下面是我尝试使用的代码.
//Pass it as a parameter when you create the message
$message = Swift_Message::newInstance();
$message->setSubject('My subject');
$message->setFrom(array('noreply@domain.com' => 'No Reply'));
$message->setTo(array('myemail@domain.com' => 'My Name'));
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 25);
//Supposed to allow local domain sending to work from what I read
$transport->setLocalDomain('[127.0.0.1]');
$mailer = Swift_Mailer::newInstance($transport);
//Send the message
$result = $mailer->send($message);
这是我的错误消息的一部分,
Warning: fsockopen() [<a href='function.fsockopen'>function.fsockopen</a>]:PHP_network_getaddresses: getaddrinfo Failed: Name or service not kNown in /path/Swift/Transport/StreamBuffer.PHP
更新
我用gmail工作了.我将Swift_SmtpTransport行更改为以下内容,
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')->setUsername('username')->setPassword('password');
解决方法:
localhost是当前机器的别名(在这种情况下,PHP运行的机器).如果你真的想用localhost发送邮件,你可以这么说:
$transport = Swift_SmtpTransport::newInstance('localhost', 25);