Wordpress - Why won't wp_mail() let me set the From: header when plain old PHP mail() will?
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';
}
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()
.
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()
.