Why won’t wp_mail() let me set the From: header when plain old PHP mail() will?

When I use wp_mail( $to, $subject, $message, $headers ) (with values in place, of course), the email gets sent with a from name and email that isn’t set anywhere I can find (not even in PHP or Apache settings). However, using mail( $to, $subject, $message, $headers ) instead works just fine. What could be happening with wp_mail() to cause this?

Related posts

Leave a Reply

5 comments

  1. Hi @helenyhou:

    You can set the header, just not with a parameter. WordPress uses “hooks” and the hooks you need are 'wp_mail_from' and 'wp_mail_from_name' hooks.

    Here are the hooks you might add to your theme’s functions.php file to modify the "From:" header when using wp_mail() to the email address Helen Hou-Sandi <helenyhou@example.com>:

    add_filter('wp_mail_from','yoursite_wp_mail_from');
    function yoursite_wp_mail_from($content_type) {
      return 'helenyhou@example.com';
    }
    add_filter('wp_mail_from_name','yoursite_wp_mail_from_name');
    function yoursite_wp_mail_from_name($name) {
      return 'Helen Hou-Sandi';
    }
    
  2. Well, if you’re using the From: "Your Name" <youremail@example.com>rn format in your headers, you shouldn’t be having a problem (unless you have a plugin installed which overrides the wp_mail function).

    However, as Mike said, you can filter the ultimate values with those filters, or you can just install this plugin:

    Send From

    It’ll give you an options setting to determine what name and email to use in wp_mail().

  3. Sorry to revive an old question but isn’t it better to set via the headers like so:

    $subject  = "MyPlugin: Alert (".get_bloginfo('wpurl').")";
    $headers  = "MIME-Version: 1.0" . "rn";
    $headers .= "Content-type: text/html; charset=".get_bloginfo('charset')."" . "rn";
    $headers .= "From: MyPlugin <".$this->settings['from_address'].">" . "rn";
    wp_mail($this->settings['notify_address'], $subject, $alertMessage, $headers);
    

    That way you don’t have to worry about using a filter and then removing at after wp_mail().

  4. It’s a bit of a hack but you can also use the Reply To header tag:

    $headers = 'Reply-To: "Aaren A. Aarenson" <aaron@somemail.com>';

    Sadly this adds the email address to the From list, and replying means you will have to manually remove the address configured in the wp_mail_from filter.