問題描述

有沒有一個類似的可以幫助我實現這一點的 action_hook?

我嘗試在 PHP 字符串變量中添加標記,並剛剛使用 wp_mail 函數發出電子郵件,如下所示:

$email_to = 'someaddress@gmail.com';
$email_subject = 'Email subject';
$email_body = "<html><body><h1>Hello World!</h1></body></html>";
$send_mail = wp_mail($email_to, $email_subject, $email_body);

但它顯示為明文嗎?

有任何想法嗎?

最佳解決方案

來自 wp_mail codex page

The default content type is ‘text/plain’ which does not allow using HTML. However, you can set the content type of the email by using the ‘wp_mail_content_type’ filter.

function wpse27856_set_content_type(){
    return "text/html";
}
add_filter( 'wp_mail_content_type','wpse27856_set_content_type' );

次佳解決方案

或者,您可以在 $ headers 參數中指定 Content-Type HTTP 頭:

$to = 'sendto@example.com';
$subject = 'The subject';
$body = 'The email body content';
$headers = array('Content-Type: text/html; charset=UTF-8');

wp_mail( $to, $subject, $body, $headers );

第三種解決方案

使用 wp_mail 功能後,請勿忘記刪除內容類型過濾器。遵循接受的答案命名,您應該在執行 wp_mail 後執行此操作:

remove_filter( 'wp_mail_content_type','wpse27856_set_content_type' );

在這裏檢查這張票 – 重置 content-type 以避免衝突 – http://core.trac.wordpress.org/ticket/23578

參考文獻

注:本文內容整合自 google/baidu/bing 翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:gxnotes#qq.com(#替換為 @) 。