2010-12-19 54 views
3

我刚开始学习如何使用SwiftMailer,我无法从本地主机发送简单的测试消息。以下是我正在尝试使用的代码。PHP SwiftMailer Localhost测试设置

//Pass it as a parameter when you create the message 
$message = Swift_Message::newInstance(); 
$message->setSubject('My subject'); 
$message->setFrom(array('[email protected]' => 'No Reply')); 
$message->setTo(array('[email protected]' => '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'); 
+0

这条线也适用于我使用Gmail帐户 – 2014-11-21 11:48:20

回答

6

localhost为当前机器的别名(在这种情况下,机器PHP运行在)。如果你真的想与你本地主机发送邮件这样说:

$transport = Swift_SmtpTransport::newInstance('localhost', 25); 

...但你还需要安装和配置自己的邮件服务器。如果你不知道这是什么,我建议你使用你的邮件提供商的SMTP服务器。

+0

好,所以我正在改变我的代码使用Gmail的smtp ....你能告诉我如何设置通过SwiftMailer的SSL信息,以得到这个工作?我还需要在某处添加用户名/密码。 – Metropolis 2010-12-20 00:14:00

+0

很酷,我将ssl字符串添加到了构造函数中,它工作正常!谢谢您的帮助。 – Metropolis 2010-12-20 01:02:16

+1

我正在测试从本地主机,因为我设置了一个webapp我写和$运输= Swift_MailTransport :: newInstance('localhost',25);得到它为我工作 – kyle 2013-12-15 21:35:52