swift mailer发送到队列以便以后发送

cri*_*eys 8 php email swiftmailer

使用http://swiftmailer.org时,我可以向邮件队列发送一条消息,以便php立即返回,而不是立即发送消息吗?

Noa*_*can 6

这是一个老问题,但是因为它出现在我的谷歌搜索中,我会用我想出的答案回答它.

是!Swiftmailer能够写入假脱机而不是立即发送.实施非常简单:

$spool = new Swift_FileSpool('/where/you/want/your/spool');
$transport = Swift_SpoolTransport::newInstance($spool);
$mailer = Swift_Mailer::newInstance($transport);
Run Code Online (Sandbox Code Playgroud)

这告诉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);
Run Code Online (Sandbox Code Playgroud)


Mar*_*c B 1

你不能。swiftmailer/php 实际上并不为您传递邮件,它们只是将其交给 SMTP 服务器,然后该服务器为您传递邮件。您需要告诉 SMTP 不要处理传出队列以“停止”传送。

在现实世界中,swift/php 只需走到角落并将信封放入邮箱即可。邮政卡车随后立即出现,并开始通过邮政系统发送邮件。但这完全超出了 PHP 的权限。