Page 1 of 1

Force "by value" for swift message ?

Posted: Mon Oct 01, 2007 5:06 am
by kondor
Hello !!

I Use swift to send emails who are compose like this :

- template with variables like this {test} (I use Decoration Plugin)
-There is attachments common to all the recipients
-some recipients have a specific attachments

So, I build my email (generic template + common attachments) and then I loop on all the recipients to attach specific files if necessary, I do this in a class method by giving the recipients ($destinataires), the swift message object ($message) and the sender ($from)

Code: Select all

private function doSendEmail($destinataires, $current_message, $from)
	{
		foreach ($destinataires as $destinataire)
		{

			// Specific attachements for the current recipient
			foreach($destinataire->attachements as $attachement)
			{
				$current_message->attach(new Swift_Message_Attachment(base64_decode($attachement->data), $attachement->name, $attachement->mime));	
			}
			
			//send message
			$this->swift->send($current_message, $destinataire->email, $from);
		}
	}
The problem is that when I call $current_message->attach() in the loop this modify the swift message object ITSEFL and not a COPY (this is logical because in PHP5 the arguments are pass by reference and not by value) ... but with this behavior the specific attachments are attached within the "original message (i.e. template + common attachments)

How can I attach specific attachments without modifying the "original message" ?? how can I send $message "by value" to my function so I have a COPY of the object and not a reference on this object ?

I've tried "clonning" in PHP5 without success :

Code: Select all

private function doSendEmail($destinataires, $message, $from)
	{

		$current_message = new Swift_Message();
		$current_message = clone $message;
...
...

       }
But it doesnt work :

Code: Select all

$current_message->attach(...)
still modify the "original message" i.e. the message containing the template body + common attachments ..


Any ideas ? please ;)

Posted: Mon Oct 01, 2007 7:20 am
by chuckl
PHP will happily pass by value or by reference. Whether you REALLY want to do this is another matter entirely.

Are you saying that once a specific customer attachment is added, it stays as part of the message for all the remaining customers?
Can you not build the message for each customer?

i.e.

Instead of

Create message

Loop through customers, sending message and adding special attachments.

use

Loop through customers, assembling each customers message and send.

Posted: Tue Oct 02, 2007 7:45 am
by kondor
Excatly ! how can I work on a copy of message while looping on recipients .. without modify the original SwiftMessage object ?

THanks ;)

Posted: Tue Oct 02, 2007 8:31 am
by Chris Corbyn
kondor wrote:Excatly ! how can I work on a copy of message while looping on recipients .. without modify the original SwiftMessage object ?

THanks ;)
Can't you just make a new object and overwrite the old one? If you want to use clone you'll need to implement a __clone() method. That's extremely compilcated in this case however because the use of references in Swift_Message is pretty intense. I'd probably have to add the __clone() method to Swift_Message_Mime and Swift_Message myself.