WordPress Sender Email Address

envelope with address

When your WordPress website send emails, if the sender email address isn’t specified in code it’ll default to sending as wordpress@example.org. You probably don’t want your clients to see this. Instead, you’ll probably want to go with something like [email protected] or [email protected].

infoTo apply this simple modification, you’ll need to be using a custom child theme so you can modify your functions.php file.

Create a new file called functions-rewrite-sender.php in your custom child theme’s folder and paste the following into it.

<?php

/**
 * Rewrite the sender name and/or address in outgoing emails.
 * https://wp-tutorials.tech/refine-wordpress/set-wordpress-default-email-sender/
 */

// Block direct access.
defined('WPINC') || die();

/**
 * Rewrite the outgoing email sender's name.
 */
function wpt_wp_mail_from_name($sender_name) {
	return get_bloginfo('name');
}
add_filter('wp_mail_from_name', 'wpt_wp_mail_from_name');

/**
 * If the sender's address starts with "wordpress@"...
 * get the site's domain name and send the email as no-reply@site-domain
 */
function wpt_wp_mail_from($sender_email) {
	if (strpos(strtolower($sender_email), 'wordpress@') === 0) {
		// Get the site's URL.
		$site_url = get_site_url();

		// Parse the URL into useful stuff.
		// https://www.php.net/manual/en/function.parse-url.php
		$parsed_url = parse_url($site_url);

		// Get the host. If it starts with "www." then strip it
		// out so we just get the domain part.
		$domain = $parsed_url['host'];
		if (strpos($domain, 'www.') === 0) {
			$domain = substr($domain, 4);
		}

		// Set a nice, clean sender address.
		$sender_email = 'no-reply@' . $domain;
	}

	return $sender_email;
}
add_filter('wp_mail_from', 'wpt_wp_mail_from');

All we do here is hook two standard WordPress filters wp_mail_from & wp_mail_from_name, and override the returned strings if we’ve set the relevant constant.

To use this snippet, just add the following to functions.php in your custom child theme.

/**
 * Rewrite the sender/from for outgoing emails.
 */
require_once dirname( __FILE__ ) . '/functions-rewrite-sender.php';

Testing the Sender Email

Install something like the WP Test Email plugin then send an email to an address that your website doesn’t know about. If your website is www.example.org, don’t send your test email to [email protected]. Send the test email to a personal GMail/Outlook/Whatever address so you can get a clear idea of what your customers will actually see.

When you receive the test email and you’re happy with the sender name/email, also try to check the headers of your outgoing emails…

Happy emailing!

Like This Tutorial?

Let us know

WordPress plugins for developers

2 thoughts on “WordPress Sender Email Address”

Leave a comment