尝试使用 swift mailer、gmail smtp、php 发送邮件
这是我的代码:
<?php
require_once 'Swift/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465)
->setUsername('[email protected]')
->setPassword('pass');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('[email protected]' => 'MY NAME'))
->setTo(array('[email protected]' => 'YOU'))
->setBody('This is the text of the mail send by Swift using SMTP transport.');
//$attachment = Swift_Attachment::newInstance(file_get_contents('path/logo.png'), 'logo.png');
//$message->attach($attachment);
$numSent = $mailer->send($message);
printf("Sent %d messages\n", $numSent);
?>
运行后出现此错误...
致命错误:未捕获异常“Swift_TransportException”,消息“预期响应代码 220,但在 /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php 中收到代码“”,消息“”” :406
Stack trace:
#0 /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php(299): Swift_Transport_AbstractSmtpTransport->_assertResponseCode('', Array)
#1 /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php(107): Swift_Transport_AbstractSmtpTransport->_readGreeting()
#2 /home/sitenyou/public_html/Swift/lib/classes/Swift/Mailer.php(74): Swift_Transport_AbstractSmtpTransport->start()
#3 /home/sitenyou/public_html/sgmail.php(16): Swift_Mailer->send(Object(Swift_Message))
#4 {main} thrown in /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php on line 406
Here is my code:
<?php
require_once 'Swift/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465)
->setUsername('[email protected]')
->setPassword('pass');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('[email protected]' => 'MY NAME'))
->setTo(array('[email protected]' => 'YOU'))
->setBody('This is the text of the mail send by Swift using SMTP transport.');
//$attachment = Swift_Attachment::newInstance(file_get_contents('path/logo.png'), 'logo.png');
//$message->attach($attachment);
$numSent = $mailer->send($message);
printf("Sent %d messages\n", $numSent);
?>
AFter RUNNING GOT THIS ERROR...
Fatal error: Uncaught exception 'Swift_TransportException' with message 'Expected response code 220 but got code "", with message ""' in /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php:406
Stack trace:
#0 /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php(299): Swift_Transport_AbstractSmtpTransport->_assertResponseCode('', Array)
#1 /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php(107): Swift_Transport_AbstractSmtpTransport->_readGreeting()
#2 /home/sitenyou/public_html/Swift/lib/classes/Swift/Mailer.php(74): Swift_Transport_AbstractSmtpTransport->start()
#3 /home/sitenyou/public_html/sgmail.php(16): Swift_Mailer->send(Object(Swift_Message))
#4 {main} thrown in /home/sitenyou/public_html/Swift/lib/classes/Swift/Transport/AbstractSmtpTransport.php on line 406
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
鉴于这个问题的年龄,我最近不得不经历这个过程,所以我想向仍然来到这里的人确认这一切仍然相关^^,但这里有一个关于如何启用两步验证的完整指南,获取您的应用程序密码并发送电子邮件。
如前所述使用 Gmail 时,您需要在 Google 帐户上启用两步验证并创建应用程序密码。
点击 9 个点 >安全>两步验证>开始使用
输入您的手机,然后验证设备。
启用后,您需要转到两步验证,然后滚动到底部并找到应用程序密码。
创建一个新密码,然后在代码中使用它,如下所示:
Given the age of the question I have just recently had to go through this process so I would like to confirm for anyone still coming here that it is all still relavent ^^ but here is a full guide on how to enable 2-Step Verification, get your App Password and send an email.
When using gmail as mentioned previously you need to have 2-Step Verification enabled on your Google Account and create an App Password.
Click on the 9 dots > Security > 2-Step Verification > Get Started
Enter your mobile and then verify the device.
Once you have enabled it you need to go to 2-Step Verification and then scroll to the bottom and find App Passwords.
Create a new password and then use it in your code as below:
GMail 的 SMTP 需要加密。使用:
GMail's SMTP requires encryption. Use:
缺少 ssl 参数,应该是类似的内容经过
测试并且工作正常
there is missing the ssl parameter, it should be something like that
Tested and work fine
Swift SmtpTransport - 代码(发送电子邮件)
GMAIL 的 SMTP 是:smtp.googlemail.com
完整代码:
Swift SmtpTransport - Code (send a email)
The SMTP of GMAIL is: smtp.googlemail.com
The Full Code:
我不能确定,但我认为 Gmail 的端口是 587,使用的是 TLS,这不是 SSL,而是它的较新版本。您应该检查一下,因为我认为您放置了错误的构造代码。
祝你好运!
I cannot be sure, but I think that Gmail's port is 587 using TLS, which is not SSL, but a newer version of it. You should check into that, because I think you are placing the wrong construction code.
Best of luck!
我已经设法在没有 SSL 的情况下使其工作,方法如下:
I have managed to get this working without the SSL, here is how:
我在 Laravel 3 中使用“Messages Swift Mailer”包并遇到了同样的问题。经过一些测试后,就我而言,解决方案是在“from”参数上设置我在 SMTP 身份验证中使用的相同电子邮件地址。
我试图使用不同的地址,这触发了“swiftmailer 预期响应代码 220 但收到带有消息的代码” 错误。
希望有帮助。
I'm using the "Messages Swift Mailer" bundle in Laravel 3 and having the same issue. After some testing, in my case, the solution was to set the same email address that I used in the SMTP authentication on the "from" parameter.
I was trying to use a different address and that was triggering the "swiftmailer expected response code 220 but got code with message" error.
Hope that helps.
我之前遇到过同样的错误,我在 Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl") 中添加了“ssl”参数,就像 osos 所说的那样。
有用!!谢谢..:D
这是我的代码:
I got same error before and i added "ssl" parameter in Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl") like osos said.
IT WORKS!! thanks..:D
this is my code:
对于谷歌应用程序,除了按照接受的答案中建议的设置端口 465 和 ssl 之外,您可能还需要 启用允许不太安全的应用设置,按照https://stackoverflow.com/a/25238515/947370
For google apps, in addition to setting to port 465 and ssl as recommended in the accepted answer, you may have to enable allow less secure apps setting, as per https://stackoverflow.com/a/25238515/947370