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?

5

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';
}

Leave a Comment