XHTML input for PHPmailer 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:

&lt;p&gt;This is a paragraph&lt;/p&gt;
 &lt;span&gt;Now this is important!&lt;/span&gt;
&lt;h4&gt;Uh Oh!&lt;/h5&gt;


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

'&lt;p&gt;'.
'&lt;/p&gt;'.
'&lt;span&gt;'.
'&lt;h4&gt;'.
'&lt;/h4&gt;';


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-&gt;Body = 
        '&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;'.
        '&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;'.
        '&lt;head&gt;'.
        '  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;'.
        '  &lt;title&gt;Demystifying Email Design&lt;/title&gt;'.
        ' &lt;link href="https://fonts.googleapis.com/css?family=Dosis" rel="stylesheet" type="text/css"/&gt;'.
        '  &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"/&gt;'.
        '&lt;/head&gt;'.
        '&lt;body style="margin: 0; padding: 0;"&gt;'.
        ' &lt;table border="0" cellpadding="0" cellspacing="0" width="100%"&gt;'.
        '&lt;tr&gt;'.
        '&lt;td&gt;'.
        '&lt;table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;"&gt;'.
        '&lt;tr&gt;'.
        '&lt;td align="center" bgcolor="#70bbd9" style="padding: 40px 0 30px 0;"&gt;
      &lt;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;" /&gt;'.
      '&lt;/td&gt;'.
      '&lt;/tr&gt;'.
      '&lt;tr&gt;'.
      '&lt;td bgcolor="#ffffff" style="padding: 40px 30px 40px 30px; font-family: "Dosis", sans-serif;"&gt;'.
      '&lt;table border="0" cellpadding="0" cellspacing="0" width="100%"&gt;'.
      '&lt;tr&gt;'.
      '&lt;td&gt;';
      echo "Hello there hero!";
      '&lt;/td&gt;'.
      '&lt;/tr&gt;'.
      '&lt;tr&gt;'.
      '&lt;td style="padding: 20px 0 30px 0;"&gt;';
      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?";
      '&lt;/td&gt;'.
      '&lt;/tr&gt;'.
      '&lt;tr&gt;'.
      '&lt;td&gt;'.
      '&lt;table border="0" cellpadding="0" cellspacing="0" width="100%"&gt;'.
      '&lt;tr&gt;'.
      '&lt;td width="260" valign="top"&gt;'.
      '&lt;tr&gt;'.
      '&lt;td&gt;'.
      '&lt;div class="button"&gt;';
      echo "Verify Account!";
      '&lt;/div&gt;'.
      '&lt;/td&gt;'.
      '&lt;/tr&gt;'.
      '&lt;/table&gt;';
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-&gt;Body = $message;


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

&lt;h2&gt;%variable1%&lt;/h2&gt;
&lt;p&gt;%variable2%&lt;/p&gt;


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

$mail-&gt;IsHTML(true);         // specify that it is html
$mail-&gt;CharSet = "utf-8";    // avoid encoding issues