possible to extend the batchSend() method?

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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

possible to extend the batchSend() method?

Post 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 />';
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: possible to extend the batchSend() method?

Post 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().
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: possible to extend the batchSend() method?

Post by s.dot »

amazing! thanks.
just the number sent is fine for me
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: possible to extend the batchSend() method?

Post 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(...);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply