将WordPress模板用于HTML电子邮件

时间:2014-09-30 作者:Malibur

我正在开发一个插件,需要在提交表单后发送电子邮件。

我正在使用wp_mail() 对于这个,它工作得很好。我的问题是,在我的代码中,HTML是由一组PHP字符串添加到如下变量中生成的:

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

..etc
我现在有30多行这样的话;这让我最终可以做到:

    //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"; \'));
如果我可以引用一个使用普通WordPress主题模板标记的单独文件来生成邮件的内容,那么我更愿意这样做,这样在一个单独的文件中,我会有如下内容:

<!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">
    <head>
        (headertags)
    </head>
    <body>
        <h1><?php the_title($post_id); ?></h1>
        <p><?php my_custom_template_body_tag(); ?></p>
    </body>
</html>
但我不知道如何将内容返回到wp_mail() 作用我试过使用file_get_contents() 但这只是忽略了PHP生成的内容,我已经研究了Herdeoc语法。但我发现这很难看,而且容易出错。我还有其他选择吗。我真的很喜欢这样做:

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

2 个回复
最合适的回答,由SO网友:ifdion 整理而成

您应该使用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);
template/email-header.php, 您可以使用

<html xmlns="http://www.w3.org/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 ?>

SO网友:Aron

您可以做一些更像合并字段的事情。这样,您就可以通过使用带有占位符的电子邮件模板并在其上运行字符串替换来保持html和PHP的分离。如下所示:

HTML

<!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">
    <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 ...

结束

相关推荐

1 post, 2 templates

我想能够呈现两种不同的风格(2个模板)后。例如,假设我有post ID 133,我希望有两个url来访问它,因此它会呈现不同模板应用的位置。洛雷姆。com/render1/133。com/render2/1333,例如。。。或者可能是这样的:洛雷姆。com/post/133和lorem。com/post/133?模板=2您将如何操作?