Skip to main content
4 of 4
readability cleanup
butlerblog
  • 5.1k
  • 3
  • 26
  • 42

Change email from and display name with a filter action

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.

Michael
  • 123
  • 1
  • 5