WP doesn’t send email? try this

Snippets for sending emails with Wordpress

February 8, 2020

Here are some small block of codes that you can include in your functions.php if your calls to wp_mail fail.

Problems could happen in other part of the sending process (for example in the SMTP server), but you can try these:

/* specify the From header in email */
add_action( 'phpmailer_init', 'fix_return_path' );
function fix_return_path( $phpmailer ) {
	$phpmailer->Sender = $phpmailer->From;
}

/*  force the use of from header */
add_filter( 'wp_mail_from', 'force_from' );
function force_from( $email ) {
    return "noreply@yourdomain.com";
}
 
/* add the name of the sender */
add_filter( 'wp_mail_from_name', 'force_from_name' );
function force_from_name( $name ) {
    return bloginfo('name');
}

All those lines of code try to better compose your email when WordPress send messages using wp_mail. A mail composed properly can pass antispam filters better.

If these lines don’t solve the problem try to send an email from a php file with the mail function.

If it doesn’t send emails the problem belongs to the server, you can also try an external SMTP server (there are also WordPress plugins that let wp_mail use an external SMTP server).

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Comments on “WP doesn’t send email? try this”

Leave a Reply

Your email address will not be published. Required fields are marked with an *

Recommended

WordPress Gutenberg doesn’t parse Istagram oembed url

Sometimes Instagram embed doesn't work, why?

November 4, 2019

Highlight text for search results in PHP

Useful code to highlight text occurences in search results or in a text. How to highlight text in a string…

September 2, 2016

How to add rel=”nofollow” to links with preg_replace()

Adding rel="nofollow" to external link is a good SEO practice.

September 22, 2015

Limit the number of categories for posts in WordPress

CHOOSE ONLY ONE CATEGORY WORDPRESS If you need to limit the number of categories used by the authors of your…

September 14, 2015

WordPress anyone can register, but no email confirmation

Many times it has happened to me that my WordPress installation properly send emails using wp_mail directly, specifing also the…

August 25, 2015

Block junk emails, spammers and temporary emails

If you need an Email Validator Function, consider this version that includes also the check against common temporary mail services…

December 3, 2013