apply_filters( 'wp_mail_from', string $from_email )

Filters the email address to send from.


Parameters Parameters

$from_email

(string) Email address to send from.


Top ↑

More Information More Information

  • The wp_mail_from filter modifies the “from email address” used in an email sent using the wp_mail() function. When used together with the ‘wp_mail_from_name‘ filter, it creates a from address like “Name”. The filter should return an email address.
  • To avoid your email being marked as spam, it is highly recommended that your “from” domain match your website.
  • Some hosts may require that your “from” address be a legitimate address.
  • If you apply your filter using an anonymous function, you cannot remove it using remove_filter().

Top ↑

Source Source

File: wp-includes/pluggable.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
2.2.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 2 content
    Contributed by Elio Rivero

    Example of how to use this filter to update the user portion of the email address to send from.

    <?php
    /**
     * Filter the email address to send from and replace the user.
     *
     * @param string $from_email Email address to send from.
     *
     * @return string The new email address.
     */
    function wporg_replace_user_mail_from( $from_email ) {
    	// This will give us and array with the email user and the domain
    	// like this [ 'wordpress', 'example.tld' ]
    	$parts = explode( '@', $from_email );
    
    	// Add our new email user, add @ again, and append the domain
    	// This will be hello@example.tld
    	return 'hello@' . $parts[1];
    }
    
    add_filter( 'wp_mail_from', 'wporg_replace_user_mail_from' );
    
  2. Skip to note 3 content
    Contributed by Dhimas Kirana

    Simple way to filter the email address using str_replace() function.

    <?php
    /**
     * Filter the email address using str_replace() function.
     */
    function wpdocs_custom_wp_mail_from( $original_email_address ) {
    	// The $original_email_address value is wordpress@yourdomain.tld
    	// So we just change 'wordpress@' to something else like 'noreply@' or etc
    	return str_replace( 'wordpress@', 'noreply@', $original_email_address );
    }
    add_filter( 'wp_mail_from', 'wpdocs_custom_wp_mail_from' );
    
  3. Skip to note 4 content
    Contributed by stevenlinx

    Examples migrated from Codex:

    add_filter( 'wp_mail_from', 'custom_wp_mail_from' );
    function custom_wp_mail_from( $original_email_address ) {
    	//Make sure the email is from the same domain 
    	//as your website to avoid being marked as spam.
    	return 'webmaster@mydomainname.com';
    }
    

    Using anonymous functions (PHP 5.3.0+) for callback:

    add_filter( 'wp_mail_from', function( $email ) {
    	return 'webmaster@mydomainname.com';
    } );
    

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