-3

I have a PHP file (mail-mail.php) that send some mails when a form passes him some infos, and works great.

All the mails were once inserted in the main file (mail-mail.php) like this

body = <<<BODY
-THE ENTIRE HTML CODE OF THE MAIL-
BODY;

File become very huge (mail-mail.php send 3-5 mails everytime that runs) very long (more than 500 lines), and not comfortable if i have to change the mail contents

So i decided to take the entire mail code out and replaced them with some external files with the HTML code of the mail.

$body = file_get_contents('./mail/inv.php');

Inside the HTML code there is a variable ($name) that in the <<

I tried to load the file separately in this way

$body = str_replace("$name", $name, file_get_contents('./mail/inv.php'));

or this way

$prebody = file_get_contents('./mail/inv.php');
$body = str_replace("$name", $name, $prebody);

but still the $prebody (and the $body of course) loads "inv.php" without changing the $name value.

Florian Lauterbach
  • 1,330
  • 1
  • 11
  • 25

2 Answers2

1

If you want to replace the text $variable you will need to either use singlequotes, or escape your variable character ($)

right now "$name" becomes "" and doesnt replace anything. singlequote strings are NOT parsed for variables.

See also: What is the difference between single-quoted and double-quoted strings in PHP?

Community
  • 1
  • 1
EJTH
  • 2,178
  • 1
  • 20
  • 25
0
$body = str_replace("$name", $name, $prebody);

Because you put $name in double quotes it is affected by variable parsing inside strings (i.e. it is replaced with the value of variable $name).

The line above is the same as:

$body = str_replace($name, $name, $prebody);

which means you try to replace the value you have in variable $name with itself. Of course, this returns an unchanged copy of $prebody.

Put $name in single quotes and it will work:

$body = str_replace('$name', $name, $prebody);
axiac
  • 68,258
  • 9
  • 99
  • 134