最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

templates - Using WordPress templating for HTML emails

matteradmin6PV0评论

I am working on a plugin that needs to send out an email after a form submission.

I am using wp_mail() for this and it works fine. My problem is that in my code the HTML is generated by a bunch of PHP strings being added to a variable like so:

$content = $html_headers;
$content .= '<h1>' . the_title($post_id) . '</h1>';
$content .= '<p>' . $post_body . '</p>;

..etc

I currently have more than 30 lines like that; and that allows me to finally do:

    //add filter to allow html
    add_filter('wp_mail_content_type', create_function('', 'return "text/html"; '));

    //Send email
    wp_mail( '[email protected]', 'mail tester', $content, 'From: some one <[email protected]>' );

    //remove filter to allow html (avoids some conflict.)
    remove_filter('wp_mail_content_type', create_function('', 'return "text/html"; '));

I would prefer it if I could reference a separate file that uses normal WordPress theme template-tags in order to generate the content of the mail, so that in a separate file I'd have something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ".dtd">
<html xmlns="">
    <head>
        (headertags)
    </head>
    <body>
        <h1><?php the_title($post_id); ?></h1>
        <p><?php my_custom_template_body_tag(); ?></p>
    </body>
</html>

but I don't know how to then return that content to the wp_mail() function. I have tried using file_get_contents() but that just ignores PHP generated content, and I've looked into the heredoc syntax. But I find that quite ugly and error prone. Do I have any other options. I really love it if I could do something like this:

$content = parse_and_return_content_of('path/to/template/file', $arg);

Thank you

I am working on a plugin that needs to send out an email after a form submission.

I am using wp_mail() for this and it works fine. My problem is that in my code the HTML is generated by a bunch of PHP strings being added to a variable like so:

$content = $html_headers;
$content .= '<h1>' . the_title($post_id) . '</h1>';
$content .= '<p>' . $post_body . '</p>;

..etc

I currently have more than 30 lines like that; and that allows me to finally do:

    //add filter to allow html
    add_filter('wp_mail_content_type', create_function('', 'return "text/html"; '));

    //Send email
    wp_mail( '[email protected]', 'mail tester', $content, 'From: some one <[email protected]>' );

    //remove filter to allow html (avoids some conflict.)
    remove_filter('wp_mail_content_type', create_function('', 'return "text/html"; '));

I would prefer it if I could reference a separate file that uses normal WordPress theme template-tags in order to generate the content of the mail, so that in a separate file I'd have something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3/1999/xhtml">
    <head>
        (headertags)
    </head>
    <body>
        <h1><?php the_title($post_id); ?></h1>
        <p><?php my_custom_template_body_tag(); ?></p>
    </body>
</html>

but I don't know how to then return that content to the wp_mail() function. I have tried using file_get_contents() but that just ignores PHP generated content, and I've looked into the heredoc syntax. But I find that quite ugly and error prone. Do I have any other options. I really love it if I could do something like this:

$content = parse_and_return_content_of('path/to/template/file', $arg);

Thank you

Share Improve this question edited Apr 14, 2019 at 12:14 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Sep 30, 2014 at 13:13 MaliburMalibur 5891 gold badge5 silver badges19 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 5

You should use ob_get_contents()

    ob_start();
    include('template/email-header.php');
    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'));
    include('template/email-footer.php');
    $message = ob_get_contents();
    ob_end_clean();
    wp_mail($user_email, $email_subject, $message);

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

<html xmlns="http://www.w3/1999/xhtml">
<head>
    <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>
</head>
<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:

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3/1999/xhtml">
    <head>
        (headertags)
    </head>
    <body>
        <h1>[POST.TITLE]</h1>
        <p>[POST.CONTENT]</p>
    </body>
</html>

PHP

$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 ...
Post a comment

comment list (0)

  1. No comments so far