首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Swift Mailer不发送电子邮件

Swift Mailer不发送电子邮件
EN

Stack Overflow用户
提问于 2014-07-23 23:00:32
回答 1查看 999关注 0票数 0

我正在尝试使用Swift Mailer发送带有附件的电子邮件。电子邮件未发送。我是PHP的新手,所以这可能是一个简单的问题,但我不能解决它。代码:

代码语言:javascript
运行
复制
<?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包含在内。我的test 1 echo运行,但test 2 echo从不运行。这让我认为问题存在于$numSent变量中。当然,这个问题仍然非常广泛,但希望这能缩小一些范围。此外,$numSent下面的函数都不起作用,所以它不会告诉我我的电子邮件是否正在发送。

弄明白了,原来你需要使用'tls://smtp.gmail.com‘。

EN

Stack Overflow用户

发布于 2014-07-23 23:06:24

如果第二个回声没有出现,那么php肯定已经退出了!

将error_reporting(E_ALL)与ini_set(‘display_ error’,1)结合使用,并添加错误处理程序,如下所示:

代码语言:javascript
运行
复制
set_error_handler(function($severity, $message, $file, $line)
{
    if(!(error_reporting() & $severity))
    {
        return;
    }
    throw new ErrorException($message, $severity, $severity, $file, $line);
});

这将导致通知在与E_ALL结合使用时抛出异常。

代码语言:javascript
运行
复制
<?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>';
}
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24914044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档