钩子文档

wp_mail_content_type

💡 云策文档标注

概述

wp_mail_content_type 是一个用于过滤 wp_mail() 函数邮件内容类型的钩子。默认内容类型为 'text/plain',但可通过此钩子更改为 'text/html' 或其他 MIME 类型,以支持 HTML 邮件发送。

关键要点

  • wp_mail_content_type 钩子允许修改 wp_mail() 发送邮件的默认内容类型,从 'text/plain' 切换到 'text/html' 等。
  • 使用 add_filter() 添加回调函数来设置内容类型,例如返回 'text/html' 以发送 HTML 邮件。
  • 注意:更改内容类型可能影响密码重置邮件等系统邮件,建议在发送自定义邮件后使用 remove_filter() 重置或调整邮件内容以兼容 HTML 格式。
  • 替代方法:可通过在 wp_mail() 的 $headers 参数中直接设置 Content-Type 头来指定内容类型,无需使用钩子。

代码示例

// 使用钩子设置 HTML 邮件内容类型
add_filter( 'wp_mail_content_type', function( $content_type ) {
    return 'text/html';
} );

// 发送邮件后重置内容类型以避免冲突
remove_filter( 'wp_mail_content_type', 'set_content_type' );

// 直接在 $headers 中设置内容类型
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
wp_mail( $to, $subject, $body, $headers );

注意事项

  • 将内容类型更改为 'text/html' 可能导致密码重置邮件显示问题,需通过过滤 retrieve_password_message 或重置内容类型来解决。
  • 建议在发送自定义 HTML 邮件后及时移除钩子,以防止影响其他邮件发送功能。

📄 原文内容

Filters the wp_mail() content type.

Parameters

$content_typestring
Default wp_mail() content type.

More Information

  • The default content type for email sent through the wp_mail() function is ‘text/plain‘ which does not allow using HTML. However, you can use the wp_mail_content_type filter to change the default content type of the email.
  • In general, content type is going to be ‘text/plain‘ as the default, or ‘text/html‘ for HTML email; but other MIME types are possible.

Source

$content_type = apply_filters( 'wp_mail_content_type', $content_type );

Changelog

Version Description
2.3.0 Introduced.

User Contributed Notes

  1. Skip to note 5 content

    Switch to HTML formatted email when using <a href="https://developer.wordpress.org/reference/functions/wp_mail/" rel="nofollow">wp_mail()</a>:

    /**
     * Filter the mail content type.
     */
    function wpdocs_set_html_mail_content_type() {
    	return 'text/html';
    }
    add_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
    
    $to      = 'sendto@example.com';
    $subject = 'The subject';
    $body    = 'The email body content';
    
    wp_mail( $to, $subject, $body );
    
    // Reset content-type to avoid conflicts -- <a href="https://core.trac.wordpress.org/ticket/23578" rel="nofollow ugc">https://core.trac.wordpress.org/ticket/23578</a>
    remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );

  2. Skip to note 6 content

    Follow the bellow code snippet to set wp_mail(); content type without using add_filter( 'wp_mail_content_type', 'your_function_name' );.

    To send HTML formatted mail, you can specify the Content-Type HTTP header in the $headers parameter:

    $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 );

    That’s all about it. Thank you!

  3. Skip to note 7 content

    Examples migrated from Codex:

    The following example will change the default content (mime) type for the wp_mail() function to ‘text/html’:

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

    The following example shows that it is not necessary to call another method if you can use anonymous functions (PHP 5.3.0+):

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

    The following example shows that you could use different MIME types for different purposes by building some conditional logic into your filter:

    add_filter( 'wp_mail_content_type', 'my_mail_content_type' );
    
    function my_mail_content_type( $content_type ) {
    
        if ( $some_condition ) {
            return 'multipart/mixed';
        } else {
            return 'text/plain';
        }
    }

  4. Skip to note 8 content

    Example migrated from Codex:

    If you change the content type to `text/html`, it will cause problems with password reset emails.

    To remedy this, consider:
    modifying the email body of password reset emails to make it work with ‘text/html‘ content type
    OR
    reset the content type back to ‘text/plain‘ after you’re done sending the custom emails ( either by explicitly resetting the content type back to ‘text/plain‘ or by removing the callback function filter that changes the content type to ‘text/html‘ with remove_filter() )

    The following example shows how to use the filter `retrieve_password_message` to make the email body of password reset emails work with ‘text/html‘ content type:

    // adding support for html emails
    add_filter( 'wp_mail_content_type','mycustom_set_content_type' );
    
    function mycustom_set_content_type() {
            return "text/html";
    }
    
    // also filter the password reset email for compatibility with the HTML format
    add_filter( 'retrieve_password_message', 'mycustom_retrieve_password_message', 10, 1 );
    
    function mycustom_retrieve_password_message( $message ) {
            $message = str_replace('<','',$message);
            $message = str_replace('>','',$message);
            $message = str_replace("n",'<br>',$message);
            return $message;
    }

    The following example shows how to reset the content type back to ‘text/plain‘ by removing the callback function filter that changes the content type to ‘text/html‘ with remove_filter() :

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