wp_mail wordpress html风格没有aplying

时间:2016-02-23 10:44:09

标签: php css wordpress styles html-email

我试图从我的wordpress插件发送邮件,但是当我检索它时,它没有样式或图像。

我这样做:

<?php
$headers = array(
    "From: Enara <no-reply@enara.es>",
    "MIME-Version: 1.0",
    "Content-type: text/html; charset=UTF-8'"
);
$message = "<html>
    <head>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
    </head>
    <body>
        <div style='background-color: #16a085;'>
            <table>
                <tr><td colspan='2'><img style=\'margin-botton: 20px;\' src='http://enara.litrodeenergia.com/wp-content/uploads/2015/12/enara-white.png' /></td></tr>
                <tr><td colspan='2'>DATOS DE CLIENTE</td></tr>
                <tr><td colspan='2'>$particular</td></tr>
                <tr>
                    <td>Nombre:</td>
                    <td>$nombre<br></td>
                </tr>
                <tr>
                    <td>Apellidos:</td>
                    <td>$apellidos</td>
                </tr>
                <tr>
                    <td>Nif:</td>
                    <td>$nif</td>
                </tr>
                <tr>
                    <td>Tel&eacute;fono:</td>
                    <td>$telefono</td>
                </tr>
            </table>
        </div>
    </body>
</html>";

$result = new stdClass();
$result->mailOk = wp_mail($to, $subject, $message, $headers);

我在脑海中尝试了一个样式代码,删除了html / head / body块(只发送了div html代码),将内容类型更改为iso,并使wp过滤器{{1} }&#39; text / html&#39;。

图片也不起作用。

如果我使用devtools调试收到的电子邮件,我会看到:

wp_mail_content_type

电子邮件标题:

<div style="background-color:#16a085">
<table>
<tbody>
    <tr>
        <td colspan="2"><img src="http://enara.litrodeenergia.com/wp-content/uploads/2015/12/enara-white.png" style=""></td>
    </tr>
    <tr>
        <td colspan="2">DATOS DE CLIENTE</td>
    </tr>
    <tr>
        <td colspan="2">Usuario particular</td>
    </tr>
    <tr>
        <td>Nombre:</td>
        <td>afsd<br>
    </td>
    </tr>
    <tr>
        <td>Apellidos:</td>
        <td></td>
    </tr>
    <tr>
        <td>Nif:</td>
        <td></td>
    </tr>
    <tr>
        <td>Teléfono:</td>
        <td></td>
    </tr>
</tbody>
</table>

2 个答案:

答案 0 :(得分:0)

尝试添加

add_filter('wp_mail_content_type',create_function('', 'return "text/html"; '));

默认情况下似乎wp_mail()会返回text/plain内容。

  

您可以使用WordPress的wp_mail()函数从WordPress网站发送电子邮件。但是,默认内容类型是“text / plain”,它不允许使用HTML。如果您要发送HTML电子邮件,则需要使用“wp_mail_content_type”过滤器将电子邮件的内容类型设置为“text / html”。

来源:How to Send HTML Emails From WordPress

修改

问题似乎与邮件客户端呈现HTML内容有关。

答案 1 :(得分:0)

如果你把代码放在wp_mail_content_type

的代码中,那可能会更好

您应该在调用 wp_mail()之前过滤内容类型,例如:

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

wp_mail( 'me@example.net', 'The subject', '<div>The message</div>' );