I have used PHPmailer to send email to users upon registering to the site. Now, everything works as should, however, I find myself doing painstaking work adding the HTML elements in.

Here's an example piece of HTML code:

<p>This is a paragraph</p>
 <span>Now this is important!</span>
<h4>Uh Oh!</h5>

Now in the PHP class, I would have to '' and . after each HTML element, so for example:


As you could imagine, doing this for a whole XHTML page, can take a very long time. I was wondering if there's a method that's quicker and more efficient than my technique.

Here's my entire PHP class that includes the HTML elements:

        $mail->Body = 
        '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'.
        '<html xmlns="http://www.w3.org/1999/xhtml">'.
        '  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'.
        '  <title>Demystifying Email Design</title>'.
        ' <link href="https://fonts.googleapis.com/css?family=Dosis" rel="stylesheet" type="text/css"/>'.
        '  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>'.
        '<body style="margin: 0; padding: 0;">'.
        ' <table border="0" cellpadding="0" cellspacing="0" width="100%">'.
        '<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">'.
        '<td align="center" bgcolor="#70bbd9" style="padding: 40px 0 30px 0;">
      <img src="http://tutsplus.github.io/build-an-html-email-template-from-scratch/images/h1.gif" alt="Creating Email Magic" width="300" height="230" style="display: block;" />'.
      '<td bgcolor="#ffffff" style="padding: 40px 30px 40px 30px; font-family: "Dosis", sans-serif;">'.
      '<table border="0" cellpadding="0" cellspacing="0" width="100%">'.
      echo "Hello there hero!";
      '<td style="padding: 20px 0 30px 0;">';
      echo " Cheesy heading, we know. BUT! Thanks for signing up with us! We just need you to do one, tiny little thing! You see that button down there? The big yellow one? We're going to need you to click that.. Seems daunting, but trust us. Once you do, you'll get access to all our websites features! Easy right? So go ahead, what are you waiting for?";
      '<table border="0" cellpadding="0" cellspacing="0" width="100%">'.
      '<td width="260" valign="top">'.
      '<div class="button">';
      echo "Verify Account!";

1 Answer 1


You may use a template.

$message = file_get_contents('path/to/your/html/template.html');

Then replace parts of it applying variables

// Defining variables
$variable1 = 'This will be a heading';
$variable2 = 'This will be a paragraph. Look at me, I\'m awesome!';

// Replace placeholders with my variables
$message = str_replace('%variable1%', $variable1, $message); 
$message = str_replace('%variable2%', $variable2, $message);

And finally pass it to PHPmailer

$mail->Body = $message;

In your HTML template reference variables as part of your body, nothing more than


You probably already know, but I'd suggest anyway to specify that the message contains HTML

$mail->IsHTML(true);         // specify that it is html
$mail->CharSet = "utf-8";    // avoid encoding issues
  • \$\begingroup\$ Thanks for your answer! Really helpful! Could you, however explain the str_replace? \$\endgroup\$ Feb 18, 2016 at 12:13
  • \$\begingroup\$ Glad to be of help! It looks for whatever you pass to it as first argument and replaces it with whatever you pass as second, while the third is the string to look into. file_get_contents retrieves the content of your html template and put it on the variable $message as a string, so that we can work with it. \$\endgroup\$
    – phaberest
    Feb 18, 2016 at 13:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.