Change WordPress Mail Sender

WordPress makes it easy and fast to add new users in the backend. Since version 2.8 of WordPress, it can send the access information via email. A nice feature, with no additional settings to change the sender of this email. For example the email should be send from the administrator instead from WordPress.

Nevertheless, there is a possibility and with the help of two hooks, the sender can be changed. I created a small Plugin, where you can easy and simple adjust the sender name and sender email.
-
The Plugin is available in the following source code and has no options for the backend and doesn't leave any data in the database. Anyone who wants can simply extend the Plugin.

<?php
/**
 * @package WP Mail From
 * @author Frank Bültge
 * @version 0.1
 */
 
/*
Plugin Name: WP Mail From
Plugin URI: http://bueltge.de/
Description: Change the default address that WordPress sends it’s email from.
Version: 0.1
Author: Frank Bültge
Author URI: http://bueltge.de/
Last Change: 11.08.2009 08:41:06
*/

if ( !function_exists('add_action') ) {
	header('Status: 403 Forbidden');
	header('HTTP/1.1 403 Forbidden');
	exit();
}

if ( !class_exists('wp_mail_from') ) {
	class wp_mail_from {
		
		function wp_mail_from() {
			add_filter( 'wp_mail_from', array(&$this, 'fb_mail_from') );
			add_filter( 'wp_mail_from_name', array(&$this, 'fb_mail_from_name') );
		}
		
		// new name
		function fb_mail_from_name() {
			$name = 'My Blog is my Blog';
			// alternative the name of the blog
			// $name = get_option('blogname');
			$name = esc_attr($name);
			return $name;
		}
		
		// new email-adress
		function fb_mail_from() {
			$email = 'info@example.com';
			$email = is_email($email);
			return $email;
		}
		
	}
	
	$wp_mail_from = new wp_mail_from();
}
?>

The values for name and email have to be maintain in each of the associated function. After that, the two values are getting examined, but is not necessarily needed.

As a note: the function esc_attr() is only since version 2.8 available and replaces the function attribute_escape(). Should the solution be used in an earlier version, then you have to change the function.

For suggestions and improvements, I am grateful, as always. You can use this Plugin to improve WordPress a bit and the user is not surprised about the sender "WordPress".

Comments are closed.

4 comments

  1. Spence Kiddle

    Frank,

    Awesome little script very easy to use.
    FYI the function names are flipped.

    //new name
    fb_mail_from()
    should be fb_mail_from_name()

    //new email-address
    fb_mail_from_name()
    should be fb_mail_from()

  2. Frank

    @Spence Kiddle: thanks for your hint; you have right.
    I have fixed this.

  3. Mike

    awesome post! If you are looking to just change the name of the default sender from "WordPress" to something like "Mike" just edit line 352 of pluggable.php. change:

    $from_name = 'WordPress';

    to

    $from_name = 'Mike';...

    As a side note, if you update wordpress, this change will be undone...so a more permanent solution would be to use a plugin. But for quick and easy, this is a viable solution.

  4. Derry

    @Mike

    edit line 375 in WordPress 3.0 . You also want to edit line 391

    $from_email = 'wordpress@' . $sitename;

    Cheers