36

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?

5 Answers 5

45

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 <[email protected]>:

add_filter('wp_mail_from','yoursite_wp_mail_from');
function yoursite_wp_mail_from($content_type) {
  return '[email protected]';
}
add_filter('wp_mail_from_name','yoursite_wp_mail_from_name');
function yoursite_wp_mail_from_name($name) {
  return 'Helen Hou-Sandi';
}
5
  • I was just looking at the filters and you are right, it does fix the problem. I suppose the Codex should be updated? codex.wordpress.org/Function_Reference/wp_mail Also related: this seems to affect lots of form plugins out there, including the mighty GravityForms. I'm currently writing a custom form, but if this is how WP is supposed to behave, why does setting the headers using the regular old method work for others (and even some of my other sites)? Commented Feb 10, 2011 at 21:47
  • @helenyhou - To keep from affecting other forms you'd need to add the filters in your plugin just before called wp_mail() and then remove them right after. As for why setting the headers work, I don't know what your other use-cases are but I'd be surprised if wp_mail() would work that way. Commented Feb 10, 2011 at 22:49
  • 4
    Ahhhh I found what was happening - another plugin was adding the filters globally for something I didn't ask for and can't opt out of - bad, bad, bad. So now the regular headers do work after all. Seems like using those filters would be best practice though, if they're in there. Commented Feb 10, 2011 at 23:00
  • Just a note here that any filter a plugin is adding can be removed via remove_filter(HOOK, FUNCTION). Commented Sep 16, 2012 at 21:27
  • it only works if the from email domain as same as the site.
    – Omer
    Commented Apr 28, 2018 at 7:02
4

Well, if you're using the From: "Your Name" <[email protected]>\r\n 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" . "\r\n";
$headers .= "Content-type: text/html; charset=".get_bloginfo('charset')."" . "\r\n";
$headers .= "From: MyPlugin <".$this->settings['from_address'].">" . "\r\n";
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().

5
  • 2
    I did not set the filter, another plugin did. The filter takes precedence over the header. Also, you really should use site_url() instead of get_bloginfo('wpurl') Commented Apr 16, 2011 at 4:14
  • But then surely that plugin is bad for not clearing up after itself.... not alot you can do about it though :( in that case I better change my plugin to use filter just in case another plugin has broken it.
    – Scott
    Commented Apr 16, 2011 at 8:59
  • @helenyhou - +1 for reminding me about site_url(). I've also changed my plugin to use filters rather than using $headers. Plus I've made sure to remove those filters once I've sent the email so not to get in the way of anything else :)
    – Scott
    Commented Apr 16, 2011 at 13:36
  • yes, it's that plugin's bad for not giving me an option not to use those filters, but luckily commenting out the filters worked out for my case. I'll probably ask around to see which is better to use in the future - a per-use filter or the more familiar headers. The headers are probably more efficient, but then why are those filters there? Always more questions :) Commented Apr 26, 2011 at 12:45
  • As of the current version, it is totally acceptable to use get_bloginfo('wpurl'): This returns site_url() already. get_bloginfo('home') and get_bloginfo('siteurl') are deprecated. developer.wordpress.org/reference/functions/get_bloginfo
    – Ion
    Commented Dec 25, 2016 at 16:03
1

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

$headers = 'Reply-To: "Aaren A. Aarenson" <[email protected]>';

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.

1
  • This fixed it for me. You saved a life mate! ;)
    – user382738
    Commented Jun 26, 2017 at 23:43
0

I had the same problem. For me it came out that the hosting provider (BlueHost) was preventing from changing the from field. Here they explain it https://my.bluehost.com/cgi/help/206 .

I fixed the problem by adding the email to cPanel mailboxes just as they say.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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