Modifying message objects affects subsequent sends
Posted: Fri Dec 05, 2008 3:05 pm
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!
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");
}
}
}