Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift mailer send to queue for later delivery

When using http://swiftmailer.org can I send a message to the mail queue so that php returns right away instead of actually sending the message right now?

like image 645
crickeys Avatar asked Jan 27 '12 20:01

crickeys


2 Answers

This is an old question, but since it came up in my google search, I'll answer it with what I figured out.

YES! Swiftmailer has the ability to write to a spool instead of sending immediately. Implementation is pretty easy:

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

This tells swiftmailer to write messages to the disk rather then send them. Then using a cron job or other trigger send the messages using something like:

$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);
like image 117
Noah Duncan Avatar answered Nov 16 '22 19:11

Noah Duncan


You can't. swiftmailer/php don't actually deliver the mail for you, they just hand it over to the SMTP server, and THAT server does the delivery for you. You'd need to tell the SMTP to not process the outgoing queue to 'stop' delivery.

In realworld terms, swift/php just walk to the corner and drop your envelope in the mail box. The postal truck shows up immediately afterwards and starts the process of sending the mail on its way through the postal system. But that's completely out of PHP's purview.

like image 28
Marc B Avatar answered Nov 16 '22 19:11

Marc B