Page 1 of 1

passing form variables to email template

Posted: Fri Apr 18, 2008 1:29 pm
by palaina
hi all.

depending on the radio button selection users pick in my form, they will get a separate email message (template, if you will). i currently have it set up like this:

Code: Select all

 
require_once "_swift/lib/Swift.php";
require_once "_swift/lib/Swift/Connection/SMTP.php";
 
$swift =& new Swift(new Swift_Connection_SMTP("localhost"));
 
 if($rdo_val == "string example one") {
$message =& new Swift_Message("subject one");
$message->attach(new Swift_Message_Part("<p>hello user, thanks for choosing " . $rdo_val . ".</p> <p>In nibh purus, feugiat ac, pellentesque eu, ornare sed, tortor. Praesent arcu " . $txt_one . " onec imperdiet egestas enim. Proin scelerisque " . $txt_two . " nisi ut leo. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vivamus</p> <img src=\"http://www.domain.tld/img/img-one.jpg\">","text/html"));
 }
 
 else if($rdo_val == "string example two") {
$message =& new Swift_Message("subject two");
$message->attach(new Swift_Message_Part("<p>hello user, thanks for choosing " . $rdo_val . ".</p> <p>In nibh purus, feugiat ac, pellentesque eu, ornare sed, tortor. Praesent arcu " . $txt_one . " onec imperdiet egestas enim. Proin scelerisque " . $txt_two . " nisi ut leo. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vivamus</p> <img src=\"http://www.domain.tld/img/img-two.jpg\">","text/html"));
 }
 
 
depending on the selection the user will get different images and a different message, and i'd rather not have all that html code in the script (i simplified the example for this question - there are more than two options), so i was wondering if i could somehow pass the information from the form to an external email template file (the file being php, of course). maybe something like this:

Code: Select all

 
 if($rdo_val == "string example one") {
$message =& new Swift_Message("subject one");
$message->attach(new Swift_Message_Part(new Swift_File("one.php"), "text/html")); //would i pass the variables in the Swift_File() function?
 }
 
 else if($rdo_val == "string example two") {
$message =& new Swift_Message("subject two");
$message->attach(new Swift_Message_Part(new Swift_File("two.php"), "text/html")); //would i pass the variables in the Swift_File() function?
 }
 
i could put all the differing html from the message in the script to send the mail, but i was thinking this would be way cleaner.

i read the documentation but didn't see anything that mentioned whether i could do this. was i looking in the right spot?

is this even possible?

Re: passing form variables to email template

Posted: Sat Apr 19, 2008 9:34 pm
by Chris Corbyn
Have a play around with this concept:

Code: Select all

$var1 = 'foo';
$var2 = 'bar';
 
ob_start(); //Prevent output from going to the browser
include 'template-file.php'; //This file has access to $var1 and $var2
$html = ob_get_clean(); //Get the contents of the buffer and restore back to normal
 
If you wrap that type of functionality in a function then you can pretty much invoke it by doing something like:

Code: Select all

$message->attach(new Swift_Message_Part(template('template-file.php', array('var1' => 'foo', 'var2' => 'bar'))));

Re: passing form variables to email template

Posted: Tue Apr 22, 2008 10:45 am
by palaina
hey chris, thanks for the tip!

looks like i will have to up my knowledge on output buffers before i dive too far into that. :)