【问题标题】:wp_email template unable to changewp_email 模板无法更改
【发布时间】:2015-12-16 12:16:19
【问题描述】:

我创建了一个用于 wp_email() 函数的基本 html 模板。 这是模板代码:

<?php

$opties = json_decode($array_opties);


$message .= '
<p>'. date("Y-m-d H:i:s") .'</p>
<p>'. $titel .'</p>
<p>'. $uitvoering_field .'</p>
<p>'. $vermogen_gewicht_gewicht .'</p>
<p>'. $vermogen_gewicht_type .'</p>
<p>'. $vermogen_gewicht_kw .'</p>
<p>'. $vermogen_gewicht_prijs .'</p>
<p><img src="'. $kleurafbeelding .'" /></p>
<p>'. $kleurtitel .'</p>
<p>'. $kleurprijs .'</p>
<p><img src="'. $bekledingafbeelding .'" /></p>
<p>'. $bekledingtitel .'</p>
<p>'. $bekledingprijs .'</p>
<p><img src="'. $velgenafbeelding .'" /></p>
<p>'. $velgentitel .'</p>
<p>'. $velgenprijs .'</p>
<table class="table table-hover table-bordered">
        <thead>
                <tr>
                        <th>Optiepakket</th>
                        <th>Omschrijving</th>
                        <th>Prijs</th>
                </tr>
        </thead>
';
foreach ($opties as &$optie) {
$message .= '
        <tr>
            <td>'. $optie->titel .'</td>
            <td width="250">'. $optie->omschrijving .'</td>
            <td>'. $optie->prijs .'</td>
        </tr>
        ';
}



$message .= '
</table>
<p>'. $bedrijf .'</p>
<p>'. $naam .'</p>
<p>'. $adres .'</p>
<p>'. $postcode .'</p>
<p>'. $plaats .'</p>
<p>'. $telefoon .'</p>
<p>'. $mobiel .'</p>

';

这是我用来发送电子邮件的代码:

                $located = locate_template('mail.php');
            if(!empty($located)){
                    include $located;
            } else {
                    include (ABSPATH. 'wp-content/plugins/quido/frontend/mail.php');
            }
    $email_ontvangers = array('example@email.com', 'example@email.com');
    $headers = 'From: from <email@example.com>' . "\r\n";
    wp_mail( $email_ontvangers, 'text', $message, $headers );

这一切正常,但我需要在电子邮件中添加一些字符串,所以我将它们添加到带有 &lt;p&gt;&lt;/p&gt; 元素的行中。但这并没有改变电子邮件。然后我在桌子上添加了一个表头。如您所见,我添加了第三个标签,这并没有改变电子邮件。然后我只是删除了文件中的所有内容,这并没有改变电子邮件。所以我有点迷路了。我还尝试更改 wp_mail 使用我的模板的路径。所以我将“mail.php”更改为模板的完整路径,但也没有成功。那么我该如何解决呢?

【问题讨论】:

    标签: php wordpress email templates


    【解决方案1】:

    默认内容类型是'text/plain',不允许使用 HTML。您可以使用以下任一方式设置电子邮件的内容类型 'wp_mail_content_type' 过滤器或包含标题 “内容类型:文本/html”。小心重置'wp_mail_content_type' 但是,在您发送消息后返回“文本/纯文本”,因为 不这样做可能会导致来自电子邮件的意外问题 WP 或插件/主题。

    add_filter( 'wp_mail_content_type', 'set_content_type' );
    function set_content_type( $content_type ) {
        return 'text/html';
    }
    

    https://codex.wordpress.org/Function_Reference/wp_mail https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_mail_content_type

    【讨论】:

    • 我的插件文件夹中有一个 function.php(所有邮件都在其中),还有一个来自我的 wordpress 模板。在 wordpress 模板 function.php 中添加了以下代码: add_filter( 'wp_mail_content_type', function($content_type){ return 'text/html'; });我不知道我是否也应该将它添加到插件函数文件中?此外,这可能不会更改电子邮件,因为即使我如上所述删除模板中的所有代码,我的电子邮件也会正常发送,那么这可行吗?我现在尝试将它添加到我的插件中。
    • 我对此进行了测试,但它不工作,我使用的 set_content_type 做同样的事情。
    【解决方案2】:

    我解决了这个问题。是因为头部没有包含这行代码:

    $headers = 'From: From <example@email.com>' . "Content-Type: text/plain; charset=\"" ."\r\n";
    

    【讨论】:

      猜你喜欢
      • 2015-05-01
      • 2013-05-11
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 2012-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多