wp mail - Change email from and display name with a filter action

admin2025-01-07  3

I'm attempting to override the "from" display name and email address using the wp_mail() function. I am using the wp_mail_from filter hook to modify it with my custom function using add_filter('wp_mail_from','abcisupport_wp_mail_from').

If I hard code the value, it will return the address so it's wired up properly. If I use the argument, it returns the site network defaults. How can I pass in a value to the arguments (here: $email and $name) to my overriding functions?

function abcisupport_wp_mail_from($email) {
  return $email; //returns our default site network email address
  /* return '[email protected]'; // returns  [email protected] as the from address*/
}

function abcisupport_wp_mail_from_name($name) {
  return $name; //returns our default site network display name

}

function send_abc_mail_before_submit(){
    add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
    add_filter('wp_mail_from','abcisupport_wp_mail_from');
    add_filter('wp_mail_from_name','abcisupport_wp_mail_from_name');

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

    remove_filter('wp_mail_from','abcisupport_wp_mail_from');
    remove_filter('wp_mail_from_name','abcisupport_wp_mail_from_name');
}

Note: all fields within the wp_mail() function are already set; code above has been simplified for readability.

I'm attempting to override the "from" display name and email address using the wp_mail() function. I am using the wp_mail_from filter hook to modify it with my custom function using add_filter('wp_mail_from','abcisupport_wp_mail_from').

If I hard code the value, it will return the address so it's wired up properly. If I use the argument, it returns the site network defaults. How can I pass in a value to the arguments (here: $email and $name) to my overriding functions?

function abcisupport_wp_mail_from($email) {
  return $email; //returns our default site network email address
  /* return '[email protected]'; // returns  [email protected] as the from address*/
}

function abcisupport_wp_mail_from_name($name) {
  return $name; //returns our default site network display name

}

function send_abc_mail_before_submit(){
    add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
    add_filter('wp_mail_from','abcisupport_wp_mail_from');
    add_filter('wp_mail_from_name','abcisupport_wp_mail_from_name');

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

    remove_filter('wp_mail_from','abcisupport_wp_mail_from');
    remove_filter('wp_mail_from_name','abcisupport_wp_mail_from_name');
}

Note: all fields within the wp_mail() function are already set; code above has been simplified for readability.

Share Improve this question edited May 15, 2019 at 13:20 butlerblog 5,0713 gold badges26 silver badges42 bronze badges asked Sep 24, 2012 at 3:06 MichaelMichael 1231 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 6

A couple possibilities.

Use the settings API to add a form field somewhere in your admin area where the user can enter the email they want to use. Retrieve it in your hooked function with get_option.

This would be the way to go if you're going to use the same from email everywhere.

<?php
add_filter('wp_mail_from', 'wpse66067_mail_from');
function wpse66067_mail_from($email)
{
    if($e = get_option('wpse66067_option'))
        return $e;

    return $email; // we don't have anything in the option return default.
}

Use an object, and store the from email as a property. This would be the way to go if you need to change the from email on a per send basis.

<?php
class WPSE66067_Emailer
{
    private static $from_email = '[email protected]';

    public static  function send_mail()
    {
        add_filter('wp_mail_from', array(__CLASS__, 'from_email');

        // change the email
        self::$from_email = '[email protected]';

        wp_mail( /* args here */ );

        // you probably don't need to do this.
        remove_filter('wp_mail_from', array(__CLASS__, 'from_email');
    }

    public static function from_email($e)
    {
        if(self::$from_email)
            return self::$from_email;

        return $e;
    }
}

Use a global. This is (probably) a terrible idea. If you need to use change the email every time, use an object and a property (see above).

<?php
$wpse66067_email = '[email protected]';

function wpse66067_send_mail()
{
    // globalize and change the email
    global $wpse66067_email;
    $wpse66067_email = '[email protected]';

    add_filter('wp_mail_from', 'wpse66067_from_email');

    wp_mail( /* args here */ );
}

function wpse66067_from_email($e)
{
    global $wpse66067_email;
    if($wpse66067_email)
        return $wpse66067_email;

    return $e;
}

This simple code snippet did the trick for me, to change WordPress <[email protected]> as default sender :

// Change the From address.
add_filter( 'wp_mail_from', function ( $original_email_address ) {
    return '[email protected]';
} );
 
// Change the From name.
add_filter( 'wp_mail_from_name', function ( $original_email_from ) {
    return 'John Doe';
} ); 

Hope this helps.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736256074a332.html

最新回复(0)