Decorator Plugin Problem

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

Post Reply
spib
Forum Newbie
Posts: 9
Joined: Wed Jul 09, 2003 2:09 am

Decorator Plugin Problem

Post 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!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Decorator Plugin Problem

Post 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.
spib
Forum Newbie
Posts: 9
Joined: Wed Jul 09, 2003 2:09 am

Re: Decorator Plugin Problem

Post 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
spib
Forum Newbie
Posts: 9
Joined: Wed Jul 09, 2003 2:09 am

Re: Decorator Plugin Problem

Post by spib »

Chris, any response to my reply below?

Thanks
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Decorator Plugin Problem

Post 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);
spib
Forum Newbie
Posts: 9
Joined: Wed Jul 09, 2003 2:09 am

Re: Decorator Plugin Problem

Post by spib »

Thanks Chris, will give that a try. Great work as always
Post Reply