Page 1 of 1

Modifying message objects affects subsequent sends

Posted: Fri Dec 05, 2008 3:05 pm
by gmorehoudh
I've implemented a SendListener plugin to mail every 100th message sent out in a mass mailing also to a list of recipients (so we can see samples of our newsletters going out, etc). Problem is, even though I create a new Swift instance to send the message, even though I clone the message object, and so forth, every subsequent message sent has the modified subject line that this plugin is creating. I've had this problem with other instances where I was trying to modify the subject line and had to give up.

Please note that MySwift is a class that extends Swift to divert mail when we're not using production servers, but I replaced it in this instance with Swift for testing and the problem still occurred.

After this plugin runs, nearly ALL the messages sent by Swift (even in a separate instance, since you can see I'm creating my own new instance to send these samples) have the "CRM" bit prepended to the subject line!

Code: Select all

/**
 * Sends every hundredth message in a CRM mailing to a specific set of recipients.
 *
 */
class MySwift_CRM_Subset_Mailer implements Swift_Events_SendListener {
 
    protected static $num_sent;
    protected $recipients;
    
    /**
     * Constructor.
     *
     * @param Swift_RecipientList $recipients the list of people to send every 100th message to
     */
    public function __construct(Swift_RecipientList $recipients) {
        $this->recipients = $recipients;
        $this->num_sent = -1;
    }
    
    /**
     * Called by Swift when sending a message.
     *
     * @param Swift_Events_SendEvent $e a SendEvent -- see http://www.swiftmailer.org/api/php5/Swi ... Event.html
     */
    public function sendPerformed(Swift_Events_SendEvent $e) {
        $this->num_sent++;
        
        if(($this->num_sent % 100) == 0) {
            $swift = new MySwift(new Swift_Connection_Sendmail());
            
            $message = clone($e->getMessage());     // why does this affect subsequent sends when we create our own swift instance and clone the message?
            //$message = new Swift_Message("CRM " . $this->num_sent . ": " . $orig_message->getSubject(), $orig_message->getBody(), $orig_message->getContentType());
            $message->setSubject("CRM " . $this->num_sent . ": " . $message->getSubject());
            
            $swift->send($message, $this->recipients, "donotreply@tfaw.com");
        } 
    }
}
 

Re: Modifying message objects affects subsequent sends

Posted: Fri Dec 05, 2008 9:58 pm
by Chris Corbyn
Swift message objects are not designed to be cloned fully. Cloning only clones the object itself, but all of its properties (including the headers) will reference the original objects, which is why your subject changes in the message, since the subject is a Header.

Perhaps you could just copy the subject line into a property, then restore it after you've sent the message?

Re: Modifying message objects affects subsequent sends

Posted: Mon Dec 08, 2008 1:11 pm
by gmorehoudh
Hi Chris.

I've dug around in the docs pretty thoroughly and I'm not sure how to accomplish what you're suggesting. I'll keep looking and post back if I can figure it out.

Re: Modifying message objects affects subsequent sends

Posted: Mon Dec 08, 2008 3:21 pm
by Chris Corbyn
What I mean is simply (pseudo):

Code: Select all

function sendPerformed(...) {
  if (($this->_count % 100) == 0)
  {
    $oldSubject = $message->getSubject();
    $message->setSubject("New subject");
    $swift->send($message, .....);
    $message->setSubject($oldSubject);
  }
}