I have several other posts on how to improve the reliability of email sent via wp_mail and how to troubleshoot your WordPress email settings, most of which has focused on the sending end. A common followup problem is email sent from your WordPress site ending up in the user’s spam folder. What can you do to your WordPress email settings to make your sent message less likely to be flagged as spam by the receiver?
The answer is that your WordPress email settings matter.
One main thing to look at is the email’s return path in the header. Since WordPress does not create email headers by default, most of the time this is going to be a non-existent email address that equates to your domain or your server (which might be based on your host – especially if you are on a shared host).
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:
So what’s going on here?
As discussed elsewhere on this site, wp_mail relies on the phpmailer class. WordPress provides us an action hook when phpmailer is initiated so we can customize it. In this case, we are hooking to this action (phpmailer_init) and making the “Sender” (return-path) the same as the “From” address.
Note that if you have WordPress email settings sending your email through as SMTP email server, this process is probably already done for you. This only applies if you are sticking with web server based email.