Tracking batchSend

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

pixelfused
Forum Newbie
Posts: 6
Joined: Mon Apr 14, 2008 8:58 am

Tracking batchSend

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Tracking batchSend

Post 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.
pixelfused
Forum Newbie
Posts: 6
Joined: Mon Apr 14, 2008 8:58 am

Re: Tracking batchSend

Post by pixelfused »

Thanks for the info Chris, I wanted to be sure I wasn't overlooking something obvious. Great software btw, very helpful.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Tracking batchSend

Post 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 ;)
pixelfused
Forum Newbie
Posts: 6
Joined: Mon Apr 14, 2008 8:58 am

Re: Tracking batchSend

Post 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())
                    ");                 
        }
        
    }
 
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Tracking batchSend

Post by Chris Corbyn »

Looks good to me :) Good work!
audrey
Forum Newbie
Posts: 12
Joined: Sat Mar 03, 2007 2:19 pm

Re: Tracking batchSend

Post 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.
kapitol
Forum Newbie
Posts: 2
Joined: Mon Feb 02, 2009 10:13 am

Re: Tracking batchSend

Post by kapitol »

nice script man!
really usefull :!:
ddh
Forum Newbie
Posts: 11
Joined: Mon Feb 02, 2009 8:53 am

Re: Tracking batchSend

Post 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
xdecock
Forum Commoner
Posts: 37
Joined: Tue Mar 18, 2008 8:16 am

Re: Tracking batchSend

Post 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();
ddh
Forum Newbie
Posts: 11
Joined: Mon Feb 02, 2009 8:53 am

Re: Tracking batchSend

Post 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
xdecock
Forum Commoner
Posts: 37
Joined: Tue Mar 18, 2008 8:16 am

Re: Tracking batchSend

Post 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
ddh
Forum Newbie
Posts: 11
Joined: Mon Feb 02, 2009 8:53 am

Re: Tracking batchSend

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Tracking batchSend

Post 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.
ddh
Forum Newbie
Posts: 11
Joined: Mon Feb 02, 2009 8:53 am

Re: Tracking batchSend

Post 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
Post Reply