使用 WordPress 通过 smtp 插件发送 html 邮件
Using WordPress to send html mails with the smtp plugin
我想在我的用户要在我的网站上注册时向他们发送 html 邮件,我已经在函数 "wp_new_user_notification" 中编写了这些代码。
$message = '<html><body>';
$message .= '<img src="//liux.so/mail/pic.jpg" alt="\r\n" />';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>username:</strong> </td><td>" . strip_tags($user->user_login) . "</td></tr>";
$message .= "<tr><td><strong>password:</strong> </td><td>" . strip_tags($plaintext_pass) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
发了一封测试邮件到自己的邮箱后,惊奇的发现整个邮件都是html代码!然后我在 Google 上搜索这个问题。它说我应该在 "message" 前面添加 "header"。就像...
mail($to, $subject, $message, $headers);
那么问题就到这里了!我已经安装了一个插件,它允许我通过 smtp 服务发送邮件。而这个插件应该这样写代码...
wp_mail($user->user_email, sprintf(__('[%s] Welcome!”'), $blogname), $message);
我不知道如何将 "header" 放入 "wp_mail" 函数中,或者只是让 html 代码起作用...
添加过滤器以发送 html 电子邮件
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
function set_html_content_type() {
return 'text/html';
}
之后您将能够发送 html 格式的电子邮件。
此代码必须在您的 functions.php 或任何插件中
@shahrukh 的回答完全正确。您应该将内容类型设置为 html,这将解决您的问题。但它可能与其他插件或默认的 wordpress 功能有一些冲突。所以你应该做的是在发送邮件之前添加过滤器,并在发送邮件之后移除过滤器。
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
wp_mail( 'To Email', 'Subject', '<h1>Html Email</h1>' );
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
function set_html_content_type() {
return 'text/html';
}
我想在我的用户要在我的网站上注册时向他们发送 html 邮件,我已经在函数 "wp_new_user_notification" 中编写了这些代码。
$message = '<html><body>';
$message .= '<img src="//liux.so/mail/pic.jpg" alt="\r\n" />';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>username:</strong> </td><td>" . strip_tags($user->user_login) . "</td></tr>";
$message .= "<tr><td><strong>password:</strong> </td><td>" . strip_tags($plaintext_pass) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
发了一封测试邮件到自己的邮箱后,惊奇的发现整个邮件都是html代码!然后我在 Google 上搜索这个问题。它说我应该在 "message" 前面添加 "header"。就像...
mail($to, $subject, $message, $headers);
那么问题就到这里了!我已经安装了一个插件,它允许我通过 smtp 服务发送邮件。而这个插件应该这样写代码...
wp_mail($user->user_email, sprintf(__('[%s] Welcome!”'), $blogname), $message);
我不知道如何将 "header" 放入 "wp_mail" 函数中,或者只是让 html 代码起作用...
添加过滤器以发送 html 电子邮件
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
function set_html_content_type() {
return 'text/html';
}
之后您将能够发送 html 格式的电子邮件。 此代码必须在您的 functions.php 或任何插件中
@shahrukh 的回答完全正确。您应该将内容类型设置为 html,这将解决您的问题。但它可能与其他插件或默认的 wordpress 功能有一些冲突。所以你应该做的是在发送邮件之前添加过滤器,并在发送邮件之后移除过滤器。
add_filter( 'wp_mail_content_type', 'set_html_content_type' );
wp_mail( 'To Email', 'Subject', '<h1>Html Email</h1>' );
remove_filter( 'wp_mail_content_type', 'set_html_content_type' );
function set_html_content_type() {
return 'text/html';
}