One common question I often hear regarding WordPress email configuration is, “How do I change the email address from wordpress@mydomain.com?” I suspect that most people are unaware that the WordPress email function wp_mail() has a default address that it sets as the “from” address. If they are aware, most users opt for changing this with a plugin.
WordPress has no ability to manage email settings through the admin panel, but that does not mean you can’t manage the email configuration at all. It actually is very flexible. You can even change your WordPress email configuration to send via an SMTP server; a method much more reliable than the generic email script. But I’m getting ahead of myself.
If your sole aim is to change the email “from” address to something that is not the default value (wordpress@yourdomain.com), this is far to simple for using a plugin (unless you have complicated needs and want more control over email configuration in the WordPress admin panel).
There are quite a few plugins that do this, and if you feel more comfortable doing it that way, by all means, add another plugin to the list of things your blog needs to load. But if you are brave enough to do something quick, easy, and lightweight, then read on!
wp_mail and some relatives
WordPress relies on a function called wp_mail() to send email. This function is essentially a wrapper for the phpmailer class. The problem is that the wp_mail default email “from” address cannot be configured via the WordPress admin panel. But the fortunate thing is that this function and its related functions are both pluggable and can be filtered. Our example here is to simply opt for a filter.
wp_mail() relies on some other outside information, some of which is wp_mail_from (an email address) and wp_mail_from_name (the real name given to the email address). Since that is all we want to change, we are just going to filter those.
Filtering the email address
Add a filter for the email address using ‘add_filter’:
add_filter( 'wp_mail_from', 'my_mail_from' ); function my_mail_from( $email ) { return "change-this-to-your-email-address"; }Filtering the email name
Now add a filter for the name of the email address:
add_filter( 'wp_mail_from_name', 'my_mail_from_name' ); function my_mail_from_name( $name ) { return "My Name"; }The functions.php file
That’s great, but what do I do with it?
I’m glad you asked. Add these two filters and their accompanying functions to your theme’s functions.php file. You don’t even need to mess with a file editor and ftp for this. I would simply go to the Appearance > Editor menu in the WP Admin Panel, then find ‘functions.php’ in the list of theme files on the right. Add these filters and you are in business.
Easy peasy lemon squeezy. And no bloated plugin to slow down your site!
If you’d rather use a plugin to handle WordPress email configuration instead of the functions file, that’s covered in this post.
For more information on testing, troubleshooting, and changing your WordPress email configuration for wp_mail, here are some additional posts:
- Testing your WordPress email settings for the wp_mail function – some information on wp_mail and a testing script you can use to make sure it is sending messages.
- Troubleshooting wp_mail WordPress Email Configuration – not everything that can go wrong is directly a problem with WP. This post has information on host restrictions and other outside problems that should be checked.
- WordPress Email Settings: Changing the wp_mail address with a simple plugin – here is a very simple and lightweight script you can load as a plugin to change the email address that WordPress sends email from.
- Easy SMTP email settings for WordPress – how to change your WordPress email configuration to send email from a valid SMTP server with a simple script, no plugin required.