Php Swift邮件发送程序不发送电子邮件

Php Swift邮件发送程序不发送电子邮件,php,html,email,swiftmailer,Php,Html,Email,Swiftmailer,我正在尝试使用Swift Mailer发送带有附件的电子邮件。电子邮件未发送。我对PHP相当陌生,所以这可能是一个简单的问题,但我无法解决它。代码: <?php require_once 'swift/lib/swift_required.php'; $transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465) ->setUsername('my gmail address') ->

我正在尝试使用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 echo运行,但测试2 echo从不运行。这使我认为问题存在于$numSent变量中。当然,问题仍然很广泛,但希望这能缩小一些范围。此外,$numSent以下的函数都不起作用,因此它不会告诉我是否正在发送电子邮件


弄明白了,你需要用tls://smtp.gmail.com“.

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

将错误报告(E_ALL)与ini_集合(“显示错误”,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>';
}

使用以及帮助您缩小问题范围。总而言之,您需要学会有效地调试代码(从您编写的代码来看,似乎您自己根本没有做过任何调试工作)。您知道这可能是什么原因吗?可能是显示错误/错误报告在其他地方被覆盖。您有权访问apache错误日志吗?