Page 1 of 1

possible to extend the batchSend() method?

Posted: Sat Apr 26, 2008 4:54 pm
by s.dot
I don't really know my terminology here...

But since batchSend() returns an integer.. is there an internal counter involved? Like a class member updated? If so, would it be possible (or easily doable, or even fathomable, etc..) to write a listener for it to get realtime browser updates on how many emails have been sent?

In psuedo code, something like..

Code: Select all

while ($numSent = $swift->batchSendListen($message, $recipients, $from))
{
    echo $numSent . '<br />';
}

Re: possible to extend the batchSend() method?

Posted: Sat Apr 26, 2008 7:01 pm
by Chris Corbyn
Use a plugin:

Code: Select all

class SendCounterPlugin implements Swift_Events_SendListener {
  private $_count = 0;
  public function sendPerformed(Swift_Events_SendEvent $e) {
    $this->_count += $e->getNumSent();
    printf("%d Messages sent\n", $this->_count);
  }
}
If you need addresses you need to do add some logic comparing who's in $e->getRecipients() with who's in $e->getFailedRecipients().

Re: possible to extend the batchSend() method?

Posted: Sun Apr 27, 2008 3:12 pm
by s.dot
amazing! thanks.
just the number sent is fine for me

Re: possible to extend the batchSend() method?

Posted: Wed Apr 30, 2008 7:21 pm
by s.dot
Wooo, this code works beautifully. For anyone else reading this, I do want to point out the obvious.. you have to attach the plugin. :)

Code: Select all

//your swift code here
 
class SendCounterPlugin implements Swift_Events_SendListener
{
    private $_count = 0;
    public function sendPerformed(Swift_Events_SendEvent $e)
    {
        $this->_count += $e->getNumSent();
        echo "{$this->_count} Messages sent<br />";
    }
}
 
$swift->attachPlugin(new SendCounterPlugin(), 'counter');
$swift->batchSend(...);