Jump to content


Photo

trouble including php in an email


  • Please log in to reply
2 replies to this topic

#1 darga333

darga333
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 26 April 2006 - 11:37 PM

This is a bit interesting..

I have two files:

1. The script to gather all of the email addresses and gets the data that is going to be included in the template.
2. The template of the email which includes php variables

I have done it this way because I want to keep my email template separate from the code

all of the data that i want to email is included in mailer_template.php.

You would think hey this is easy.. set $message = file_get_contents('mailer_template.php');
and then just type mail("$email","$subject","$message","$headers");

but that doesnt work because there are php variables inside mailer_template.php (that arent getting displayed in the emails).

the goal here is to have mailer_template.php to be able to include php variables

how do you assign mailer_template.php to the variable $message so that it still includes data from the database?



#2 KrisNz

KrisNz
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • LocationAuckland, New Zealand

Posted 26 April 2006 - 11:56 PM

If you want to keep your code separate from your template then you shouldn't have php in it. Use placeholders in your template for the pieces of the email that are going to change.

e.g
#template.html
Dear %NAME%, thanks for registering with %SITENAME%

#code

$personsName = "Bob";
$siteName = "foo.bar";
$body = file_get_contents("template.html");
$body = str_replace("%NAME%",$personsName,$body);
$body = str_replace("%SITENAME%",$siteName,$body);



#3 darga333

darga333
  • Members
  • PipPipPip
  • Advanced Member
  • 53 posts

Posted 27 April 2006 - 12:29 AM

Hey thank you so much! I had no idea that you could even do that!

It works now!

[!--quoteo(post=369073:date=Apr 26 2006, 07:56 PM:name=KrisNz)--][div class=\'quotetop\']QUOTE(KrisNz @ Apr 26 2006, 07:56 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
If you want to keep your code separate from your template then you shouldn't have php in it. Use placeholders in your template for the pieces of the email that are going to change.

e.g
#template.html
Dear %NAME%, thanks for registering with %SITENAME%

#code

$personsName = "Bob";
$siteName = "foo.bar";
$body = file_get_contents("template.html");
$body = str_replace("%NAME%",$personsName,$body);
$body = str_replace("%SITENAME%",$siteName,$body);

[/quote]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users