我刚刚开始学习如何使用 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);

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top