Passing variables into an email message located in a separate file

I want to send an HTML-based email with a message that contains variables (e.g. receiver name, date, etc).

However, the email message should be located in a separate file from the script which defines the variables and sends the email.

How can I do this?

This tutorial uses output buffering. The entire email message is still stored in the script.

For the sake of keeping my script tidy, I would like the contents of the html message to be stored in a separate file.

Let’s say for example, in my script, I define the variables ‘$name’ and ‘$date’.
And the separate file which serves as the message content of the email looks something like this:

'Hello '.$name.', today\\'s date is '.$date.'.'

In the script, the variable ‘$message’ should be exactly that, but replacing the variable names with the values earlier defined in the script…

$name = 'John Smith';
$date = 'October 4';

$message = ??? //This is where the other file is defined as the $message variable.

email($to, $subject, $message, $headers);

This tutorial should help

In the output buffer you can include php tags <?php ?>.

So if I understand correctly what you want, in the ob you could have


located at the appropriate location.