当使用 http://swiftmailer.org 时,我是否可以将消息发送到邮件队列中,以便php立即返回而不是立即发送消息?
这是一个老问题,但由于它出现在我的谷歌搜索中,我将用我找到的方法来回答它。
是的!Swiftmailer有将邮件写入队列而不是立即发送的能力。实现起来非常容易:
$spool = new Swift_FileSpool('/where/you/want/your/spool');
$transport = Swift_SpoolTransport::newInstance($spool);
$mailer = Swift_Mailer::newInstance($transport);
这告诉SwiftMailer将消息写入磁盘而不是发送它们。然后使用类似以下命令的cron作业或其他触发器来发送消息:
$spool = new Swift_FileSpool('/where/you/put/your/spool');
$spool_transport = Swift_SpoolTransport::newInstance($spool);
// Create the smtp transport.
$smtp_transport = Swift_SmtpTransport::newInstance('your.smtp.host', 25);
// Get the messages from the spool
$spool = $spool_transport->getSpool();
// Send the messages via the real transport.
$sent = $spool->flushQueue($smtp_transport);
你做不到。SwiftMailer/PHP实际上并不会为你发送邮件,它们只是将邮件交给SMTP服务器,而该服务器会为你处理发送。你需要告诉SMTP停止处理发件队列以“停止”发送。
从现实角度来看,Swift/PHP只是走到街角,把你的信封放进邮箱里。邮政卡车随即出现,并开始通过邮政系统发送邮件。但这完全超出了PHP的范畴。
如果您使用sendmail传输,那么它应该立即返回。
来自https://github.com/swiftmailer/swiftmailer/blob/4.1/doc/sending.rst:
通常,当它将您的消息写入磁盘以便发送时,sendmail进程会很快响应。
您还可以查看缓冲:http://symfony.com/doc/current/cookbook/email/spool.html