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.

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?

share|improve this question

3 Answers 3

up vote 24 down vote accepted

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';
}
share|improve this answer
    
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
1  
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
    
@helenyhou - Glad you got your answer, and glad I could help. –  MikeSchinkel Feb 11 '11 at 13:19
    
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

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.

share|improve this answer

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().

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

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.