Page 1 of 2

Tracking batchSend

Posted: Tue Jan 13, 2009 8:13 am
by pixelfused
I'm using batchSend to send out a newsletter, but I would like to be able to track the message ids, as well as successful recipients as I go. (In case the system crashes, etc... if I ever have to restart the send process I need to know who has received it already) Is this possible?

Re: Tracking batchSend

Posted: Tue Jan 13, 2009 7:07 pm
by Chris Corbyn
pixelfused wrote:I'm using batchSend to send out a newsletter, but I would like to be able to track the message ids, as well as successful recipients as I go. (In case the system crashes, etc... if I ever have to restart the send process I need to know who has received it already) Is this possible?
It is possible, you'll need to write a plugin. There's some documentation on the wiki for writing plugins. This one would be incredibly simple, using a SendListener and reading the addresses as they pass through.

Re: Tracking batchSend

Posted: Thu Jan 15, 2009 8:24 am
by pixelfused
Thanks for the info Chris, I wanted to be sure I wasn't overlooking something obvious. Great software btw, very helpful.

Re: Tracking batchSend

Posted: Thu Jan 15, 2009 10:50 pm
by Chris Corbyn
Let me know if you get stuck... I'll be happy to help with the plugin. It probably amounts to about 20 lines of code ;)

Re: Tracking batchSend

Posted: Fri Jan 16, 2009 10:09 am
by pixelfused
This is what I ended up with, seems to be working great. Thanks again for the help!

Code: Select all

 
    require_once dirname(__FILE__) . "/../ClassLoader.php";
    Swift_ClassLoader::load("Swift_Events_SendListener");
    
    class Swift_Plugin_RcptLogging implements Swift_Events_SendListener{
 
        public function sendPerformed(Swift_Events_SendEvent $e){
            $recipients = $e->getRecipients();
            $to = array_keys($recipients->getTo());
            $sent['to'] = $to[0];
            $sent['success'] = $e->getNumSent();
            
            mysql_query("
                        INSERT INTO     log_subscribers_mailings (subscriber_id, mailing_id, sent, ts) 
                        (SELECT         (SELECT id FROM subscribers WHERE email = '$sent[to]'), 
                                        (SELECT mailing_id FROM mail_queue WHERE in_process = 1),
                                        $sent[success],
                                        now())
                    ");                 
        }
        
    }
 

Re: Tracking batchSend

Posted: Fri Jan 16, 2009 10:10 pm
by Chris Corbyn
Looks good to me :) Good work!

Re: Tracking batchSend

Posted: Wed Jan 21, 2009 3:04 pm
by audrey
Thanks for posting this! I was just wondering about the same thing. I'm going to see about adapting this to my code.

Re: Tracking batchSend

Posted: Mon Feb 02, 2009 10:14 am
by kapitol
nice script man!
really usefull :!:

Re: Tracking batchSend

Posted: Tue Feb 03, 2009 1:28 pm
by ddh
Based on the above code, I'm trying to put together a plugin that returns the message id to the calling script. But my knowledge of OOP is unfortunatelly to limited to get it to work.

This is part of the calling script:

Code: Select all

//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
 
//Create a message
$message = Swift_Message::newInstance('A basic message')
  ->setBody("da message", 'text/plain')
  ->setTo(...)
  ->setFrom(...)
  ;
 
  require "../lib/classes/Swift/Plugins/ReturnIdPlugin.php";
  $mailer->registerPlugin(new Swift_Plugins_Return_id());
  
//Send it
if ($mailer->send($message))
{
  echo "Message sent!";
  
  echo "\nId:" . $mailer->returnId();
}
else
{
  echo "Error sending";
}
And here's the 'plugin':

Code: Select all

class Swift_Plugins_Return_id implements Swift_Events_SendListener {
 
    public function returnId(Swift_Events_SendEvent $e) {
        $recipients = $e->getRecipients();
        $to = array_keys($recipients->getTo());
        return $to[0];
 
    }
    
    /**
    * Not used.
    */
    public function beforeSendPerformed(Swift_Events_SendEvent $evt)
    {
    }
 
    public function sendPerformed(Swift_Events_SendEvent $evt)
    {
    }
}
The above code throws an error:

Code: Select all

Fatal error: Call to undefined method Swift_Mailer::returnId() in ... examples/basics2.php on line 49
... and doesn't return an ID. Can anyone explain what I'm doing wrong? Any help would be appreciated!

Thanks a lot!

David

Re: Tracking batchSend

Posted: Tue Feb 03, 2009 2:16 pm
by xdecock
Change

Code: Select all

 
 
$mailer->registerPlugin(new Swift_Plugins_Return_id());
 
 
To

Code: Select all

 
 
$mailer->registerPlugin($returnPlugin=new Swift_Plugins_Return_id());
 
 

and

$mailer->returnId(); by $returnPlugin->returnId();

Re: Tracking batchSend

Posted: Tue Feb 03, 2009 2:40 pm
by ddh
Thank you xdecock! That did take care of the error I had. But now a new one occurs:

Code: Select all

Catchable fatal error: Argument 1 passed to Swift_Plugins_Return_id::returnId() must be an instance of Swift_Events_SendEvent, none given, called in /home/.../Swift-4.0.0-b4/examples/basics2.php on line 49 and defined in /home/.../Swift-4.0.0-b4/lib/classes/Swift/Plugins/ReturnIdPlugin.php on line 6
So it seems that Swift_Plugins_Return_id::returnId() should be passed an instance of Swift_Events_SendEvent. Can anyone tell me how to accomplish this?

Or would there be a different way to write the returnId function, that would not require Swift_Events_sendEvent to be passed?

Again, any help would be appreciated!

David

Re: Tracking batchSend

Posted: Tue Feb 03, 2009 3:21 pm
by xdecock
Yup, not reviewed all your code

Code: Select all

 
class Swift_Plugins_Return_id implements Swift_Events_SendListener {
 
    $this->messageIds=array();
    public function returnId(Swift_Events_SendEvent $e) {
        return $this->messageIds;
    }
   
    /**
    * Not used.
    */
    public function beforeSendPerformed(Swift_Events_SendEvent $evt)
    {
    }
 
    public function sendPerformed(Swift_Events_SendEvent $evt)
    {
        $recipients = $e->getRecipients();
        $to = array_keys($recipients->getTo());
        $this->messageIds[] = $to[0];
    }
}
 

But suddenly, wine make me doubt of what you try to do, hope my snippet helps you...

PS: wine is not emulator

Re: Tracking batchSend

Posted: Tue Feb 03, 2009 3:38 pm
by ddh
Thanks again. And wine is good, but too much does make coding coherently somewhat harder :D

The code you gave me first got me this error:

Code: Select all

Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in /home/.../Swift-4.0.0-b4/lib/classes/Swift/Plugins/ReturnIdPlugin.php on line 6
So I changed:

Code: Select all

$this->messageIds=array();
to:

Code: Select all

private $messageIds = array();
And now I'm stuck with the errors:

Code: Select all

Notice: Undefined variable: e in /home/.../Swift-4.0.0-b4/lib/classes/Swift/Plugins/ReturnIdPlugin.php on line 21
 
Fatal error: Call to a member function getRecipients() on a non-object in /home/.../Swift-4.0.0-b4/lib/classes/Swift/Plugins/ReturnIdPlugin.php on line 21
What I'm trying to do is to catch the message-id of the message that's send, in order to later store it in a database. so that I can process bounced emails.

Cheers, David

Re: Tracking batchSend

Posted: Tue Feb 03, 2009 3:46 pm
by Chris Corbyn
In that code $e should be $evt. Simple typo due to wine consumption :P

Code: Select all

class Swift_Plugins_Return_id implements Swift_Events_SendListener {
 
    private $messageIds=array();
   
    public function returnId() {
        return $this->messageIds;
    }
   
    /**
    * Not used.
    */
    public function beforeSendPerformed(Swift_Events_SendEvent $evt)
    {
    }
 
    public function sendPerformed(Swift_Events_SendEvent $evt)
    {
        $recipients = $evt->getRecipients();
        $to = array_keys($recipients->getTo());
        $this->messageIds[] = $to[0];
    }
}
But what information are you trying to get? Addresses, or Message-ID headers? This plugin collects addresses of the recipients.

Re: Tracking batchSend

Posted: Tue Feb 03, 2009 3:48 pm
by ddh
Thanks Chris, I had just spotted it as well.

I'd like to get the message ID. But am I then going about it in the wrong way?

Cheers, David