PHP SwiftMailer 本地主机测试设置

发布于 2024-10-08 13:08:44 字数 1439 浏览 2 评论 0原文

我刚刚开始学习如何使用 SwiftMailer 并且我在从本地主机发送简单的测试消息时遇到问题。下面是我尝试使用的代码。

//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');

I just started learning how to use SwiftMailer and I am having trouble sending a simple test message from my localhost. Below is the code that I am trying to use.

//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);

Here is part of my error 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

Update

I got it to work using gmail. I changed the Swift_SmtpTransport line to the following,

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')->setUsername('username')->setPassword('password');

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

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

发布评论

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

评论(1

却一份温柔 2024-10-15 13:08:44

localhost 是当前计算机的别名(在本例中为运行 PHP 的计算机)。如果您确实想使用本地主机发送邮件,您可以这样说:

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

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

localhost is an alias for current machine (in this case, the machine PHP runs on). If you really want to send mail with localhost you have say so:

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

... but you also need to install and configure your own mail server. If you don't know what's this all about, I suggest you use your mail provider's SMTP server.

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