【发布时间】:2014-07-23 15:00:32
【问题描述】:
我正在尝试使用 Swift Mailer 发送带有附件的电子邮件。电子邮件未发送。我对 PHP 相当陌生,所以这可能是一个简单的问题,但我无法弄清楚。代码:
<?php
require_once 'swift/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465)
->setUsername('my gmail address')
->setPassword('my gmail password')
;
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject('subject')
->setFrom(array('John@doe.com' => 'John Doe'))
->setTo(array('Jane@doe.com' => 'Jane Doe'))
->setBody('body')
->attach(Swift_Attachment::fromPath('image.png'))
;
echo('test 1');
$numSent = $mailer->send($message);
echo('test 2');
printf("Sent %d messages\n", $numSent);
if ($mailer->send($message))
{
echo "Sent\n";
}
else {
echo "Failed\n";
}
?>
swift_required.php 已成功包含。我的测试 1 回声运行,但测试 2 回声从未运行。这让我认为问题存在于 $numSent 变量中。当然,这个问题仍然非常广泛,但希望能稍微缩小范围。另外,$numSent 下面的函数都不起作用,所以它不会告诉我我的电子邮件是否正在发送。
想通了,原来你需要使用 'tls://smtp.gmail.com'。
【问题讨论】:
-
使用debugging information provided in the Swiftmailer documentation 和Swiftmailer Logger plugin 帮助您缩小问题范围。总而言之,您需要学习有效地调试代码(从您编写的内容来看,您似乎根本没有自己完成任何调试工作)。
标签: php html email swiftmailer