簡體   English   中英

Swift Mailer不發送電子郵件

[英]Swift Mailer not sending email

我正在嘗試使用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必須退出!

將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>';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM