PHP Mailer with HTML template and sending variables

PHP Mailer with HTML template and sending variables

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP Mailer Variable Substitution and Template Handling

When implementing email notifications using PHPMailer, a common requirement is to separate the HTML template from the logic that populates it. Developers often prefer editing .html files directly to change layouts or styles rather than modifying PHP code. The approach shown in your source material attempts this by loading a file, replacing string literals like %username% with actual values using str_replace, and then sending the result via SMTP.

However, the output you are seeing indicates that the substitution is not occurring as expected. Specifically, your email client displays %testusername% instead of testusername. This discrepancy arises from a mismatch between the template placeholders defined in the source file and the actual keys being passed during runtime.

In your sample_mail.html, you have defined the placeholders exactly as Username: %username%. Yet, within your index.php script, you are executing:

$message = str_replace('%testusername%', $username, $message);

You are searching for the string %testusername% (with a "t") in the HTML file, but that string does not exist there. The file contains %username%. Consequently, str_replace finds nothing to replace, leaving the literal string %testusername% intact if you had accidentally pasted the wrong variable name into your code, or simply failing to find the target if you strictly adhered to the mismatch shown above.

To resolve this, the placeholder in the HTML must match the search term exactly. If your intention is to use str_replace, ensure the keys align:

// Correct alignment based on the provided HTML content
$message = str_replace('%username%', $username, $message);
$message = str_replace('%password%', $password, $message);

While this direct string replacement method works for simple cases, it introduces fragility. If a developer updates the sample_mail.html file and adds a new image or changes a class name that coincidentally matches a variable name (e.g., changing a div ID to %username%), your PHP script might inadvertently inject sensitive data like passwords into the wrong part of the layout. This coupling between template structure and PHP logic is the primary trade-off of the str_replace approach.

A more robust architectural pattern for this requirement involves passing an associative array of variables to a dedicated rendering function, or using a templating engine that handles variable interpolation safely. For instance, you could define your data in a variable:

$data = [
    'username' => $username,
    'password' => $password
];

And then use str_replace against the array keys directly:

$message = str_replace(['%username%', '%password%'], $data, $message);

This ensures that only specific keys are replaced and prevents accidental collisions.

Regarding security, it is important to note that simply sending HTML via MsgHTML() does not render server-side variables within the browser. The email client receives raw text; it expects the PHP script to have already resolved those placeholders before transmission. If you were attempting to use Smarty or Twig templates, your current setup would require a specific library installation and configuration, as PHPMailer itself is just an SMTP transport layer.

If you are working within a Laravel ecosystem, consider leveraging the framework's built-in templating capabilities or packages designed for safe email rendering, such as laravel/framework's mailer which supports Blade syntax ({{ $username }}) out of the box without manual string manipulation. This eliminates the risk of injection and simplifies maintaining templates that are edited by non-developers.

Ultimately, if you must stick with the file-based replacement method, strict consistency between the placeholder definitions in the .html file and the keys used in the PHP str_replace calls is mandatory. Double-check every instance of a %variable% pattern against your variable assignments to ensure the substitution logic actually executes.

Our network