Swift Mailer将邮件发送到队列以便稍后发送。

9
当使用 http://swiftmailer.org 时,我是否可以将消息发送到邮件队列中,以便php立即返回而不是立即发送消息?
3个回答

7

这是一个老问题,但由于它出现在我的谷歌搜索中,我将用我找到的方法来回答它。

是的!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);

1

你做不到。SwiftMailer/PHP实际上并不会为你发送邮件,它们只是将邮件交给SMTP服务器,而该服务器会为你处理发送。你需要告诉SMTP停止处理发件队列以“停止”发送。

从现实角度来看,Swift/PHP只是走到街角,把你的信封放进邮箱里。邮政卡车随即出现,并开始通过邮政系统发送邮件。但这完全超出了PHP的范畴。


1
如果我使用sendmail(Swift_SendmailTransport)作为传输方式怎么办? - crickeys

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接