Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm attempting to override the from display name and email address using wp_mail function. I am using the hook to modify wp_mail_from with my custom function using add_filter('wp_mail_from','abcisupport_wp_mail_from'). If I hard code the value it'll 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.

share|improve this question

migrated from stackoverflow.com Sep 24 '12 at 3:07

This question came from our site for professional and enthusiast programmers.

1 Answer 1

up vote 2 down vote accepted

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;
}
share|improve this answer
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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