1

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 'michael@site.com'; // returns  michael@site.com 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.

5

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 = 'something@email.com';

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

        // change the email
        self::$from_email = 'some_other_email@example.com';

        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 = 'something@email.com';

function wpse66067_send_mail()
{
    // globalize and change the email
    global $wpse66067_email;
    $wpse66067_email = 'some_other_email@example.com';

    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;
}
  • 1
    thank you so much. apologies on the delayed response but you've been tremendous. i wish i could +3 for each of the examples you gave. – Michael Sep 26 '12 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.