Staggered/Queued Mail Sending?

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
EoN
Forum Newbie
Posts: 22
Joined: Thu Oct 11, 2007 5:57 am

Staggered/Queued Mail Sending?

Post by EoN »

Hi guys,

Currently with my system, I click my 'send' button, and my system/Swift process/send 500+ emails on the spot. It involves decorator etc, so each email is customised for the specific recipient.

My system does this with a few different types of emails.

I've spent the last few days modifying the system slightly so that instead of sending them all on the spot, I instead log 'jobs' into a database table, and a CRON job comes along and sends a 'block' of emails as a batch process.. This is to a) ensure only x amount of emails get sent per hour inline with ISP's requirements. b) to remove some of the 'nail-biting' factor we currently get when relying on a single post/browser/server connection to not 'crash' or something ;)

But I've just run into a bit of a brick wall, as i realised I've approached it sort of wrongly.

So my question is this. Is there a way I can use my current process, and have swift 'generate' all the emails etc, put in the pretty pictures, customisation etc, and do everything up until the point of 'SEND', but instead of sending the actual email, STORE the finished/ready-to-go product in the database, which the cron job could then just 'send' later on? Does that make sense? Any feedback would be greatly appreciated

Cheers,
EoN
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Staggered/Queued Mail Sending?

Post by Chris Corbyn »

Something along the lines of overriding send() works fine.

Code: Select all

class YourDecoratedSwift extends Swift {
  
  private $_db;
  
  public function __construct(Db $db) {
    $this->_db = $db;
  }
  
  public function send($message, $recipients, $from) {
    $this->_db->execute(
      "INSERT INTO mailspool VALUES (?, ?, ?)",
      array(serialize($message), serialize($recipients), serialize($from)
      );
  }
}
If you can get away with saving only certain elements of the message I'd do that however.
EoN
Forum Newbie
Posts: 22
Joined: Thu Oct 11, 2007 5:57 am

Re: Staggered/Queued Mail Sending?

Post by EoN »

Thanks for your reply Chris, it's greatly appreciated - I've been a bit stumped on this problem!

You're right, it seems like overriding the send method is the most logical place! That seems to make sense. Have a few questions thoguh:

1. Will the $message that gets stored there ALREADY have had the 'Decorator' replacements put in place? Does it already have attachments/message parts etc already concatenated together?

2. When the cron job comes to process the spool, what does it need to do to 'pick up' those pieces and send the email? How would I 'reconstruct' it into a Swift sendable state? I'm not sure if I'm making this more complicated than it is. But basically I'm just wondering what steps would needs to be done after the cron job has re-retrieved the fields from the spool.

Thanks,
EoN
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Staggered/Queued Mail Sending?

Post by Chris Corbyn »

Ah no, that's a problem. The plugin system requires that Swift is able to do its thing.

You can probably do this; but it's a bit more intricate knowledge of Swift than I'd say is desirable:

Code: Select all

class YourDecoratedSwift extends Swift {
 
  private $_db;
 
  public function __construct(Db $db) {
    $this->initializeEventListenerContainer(); //Add this call
    $this->_db = $db;
  }
 
  public function send($message, $recipients, $from) {
    $evt = new Swift_Events_SendEvent($message, $recipients, $from, 0);
    $this->notifyListeners($evt, "BeforeSendListener");
    $this->_db->execute(
      "INSERT INTO mailspool VALUES (?, ?, ?)",
      array(serialize($message), serialize($recipients), serialize($from)
      );
    $this->notifyListeners($evt, "SendListener");
  }
}
This is getting messy very quickly though. The more you want Swift to behave as normal without actually sending anything, the messier this will get.

In version 4 you can write a DatabaseTransport class really easily without all this mess. But to do such a thing in version 3 you need to know how to talk SMTP which is not great.

I just had a look to see if you could subclass the NativeMail connection since that already fakes SMTP responses, but the short answer is that it will be just as messy. Sorry.
EoN
Forum Newbie
Posts: 22
Joined: Thu Oct 11, 2007 5:57 am

Re: Staggered/Queued Mail Sending?

Post by EoN »

No problem. The good news I'm taking from your confirmation is that I'm not going crazy - and it was going to be as tricky as I thought.

I'll continue in the original method I was taking for now. Very much looking forward to the DatabaseTransport class in version 4 though!

Thanks again for your reply.
Post Reply