32

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?

44

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';
}
  • 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)? – helenhousandi Feb 10 '11 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. – MikeSchinkel Feb 10 '11 at 22:49
  • 2
    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. – helenhousandi Feb 10 '11 at 23:00
  • Just a note here that any filter a plugin is adding can be removed via remove_filter(HOOK, FUNCTION). – Jason Coleman Sep 16 '12 at 21:27
  • it only works if the from email domain as same as the site. – Omer Apr 28 '18 at 7:02
4

Well, if you're using the From: "Your Name" <youremail@example.com>\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.

1

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 dont have to worry about using a filter and then removing at after wp_mail().

  • 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') – helenhousandi Apr 16 '11 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. – Brady Apr 16 '11 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 :) – Brady Apr 16 '11 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 :) – helenhousandi Apr 26 '11 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 – James M Dec 25 '16 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" <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.

  • This fixed it for me. You saved a life mate! ;) – user382738 Jun 26 '17 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, privacy policy and cookie policy

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