【问题标题】:Swift Mailer not sending emailSwift Mailer 不发送电子邮件
【发布时间】: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'。

【问题讨论】:

标签: php html email swiftmailer


【解决方案1】:

如果第二个 echo 没有出现,php 一定已经退出了!

结合使用 error_reporting(E_ALL) 和 ini_set('display_errors', 1) 并添加如下错误处理程序:

set_error_handler(function($severity, $message, $file, $line)
{
    if(!(error_reporting() & $severity))
    {
        return;
    }
    throw new ErrorException($message, $severity, $severity, $file, $line);
});

这将导致甚至通知与 E_ALL 结合抛出异常。

<?php ini_set('display_errors', 1); 
error_reporting(E_ALL);
//@todo: add the error handler here
try
{
    //every code in the app must be wrapped in this try statement.
    //require('file_that_mails_or_whatever.php');
}
catch(Exception $e)
{
    echo $e->getMessage() . '<br>';
    echo '<pre>' . $e->getTraceAsString() . '</pre>';
}

【讨论】:

  • 好的,我做到了,但只得到一个空白页。你知道这可能是什么原因吗?
  • 可能是 display_errors / 错误报告在其他地方被覆盖。您可以访问您的 apache 错误日志吗?
猜你喜欢
  • 2011-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-21
  • 1970-01-01
  • 1970-01-01
  • 2015-08-23
  • 2018-02-11
相关资源
最近更新 更多