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

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 »

I'll post back here in a sec. This code is not correct if what you want is the Message-ID. It's also not correct for v4 (no $evt->getRecipients() method).

Something has just dawned on me however. If these recipients are all part of a batchSend() they will all have the same Message-ID (because they all get the same message). I think this should be considered a bug and the correct behaviour should be to regenerate the message ID after each send. I'll make this change today.
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 »

Correct code:

Code: Select all

class Swift_Plugins_Return_id implements Swift_Events_SendListener {
 
    private $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)
    {
        $message = $evt->getMessage();
        $this->messageIds[] = $message->getId();
    }
}
But like I say, if these are all in one single call to batchSend() you'll find that all the IDs are the same. I can fix this so easily but you may have to wait a couple of hours for the next release.
ddh
Forum Newbie
Posts: 11
Joined: Mon Feb 02, 2009 8:53 am

Re: Tracking batchSend

Post by ddh »

ok, just to add: I'm using send() and not batchsend() at the moment.

edit: I see that you already posted the code. Great, thanks, you're fast! I'll give it a try right away.

David
xdecock
Forum Commoner
Posts: 37
Joined: Tue Mar 18, 2008 8:16 am

Re: Tracking batchSend

Post by xdecock »

yup, message-id must be reset on each body / header change
ddh
Forum Newbie
Posts: 11
Joined: Mon Feb 02, 2009 8:53 am

Re: Tracking batchSend

Post by ddh »

Still no luck...

Right now I get this error again:

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 48 and defined in /home/.../Swift-4.0.0-b4/lib/classes/Swift/Plugins/ReturnIdPlugin.php on line 8
How should I pass the instance of Swift_events_SendEvent to Swift_Plugins_Return_id::returnId?

To recollect, this is the code I now use:

The plugin:

Code: Select all

class Swift_Plugins_Return_id implements Swift_Events_SendListener {
    
    //$this->messageIds=array();
    private $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) {
        $message = $evt->getMessage();
        $this->messageIds[] = $message->getId();
 
    }
 
}
And the sending code:

Code: Select all

$mailer = Swift_Mailer::newInstance($transport);
 
//Create a message
$message = Swift_Message::newInstance('A basic message')
  ->setBody("the body
", 'text/plain')
  ->setTo(...)
  ->setFrom(...)
  ;
 
  require "../lib/classes/Swift/Plugins/ReturnIdPlugin.php";
  $mailer->registerPlugin($returnPlugin=new Swift_Plugins_Return_id());
  
 
//Send it
if ($mailer->send($message))
{
  echo "Message sent!";
  
  echo "\nId:" . $returnPlugin->returnId();
}
else
{
  echo "Error sending";
}
Thanks again!

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 »

xdecock wrote:yup, message-id must be reset on each body / header change
I just implemented it :) After each send() inside any of the transports $message->generateId() is invoked. Seemed like the least expensive and most foolproof way to do it :)

Sorry, this method declaration:

Code: Select all

public function returnId(Swift_Events_SendEvent $e) {
 
//should be
 
public function returnId() {
xdecock
Forum Commoner
Posts: 37
Joined: Tue Mar 18, 2008 8:16 am

Re: Tracking batchSend

Post by xdecock »

remove the bold section in the code :

returnId(Swift_Events_SendEvent $e) {
xdecock
Forum Commoner
Posts: 37
Joined: Tue Mar 18, 2008 8:16 am

Re: Tracking batchSend

Post by xdecock »

Chris Corbyn wrote:
xdecock wrote:yup, message-id must be reset on each body / header change
I just implemented it :) After each send() inside any of the transports $message->generateId() is invoked. Seemed like the least expensive and most foolproof way to do it :)
Yup Safe route
ddh
Forum Newbie
Posts: 11
Joined: Mon Feb 02, 2009 8:53 am

Re: Tracking batchSend

Post by ddh »

Great, this works!!!!!!

Thank you both very much!

Cheers, David
Post Reply