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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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

add_filter('wp_mail_from','yoursite_wp_mail_from');
function yoursite_wp_mail_from($content_type) {
  return '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbd3ded7ded5c2d3d4cefbdec3dad6cbd7de95d8d4d6">[email protected]</a>';
}
add_filter('wp_mail_from_name','yoursite_wp_mail_from_name');
function yoursite_wp_mail_from_name($name) {
  return 'Helen Hou-Sandi';
}

Method 2

Well, if you’re using the From: "Your Name" <[email protected]>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().

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

Method 4

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.

Method 5

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x