Wordpress - Using WordPress templating for HTML emails

You should use ob_get_contents()

    printf(__('<p>A very special welcome to you, %1$s. Thank you for joining %2$s!</p>', 'cell-email'), $greetings, get_bloginfo('name'));
    printf(__('<p> Your password is <strong style="color:orange">%s</strong> <br> Please keep it secret and keep it safe! </p>', 'cell-email'), $plaintext_pass);
    printf(__('<p>We hope you enjoy your stay at %s. If you have any problems, questions, opinions, praise, comments, suggestions, please feel free to contact us at any time</p>', 'cell-email'), get_bloginfo('name'));
    $message = ob_get_contents();
    wp_mail($user_email, $email_subject, $message);

And on the template/email-header.php, you can use

<html xmlns="http://www.w3.org/1999/xhtml">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta property="og:title" content="<?php echo $email_subject ?>" />
    <title><?php echo $email_subject ?></title>
<body leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" offset="0" style="width: 100% !important; -webkit-text-size-adjust: none; margin-top: 0; margin-right: 0; margin-bottom: 0; margin-left: 0; padding-top: 0; padding-right: 0; padding-bottom: 0; padding-left: 0; background-color: #FAFAFA;" bgcolor="#FAFAFA">
<!-- the rest of the html here -->
<?php // and php generated content if you prefer ?>

You could do something a little more like merge fields. That way you can keep your html and PHP separated by using an email template with placeholders and run a string replace on them. Something like the following:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


$html_email_template_file = 'some/path/mytemplate-example.html';

// assign contents of file to $content var
$content = file_get_contents($html_email_template_file);

$content = str_replace('[POST.TITLE]', $post->post_title, $content);
$content = str_replace('[POST.CONTENT]', $post->post_excerpt, $content);

// send your email here ...