2

I am using this script to send an HTML email

  $to = '[email protected]';

  $subject = 'Test Email';

  $headers = "From: [email protected]\r\n";
  $headers .= "Reply-To: [email protected]\r\n";
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .= "Content-Type: text/html; charset=UTF-8\r\n";

  $message = $htmlContent = file_get_contents("email_template.html");

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

But I would like to add some variables like custom greeting with name etc. into the email. But I am not sure how to do that since I am using

$message = $htmlContent = file_get_contents("email_template.html");

To get the email content is because when I pasted the html content directly into the php code, the email got sent but the styling was broken for some reason...this way it works perfectly, but I don't know how to add variables.

Any help, please?

3
  • so, what you need is to pass variable to the template? (html file) Commented Jan 3, 2019 at 15:16
  • PHP code in a file doesn't get executed by file_get_contents. Commented Jan 3, 2019 at 15:24
  • maybe you can try preg_replace or str_replace() and do like <h1>hello [name]</h1> Commented Jan 3, 2019 at 15:29

1 Answer 1

6

One rudimentary solution would be to change your template to be a .php file, and have it echo some simple variables.

email_template.php

Hello <?php echo $firstName ?>,

This message is to notify you...

Now you can setup these variables in your script before you load the template.

Load it using include so that the PHP is evaluated, and capture the result using output buffering:

$firstName = "John";

ob_start();
include("email_template.php");
$message = ob_get_contents();
ob_end_clean();

Now your $message will contain the HTML email content with the dynamic first name variable.

This is a very basic solution.

Another option would be to use placeholders in your HTML template (like %first_name%) and to do string replacement.

Ultimately you may want instead to start looking at real templating options like TWIG. I also recommend you look at PHP mail libraries like SwiftMailer instead of using the raw mail function.

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

3 Comments

I've tried the include method, but this has resulted in the email_template.php being displayed when opening the php with this script and the email that it sent contained just the letter '1' and nothing else
@user1207524 - you'd need to change your email_template.php so that it returns a string containing the HTML: see example 5. This would allow you to assign the contents of the included file to a variable - otherwise include merely returns a boolean which is why you're getting 1 in the email (i.e. true).
@user1207524 refresh my answer, I've updated to show how you can capture the template output with buffering.

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.