Did you know that sometimes monthly newsletters or weekly special offers from your WordPress site are going to your recipients’junk folders? This is a common problem with emails that are sent from a website via PHP. Different mailbox providers adopt different techniques to decide when an email is or is not spam.
Why does that happen? How can you stop this from happening? In this tutorial, we’ll show you how to fix this email issue.
Why my WordPress Emails going to Spam Folder?
WordPress uses the PHP mail function to send emails. It has its own function for sending emails called wp_mail which you can find in wp-includes/pluggable.php.
While sending email, wordpress use admin email as your site’s outgoing email address. But it does not add sub-optimal Return-Path in email header. So your mail server software (from wordpress host) is adding a return-path header to your WordPress emails that looks something like the following:
Return-Path: <apache@hostingserver.domain.com>
This return email does not match the from email (i.e.your site email) and some spam filters would flag this email as junk mail. Thus emails sent by WordPress can often end up in the spam folder.
You can avoid spam folder by either using SMTP to send emails from WordPress or by adding a return-path header to match your site email.
Avoid the spam folder when sending mails with WordPress by fixing The Return-Path Header
Add the following code in your theme’s functions.php file to change the return path in your WordPress email settings match that of the “from” address:
Here is a code snippet you can add to your functions.php file to change the return path in your WordPress email settings match that of the “from” address:
// Function to change email address
function wpb_sender_email( $original_email_address ) {
return get_option('admin_email');
}
function add_email_return_path( $phpmailer ) {
$phpmailer->Sender = $phpmailer->From;
}
add_filter( 'wp_mail_from', 'wpb_sender_email' );
add_action( 'phpmailer_init', 'add_email_return_path' );