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 = ‘wordpress@’ . $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:

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

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

  2. Yes ndeed it does!

    The contact plugin uses shortcodes, so you could easily add a new widget area, say to the header and add a widget with just the shortcode in and remove any code editing alone and less “scary” for newbies.

    Matt

Leave a Reply to Elvin HaciCancel reply

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