Tracking batchSend
Moderators: Chris Corbyn, General Moderators
-
pixelfused
- Forum Newbie
- Posts: 6
- Joined: Mon Apr 14, 2008 8:58 am
Tracking batchSend
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?
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Tracking batchSend
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 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?
-
pixelfused
- Forum Newbie
- Posts: 6
- Joined: Mon Apr 14, 2008 8:58 am
Re: Tracking batchSend
Thanks for the info Chris, I wanted to be sure I wasn't overlooking something obvious. Great software btw, very helpful.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Tracking batchSend
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
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())
");
}
}
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Tracking batchSend
Looks good to me
Good work!
Re: Tracking batchSend
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
nice script man!
really usefull
really usefull
Re: Tracking batchSend
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:
And here's the 'plugin':
The above code throws an error:
... and doesn't return an ID. Can anyone explain what I'm doing wrong? Any help would be appreciated!
Thanks a lot!
David
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";
}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)
{
}
}Code: Select all
Fatal error: Call to undefined method Swift_Mailer::returnId() in ... examples/basics2.php on line 49Thanks a lot!
David
Re: Tracking batchSend
Change
To
and
$mailer->returnId(); by $returnPlugin->returnId();
Code: Select all
$mailer->registerPlugin(new Swift_Plugins_Return_id());
Code: Select all
$mailer->registerPlugin($returnPlugin=new Swift_Plugins_Return_id());
and
$mailer->returnId(); by $returnPlugin->returnId();
Re: Tracking batchSend
Thank you xdecock! That did take care of the error I had. But now a new one occurs:
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
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 6Or 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
Yup, not reviewed all your code
But suddenly, wine make me doubt of what you try to do, hope my snippet helps you...
PS: wine is not emulator
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
Thanks again. And wine is good, but too much does make coding coherently somewhat harder
The code you gave me first got me this error:
So I changed:
to:
And now I'm stuck with the errors:
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
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 6Code: Select all
$this->messageIds=array();Code: Select all
private $messageIds = array();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 21Cheers, David
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Tracking batchSend
In that code $e should be $evt. Simple typo due to wine consumption 
But what information are you trying to get? Addresses, or Message-ID headers? This plugin collects addresses of the recipients.
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];
}
}Re: Tracking batchSend
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
I'd like to get the message ID. But am I then going about it in the wrong way?
Cheers, David