Page 1 of 1

Decorator Plugin Problem

Posted: Fri Jun 06, 2008 11:46 am
by spib
Hi,

I think we're having a problem with the decorator plugin because I'm seeing incorrect information in the replacements which are made. For example, for a given email address we should see the recipient's user id in the message but we are seeing a different user ID.

Psuedo code for the way we use swift is

Create new swift(new Swift_Connection_Sendmail)
Create new SwiftMessage
Attach html and text message parts (including decorator replacement 'tags')
For Each recipient
Create new Swift_RecipientList
Add recipient to RecipientList
Create substitution array for decorator
Attach decorator plugin
swift->send()
End For Each
Disconnect Swift

Anything which is glaringly wrong there?

Many Thanks

James

P.S. We've sent over 2 million legimate opt-in emails in the 2 months since we've been using swift - pretty impressive!

Re: Decorator Plugin Problem

Posted: Fri Jun 06, 2008 6:51 pm
by Chris Corbyn
Don't attach the plugin multiple times inside the loop. That will break it. Build the full array of replacements and attach it before sending all the messages.

Re: Decorator Plugin Problem

Posted: Sat Jun 07, 2008 2:54 am
by spib
HI Chris,

Thanks for the reply. So I should only call swift->send once for all recipients in this batch? Does that mean it will send one message with multiple recipients (in the To: field) or will it still send mutliple individual emails? New pseudo-code below

Create new swift(new Swift_Connection_Sendmail)
Create new SwiftMessage
Attach html and text message parts (including decorator replacement 'tags')
For Each recipient
Create new Swift_RecipientList
Add recipient to RecipientList
Create substitution array for decorator for this recipient
End For Each
Attach decorator plugin
swift->send()
Disconnect Swift

Re: Decorator Plugin Problem

Posted: Tue Jun 10, 2008 1:16 am
by spib
Chris, any response to my reply below?

Thanks

Re: Decorator Plugin Problem

Posted: Tue Jun 10, 2008 5:17 am
by Chris Corbyn
No, you can still loop with send(), but you should only attach the plugin once (before you start looping). If you don't want to loop and send() at all then use batchSend().

Pseudo:

Code: Select all

<?php
 
$replacements = array();
 
foreach ($addresses as $address) {
  $replacements[$address] = array( /* some replacements */ );
}
 
$swift->attachPlugin(new Swift_Plugin_Decorator($replacements), 'decorator');
 
foreach ($addresses as $address) {
  $swift->send($message, $address, $yourAddress);
}
You could replace that last loop with a batchSend() like this:

Code: Select all

<?php
 
$recipients = new Swift_RecipientList();
$replacements = array();
 
foreach ($addresses as $address) {
  $replacements[$address] = array( /* some replacements */ );
  $recipients->addTo($address);
}
 
$swift->attachPlugin(new Swift_Plugin_Decorator($replacements), 'decorator');
 
$swift->batchSend($message, $recipients, $yourAddress);

Re: Decorator Plugin Problem

Posted: Tue Jun 10, 2008 1:56 pm
by spib
Thanks Chris, will give that a try. Great work as always