3
\$\begingroup\$

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:

'<p>'.
'</p>'.
'<span>'.
'<h4>'.
'</h4>';

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">'.
        '<head>'.
        '  <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"/>'.
        '</head>'.
        '<body style="margin: 0; padding: 0;">'.
        ' <table border="0" cellpadding="0" cellspacing="0" width="100%">'.
        '<tr>'.
        '<td>'.
        '<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">'.
        '<tr>'.
        '<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>'.
      '</tr>'.
      '<tr>'.
      '<td bgcolor="#ffffff" style="padding: 40px 30px 40px 30px; font-family: "Dosis", sans-serif;">'.
      '<table border="0" cellpadding="0" cellspacing="0" width="100%">'.
      '<tr>'.
      '<td>';
      echo "Hello there hero!";
      '</td>'.
      '</tr>'.
      '<tr>'.
      '<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?";
      '</td>'.
      '</tr>'.
      '<tr>'.
      '<td>'.
      '<table border="0" cellpadding="0" cellspacing="0" width="100%">'.
      '<tr>'.
      '<td width="260" valign="top">'.
      '<tr>'.
      '<td>'.
      '<div class="button">';
      echo "Verify Account!";
      '</div>'.
      '</td>'.
      '</tr>'.
      '</table>';
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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

<h2>%variable1%</h2>
<p>%variable2%</p>

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
\$\endgroup\$
2
  • \$\begingroup\$ Thanks for your answer! Really helpful! Could you, however explain the str_replace? \$\endgroup\$ Commented 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
    Commented 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.