apply_filters( 'wp_mail_content_type', string $content_type )

Filters the wp_mail() content type.


Parameters Parameters

$content_type

(string) Default wp_mail() content type.


Top ↑

More Information 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.
  • Setting the default to “text/html” can cause problems with default WordPress emails such as password reset. Line breaks will not appear and Gmail will strip out the reset link. See the following tickets for more info:
    https://core.trac.wordpress.org/ticket/21095
    https://core.trac.wordpress.org/ticket/23578

Top ↑

Source Source

File: wp-includes/pluggable.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
2.3.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Helen Hou-Sandi

    Switch to HTML formatted email when using wp_mail():

    /**
     * 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 -- https://core.trac.wordpress.org/ticket/23578
    remove_filter( 'wp_mail_content_type', 'wpdocs_set_html_mail_content_type' );
    
  2. Skip to note 2 content
    Contributed by stevenlinx

    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';
        }
    }
    
  3. Skip to note 3 content
    Contributed by stevenlinx

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

You must log in before being able to contribute a note or feedback.