使用 php 的 swiftmailer 和 gmail
我正在编写一个简单的脚本,其中使用 gmail 帐户向自己发送电子邮件.
I'm writing a simple script in which a gmail account is used to send an email to itself.
我从 SwiftMailer 的 reference 更改了脚本,但没有得到任何结果.怎么了?
I altered the script from SwiftMailer's reference, but I'm not getting any results. What's wrong?
经过进一步调试,我发现语句
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('softrain.evaluaciones@gmail.com')
->setPassword('softrain1234')
;
*/
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587)
->setUsername('softrain.evaluaciones@gmail.com')
->setPassword('password')
;
echo 'line 40 <br />';
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('softrain.evaluaciones@gmail.com' => 'Evaluaciones'))
->setTo(array('softrain.evaluaciones@gmail.com'=> '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>
推荐答案
不是要复活一个旧帖子,而是以防万一其他人正在寻找答案,因为这个帖子是在我寻找解决方案的过程中出现的尽管年龄.
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.
使用 PHP SwiftMailer 连接到 Gmail 或 Google Apps 电子邮件帐户时,您需要使用以下内容
When using PHP SwiftMailer to connect to Gmail or Google Apps email accounts you need to use the following
$transporter = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')
->setUsername($this->username)
->setPassword($this->password);
$this->mailer = Swift_Mailer::newInstance($transporter);
这对我来说很好.在原始代码中,您使用的是端口 587 而不是 465,并且您没有指定协议 (ssl).不知道那一点是否重要,但对我来说,端口 587 失败,而 465 工作正常.
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.
希望对您有所帮助.
相关文章