1

I am a newbie coder trying to build a simple web app using PHP. I am trying to send an HTML email that has a variable that will change each time it is sent. The code to initiate the email is 'email.php' and contains:

$body = file_get_contents('welcome/green2.html.php');

Within the 'green2.html.php' file, I have a variable called $highlight that needs to be populated. The $highlight variable is defined within the 'email.php' file. I had tried to simply add within the 'green2.html.php' file, however it is not being parsed. I get a blank space where the variable should be when it is output.

Also, I have done an include 'welcome/green2.html.php' within the 'email.php' file. When I echo it, the $highlight var is shown on the resulting page, but not if I echo $body.

Any help would be much appreciated!

1
  • 2
    file_get_contents() does not execute code. It just slurps in the raw bytes and stuffs them into your variable. Perhaps you're looking for include() and/or (hopefully not) eval()? Commented Jul 27, 2012 at 19:33

3 Answers 3

5

Have you tried the str_replace function? http://php.net/manual/en/function.str-replace.php.

Add a placeholder in HTML (for instance #name# for name, #email# for email), and then use the string replace function once you've loaded the content of the file.

$bodytag = str_replace("#name#", $name, $myfile);

Sign up to request clarification or add additional context in comments.

Comments

1

Loading a file via file_get_contents() will not cause it to be parsed by PHP. It will simply be loaded as a static file, regardless of whether it contains PHP code or not.

If you want it to be parsed by PHP, you would need to include or require it.

But it sounds like you're trying to write a templating system for your emails. If this is what you're doing, you'd be better off not having it as PHP code to be parsed, but rather having placeholder markers in it, and then using str_replace() or similar functions to inject variables from your main program into the string.

Hope that helps.

Comments

0

Use http://php.net/manual/en/function.sprintf.php put a %s in your code instead of the variable read the content and put the string into the sprintf with the variable you want to put that's it. Hope this will help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.