将 php 的 swiftmailer 与 gmail 结合使用
我正在编写一个简单的脚本,其中使用 gmail 帐户向自身发送电子邮件。
我从 SwiftMailer 的 参考 更改了脚本,但没有得到任何结果。怎么了?
编辑:经过进一步调试,我发现该语句
$result = $mailer->send($message);
导致代码失败(下面的回显不打印)。
这是为什么呢?就因为消息没有发送导致程序崩溃? :/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
require_once '/var/www/swift/lib/swift_required.php';
echo 'Mail sent <br />';
/* //create the transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587);
->setUsername('[email protected]')
->setPassword('softrain1234')
;
*/
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587)
->setUsername('[email protected]')
->setPassword('password')
;
echo 'line 40 <br />';
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('[email protected]' => 'Evaluaciones'))
->setTo(array('[email protected]'=> 'A name'))
->setBody('Test Message Body')
;
echo 'line 52 <br />';
$result = $mailer->send($message);
echo $result;
echo 'line 58 <br />';
?>
</body>
</html>
测试表格:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Test Mail Script</title>
</head>
<body>
<form action="scriptmail.php" method="post">
<input type="submit"/>
</table>
</form>
</body>
</html>
I'm writing a simple script in which a gmail account is used to send an email to itself.
I altered the script from SwiftMailer's reference, but I'm not getting any results. What's wrong?
Edit: after further debugging I've found that the statement
$result = $mailer->send($message);
causes the code to fail (the echo below it doesn't print).
Why is this? Just cause the message isn't sent the program crashes? :/
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
require_once '/var/www/swift/lib/swift_required.php';
echo 'Mail sent <br />';
/* //create the transport
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587);
->setUsername('[email protected]')
->setPassword('softrain1234')
;
*/
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587)
->setUsername('[email protected]')
->setPassword('password')
;
echo 'line 40 <br />';
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('[email protected]' => 'Evaluaciones'))
->setTo(array('[email protected]'=> 'A name'))
->setBody('Test Message Body')
;
echo 'line 52 <br />';
$result = $mailer->send($message);
echo $result;
echo 'line 58 <br />';
?>
</body>
</html>
The test form:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Test Mail Script</title>
</head>
<body>
<form action="scriptmail.php" method="post">
<input type="submit"/>
</table>
</form>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
并不是要复活旧帖子,而是为了以防万一其他人正在寻找答案,并且因为尽管年龄很大,但这篇文章是在我寻找解决方案期间出现的。
当使用 PHP SwiftMailer 连接到 Gmail 或 Google Apps 电子邮件帐户时,您需要使用以下内容
这对我来说效果很好。在原始代码中,您使用端口 587 而不是 465,并且没有指定协议 (ssl)。不知道这是否重要,但对我来说,端口 587 失败,而 465 工作正常。
Don't mean to resurrect an old post, but just in case others are looking for the answer, and because this post came up during my search for a solution despite the age.
When using PHP SwiftMailer to connect to Gmail or Google Apps email accounts you need to use the following
This works fine for me. In the original code, you are using port 587 not 465 and you are not specifying the protocol (ssl). Don't know if that bit matters, but for me port 587 failed and 465 worked fine.
我在谷歌搜索中发现了这个问题,并使用了 fullbaked 提供的答案中的代码。它让我在抛出的异常中看到 Google smtp 服务器正在响应一个错误代码,指示密码和用户名不被接受。经过进一步调查,我发现了以下步骤。现在它工作得很好,我可以继续充实生产代码。
不确定 Google 何时进行了更改,但除了使用 Google 用户名、密码和端口(465/ssl 或 587/tls)配置代码之外,您还需要执行以下步骤才能使用 Google smtp 服务器。
要将 gmail smtp 服务器与您的 Gmail 帐户一起使用,您需要:
1) 在 Google“帐户设置”中,通过将其设置为“允许”来启用“访问不太安全的应用程序”。
2) 在 Gmail 设置中的“转发和 POP/IMAP”选项卡下,选中
IMAP 状态,需要启用。
(这将允许发送的电子邮件保存在已发送文件夹中。)
3) 如果在步骤 1 和 2 之后您仍然抛出异常 Google's smtp
服务器不接受您需要打开的用户名和密码
使用浏览器,转到 Gmail 并登录,然后打开另一个选项卡
相同的浏览器并转到此地址:
https://accounts.google.com/DisplayUnlockCaptcha
4) 立即通过您的代码发送一封电子邮件,因为这样可以
授权使用您的 Gmail 帐户。
希望这有帮助。
I found this question doing a google search and used the code in the answer provided by fullybaked. It got me to the point where I could see in the Exceptions thrown that the Google smtp server was responding with an error code indicating password and username not accepted. Upon further investigation I found the following steps. Now it works great and I can continue to flesh out the code for production.
Not sure when Google made the change but in addition to the configuring your code with your Google username, password and port (465/ssl or 587/tls) you need to do the following steps to be able to use the Google smtp server.
To use the the gmail smtp server with your gmail account you need to:
1) In Google "Account settings" enable "Access for less secured apps" by setting it to "Allow".
2) In gmail settings under the "Forwarding and POP/IMAP" tab, check the
IMAP status, it needs to be enabled.
(This will allow the emails sent to be saved in the sent folder.)
3) If after steps 1 and 2 you still throw the Exception Google's smtp
server is not accepting the user name and password you need to open
a browser, go to gmail and log in, then open another tab in the
same browser andgo to this address:
https://accounts.google.com/DisplayUnlockCaptcha
4) Immediately send an email from your code as this will allow it to be
authorized to use your gmail account.
Hope this helps.
我发现evad说的是真的,但是我必须为当前版本的Swift Mailer稍微改变他的工作。
现在是:
应该可以正常工作。
I found that what evad said was true, but I had to change his work a little bit for the current version of Swift Mailer.
Now it is:
Should work fine.
GMail SMTP 系统在 SSL 和端口方面存在问题。我发现很难让它与 PHP 很好地协同工作。
我发现的最好的东西是 phpGMailer。您也许可以筛选这些代码,看看他们是如何让它工作的,但这对我来说总是完美无缺。
我知道这并不能解决 SwiftMail 问题,只是想我会指出这一点:)
The GMail SMTP system has it's issues given the SSL and ports. I find it hard to get it to work with PHP nicely.
The best thing I have found, that does work is phpGMailer. You may be able to sift through that code to see how they got it to work, but that has always worked flawlessly for me.
I know this does not address the SwiftMail issue, just figured I would point that out :)
465 是 ssl 的端口,587 用于 tls 加密(查看 http://swiftmailer .org/docs/sending.html#encrypted-smtp)
465 is the port for ssl, 587 is used for tls encryption (look at http://swiftmailer.org/docs/sending.html#encrypted-smtp)
我也是经过几个小时的研究才来到这里的。我终于找到了真正的答案。我想从 google 邮件服务器发送电子邮件,而不是从我的带有 smtp 身份验证的主机发送电子邮件。
您可以跟踪 Gmail 中包含完整标头的电子邮件,请遵循本指南。
https://support.google.com/mail/answer/29436?hl= en
@fullbaked 有通过 smtp 验证您的电子邮件的正确答案,但您仍然会通过主机发送。
@ducin 对于不同的端口和加密类型是正确的。
或者更现代、更安全,使用;
如果您想直接从 Gmail 服务器发送,则需要设置授权模式。默认为普通。
@gene 也是正确的,你必须验证你的应用程序,所以按照他的指示操作。
此外,为了防止您的电子邮件进入垃圾邮件文件夹,您还必须使用 DKIM(域密钥)添加 dns 文本记录
https://support.google.com/a/answer/174124?hl=en
SPF 记录
https://support.google.com/a/answer/33786?hl=en
和 DMARC
https://support.google.com/a/answer/2466580?hl=en
I also came here after hours of research. I finally found the true answer. I wanted to send email FROM google mail servers and not from my host with smtp authentication.
You can trace an email with its full headers inside of gmail, follow this guide.
https://support.google.com/mail/answer/29436?hl=en
@fullybaked had the right answer for authenticating your email via smtp but you would still be sending via your host.
@ducin was correct with the different ports and encryption types.
OR more modern and more secure, use;
If you want to send directly from a gmail server you need to set the authorization mode. Plain is default.
@gene is also correct, you do have to authenticate your app, so follow his instruction.
Further to prevent your email from landing in the spam folder, you will also have to add dns text records with DKIM (domain keys)
https://support.google.com/a/answer/174124?hl=en
SPF records
https://support.google.com/a/answer/33786?hl=en
and DMARC
https://support.google.com/a/answer/2466580?hl=en