Advice needed using PHP variables in html email

Hello,

I am developing a site for a client that has an application form and in short I am trying to use a php file to gather the submitted data and to then email the data within a structered layout set by html.

I am not having any problems with gathering the data, I am unable to pass the data through with the html. I have set up a variable named body.

$body = ’ <html> <head></head><body>//I have put my html code here and then within the html code winthin tables I am trying to display or print the data items submitted by the form. I tried to use short tags or full tags eg. <?=$one_first_name?> but that did not work.//</body></html> ';

The php $body variable is used within the mail() function and I have included the html php headers.

You could also use an external template. For example save the outline of your email in a standard HTML file but add tags in where you want specific information to go.


<html>
<body>
Dear __NAME__

Thank you for your order.
<body>
</html>

You could then read that in to your PHP script and use str_replace to put the contents of your variables in the correct place


$fp = fopen("email/customer_message.htm", 'rb');

$template = fread($fp,10024);

fclose($fp);

$template = str_replace("__NAME__",$one_first_name,$template);

$template will than contain your original email with the tags replaced with the content you have captured from the form.
That should make it a lot easier to maintain or change your email template at a later date.

I save such templates in the database and allow client to change the contents.
I also prediefine a set of variables for each email template.

That way, client knows what he can add in that template to make it personal etc.

Kind Regards,

Off Topic:

Hey greenmedia, nice to see yet another local here at SitePoint!

I agree with GreenMedia, try to keep these templates away from your application code, chances are, sometime in the near future your going to have to let a ‘Designer’ near 'em. :stuck_out_tongue:

Try this:


<?php
$one_first_name = 'Test variable!';

// Single-quote style
$body = '<html><head></head><body>'.$one_first_name.'</body></html>';
echo $body;

// OR double-quote style
$body = "<html><head></head><body>$one_first_name</body></html>";
echo $body

?>