Page 1 of 1

[SOLVED] Decorator plugin & Swift_Message_Part....?

Posted: Thu May 10, 2007 7:08 am
by dillion
I might be missing something simple but I am trying to keep a template file as content-free as possible, e.g. used as a layout only. The template has a few placeholders like {NAME}, {MESSAGE}, etc.

Two quick queries:
1- Why are the placeholders in the template not replaced with values?
2- $subject = "{NAME} - subject" with new Swift_Message($subject); does not work but $subject = " - subject" with new Swift_Message("{NAME}" . $subject); does.

A code snippet is below:

Code: Select all

$subject = "{NAME} - subject";

//Start Swift
	$swift =& new Swift(new Swift_Connection_SMTP("host.com"));
	 
	//Create the message
	$message  =& new Swift_Message($subject);
	
	$template = new Swift_File("../template.php");
	
	$message->attach(new Swift_Message_Part($template, "text/html"));

	$replacements = array(
		"me@domain.com" => array("{NAME}" => "Name", "{MESSAGE}" => "this is a message body", "{FOOTER}" => "This is a footer")
	);
		
	//Load the plugin with these replacements
	$swift->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");
	
	 
	//Try sending the email
	if($swift->send($message, "me@email.com", "from@domain.com")) echo "Email sent";
	
	//Disconnect from SMTP, we're done
	$swift->disconnect();
Many thanks in advance!

Posted: Thu May 10, 2007 9:25 am
by Chris Corbyn
I'll comment in various thing while I have chance ;)

Your template file ends with .php but any PHP in it will not be executed, just FYI.
The decorator plugin can only operate on strings currently, so your template (part) will not be replaced unless you use file_get_contents() instead.

As for the comment about the way you pass subject there must be something going wrong before it ever gets to Swift since that's just basic PHP string concatenation you're doing. Swift doesn't care *how* you pass $subject, it just sees whatever is stored in it ;)

Code: Select all

//Create the message
        $message  =& new Swift_Message($subject);
       
        $template = file_get_contents("../template.php");
       
        $message->attach(new Swift_Message_Part($template, "text/html"));

Posted: Thu May 10, 2007 10:10 am
by dillion
Chris, many thanks for your response! Also $subject issue is sorted, I had {NAME } (note the space!) - I knew there was something silly somewhere (due to lack of coffee perhaps?). :D

Btw, it was a habit to create new files with .PHP - there are no PHP scripts in it although I might just rename it to plain HTML....

cheers again.

Posted: Thu May 10, 2007 10:48 am
by dillion
one quick question:

Code: Select all

$replacements = array(
	"email@domain.com" => array("{INTRO}" => $intro, "{MESSAGE}" => $message_body, "{FOOTER}" => $footer),
	"email2@domain.com" => array("{INTRO}" => $intro2, "{MESSAGE}" => $message_body2, "{FOOTER}" => $footer2)
);

$swift->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");

$sent = $swift->send($message, "email@domain.com", "me@domain.com");
	
$sent = $swift->send($message, "email2@domain.com", "me@domain.com");
Is the code "good"? My fear is that the above may work well but:

Code: Select all

$replacements = array(
	"email@domain.com" => array("{INTRO}" => $intro, "{MESSAGE}" => $message_body, "{FOOTER}" => $footer),
	[b]"email@domain.com"[/b] => array("{INTRO}" => $intro2, "{MESSAGE}" => $message_body2, "{FOOTER}" => $footer2)
);

$swift->attachPlugin(new Swift_Plugin_Decorator($replacements), "decorator");

$sent = $swift->send($message, "email@domain.com", "me@domain.com");
	
$sent = $swift->send($message, "email@domain.com", "me@domain.com");
The 2nd array item will replace the first array item. What's the best way to ensure that each array will ALWAYS be different even if the destinations are identical?

cheers again :)

Posted: Thu May 10, 2007 10:58 am
by Chris Corbyn
I'm not sure why you would ever have two of the same address.

When you get all the addresses, try using array_unique() or something.

Posted: Thu May 10, 2007 11:35 am
by dillion
d11wtq wrote:I'm not sure why you would ever have two of the same address.

When you get all the addresses, try using array_unique() or something.
Sorry my previous query was not clear - I meant if you were filling in a contact form (for testing or demonstration for example) that sends to yourself. You would have expected two different email content.

Problem is that for replacements to work correctly you need unique keys so how do you pass these keys to swift (instead of using email address a key in the array for example)?

Posted: Thu May 10, 2007 11:48 am
by Chris Corbyn
Ah, I see. That's not really possible with just the one message if you use the default Replacements class. You'd have to send the message twice explicitly. You could implement your own Replacements class but then you'd need to have some way of figuring out which template you want based purely on the address.

See the section on extending the replacement functionality: http://www.swiftmailer.org/wikidocs/v3/ ... /decorator

Might not be much use though, it's probably easier to just manually copy the email to yourself.

Posted: Thu May 10, 2007 4:55 pm
by dillion
Ah shame but like you said it is a bit pointless sending two emails to one person using Decorator.

Although if I find a way to extend your class or rather a trick, I will post it here if interested. :)

Posted: Thu May 10, 2007 7:05 pm
by Chris Corbyn
dillion wrote:Ah shame but like you said it is a bit pointless sending two emails to one person using Decorator.

Although if I find a way to extend your class or rather a trick, I will post it here if interested. :)
I could come up with ways to do it but they'd never appear in the actual release. For example, exteding the decorating you could keep track of how many times you been asked for replacements for an address and offer different replacements based on that number of times :) Hopefully you'll see a way around it anyway. Swift is really simply supposed to provide units of code you can use to make things simpler... ;)