You’d be surprised how much time goes into making sure that emails sent by your website successfully make it to their destinations.
As we’ve discussed before, you can’t always rely on your website hosting’s PHP mail sender, especially if your organization uses Outlook 365 or Google Workspace to manage your internal email addresses.
We almost always recommend using a plugin like WP Mail SMTP if your website isn’t sending emails, with a dedicated email address created by your IT staff for use only on your website.
Generally, we recommend that email address be something like “wordpress@yourdomain.org” or “website@yourdomain.org” so it’s clear to everyone what it is used for.
But how can you ensure that the website never tries to send from a different email address? Or with the wrong sender name?
Thankfully, there are code snippets for that.
After you’ve created the email address, connected your WordPress website to it, and successfully sent emails using it, you need to make sure that no plugins or third-party scripts try to use anything else.
While that may not seem likely, many form plugins – even Gravity Forms, which we recommend – allow you to customize the email address used when notifications or email confirmations are sent. Sometimes this is needed, but if you’re having issues with email deliverability, it’s better to force your email address to be used rather than allow changes to be made.
Ensuring your newly created email address is the only option available is as simple as adding a code snippet to your site’s functions.php file. The wp_mail_from
filter allows us to set that email address, which can’t be overruled by any other plugin or setting.
Use this snippet and you’ll force WordPress to always send emails from the correct email address.
// Update from name and from email in emails
add_filter( 'wp_mail_from', function ( $email ) {
return 'wordpress@yourdomain.org';
} );
Change “wordpress@yourdomain.org” to the email address you’ve created, and you’ll be good to go.
Okay, so we’re all good with the actual email address being used – what about the sender name that shows up in someone’s inbox?
Like the email address itself, this can often be customized by plugins – again, sometimes that makes sense, but usually it’s more important that your organization’s name is always used and not able to be overwritten.
Not surprisingly, there’s a code snippet for that as well.
The WordPress filter we want to use this time is wp_mail_from_name
.
Here’s what you should add to your functions.php file.
add_filter( 'wp_mail_from_name', function ( $name ) {
return 'Your Organization Name';
} );
Put in the sender name you want to use in the above snippet, and you’ll force all emails to be shown as coming from that name.
If you’re a developer who needs to make the email address and sender editable by your client in the WordPress admin, there’s a little trick we like to use in those situations.
It’s not really an ideal solution – since the whole purpose of forcing the email address and sender name is to ensure it can’t be changed on a whim – but there may be a time where you have no choice.
To allow the email address and sender name to be editable in the WordPress admin and not hard-coded into functions.php, use a custom field – probably in an Advanced Custom Fields Site Options page – and set the two functions to return the value of that field.
While you’re here, enjoy this Junger Explains it All video on making sure your website emails get delivered.