Customize email sending header in WordPress

WordPress has wp_mail() function for sending emails via php code. Primitive form of that function is following:
wp_mail(“receiver email”,”subject”,”message”);
But this is not enough data for sender, because we need header information at least(+ attachment data if it exists). When header and attachment data are ready, we write mail command so:
wp_mail(“receiver email”,”subject”,”message”, $header_data, $attachment_data);
But if we use mail function many times in code, it is not suitable to build same header each time.
We must build header in core and then we will be able to use primitive variant of wp_mail function with header information.
wp_mail information is in wp-includes/pluggable.php.(line 376 in 3.2.1 version)  Default parametres are so:
$from_email = ‘[email protected]’ . $sitename; $from_name = ‘WordPress’;
It is not good for your site if your user gets such email from you: “WordPress” <[email protected]>
You would like to send such email header : “Your site name or your name” <[email protected]>
We can change it there. But it is not good. Why? Because pluggable.php is core file and after next WordPress update new pluggable.php will replace old one and you will lost your change. So we must change that parametres with WordPress hooks. Go to your theme editor, open functions.php and place these code there:

<br />
function website_email() {<br />
    $sender_email= 'Your email here';<br />
    return $sender_email;<br />
}<br />
function website_name(){<br />
    $site_name = 'Your site name here';<br />
    return $site_name;<br />
}<br />
add_filter('wp_mail_from','website_email');<br />
add_filter('wp_mail_from_name','website_name');<br />

That’s all.

5 comments on “Customize email sending header in WordPress

  1. Hi Matthew, thanks for opinion. Yes i am informed about Contact Form 7, nice plugin, it can send normal emails with headers, but it is just for contact page, isn’t it? But this post tells about how to change WordPress core email_headers, which are available in all wordpress codes( plugin, theme, post(via eval() function ) etc.)

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: