WP doesn’t send email? try this

Snippets for sending emails with Wordpress

Febbraio 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

PHP expert. Wordpress plugin and theme developer. Father, Maker, Arduino and ESP8266 enthusiast.

Recommended

Test page for Bright Links plugin

To test the plugin hover your mouse to a link, or tap the link on mobile device.

Dicembre 4, 2022

WordPress Gutenberg doesn’t parse Istagram oembed url

Sometimes Instagram embed doesn't work, why?

Novembre 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…

Settembre 2, 2016

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

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

Settembre 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…

Settembre 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…

Agosto 25, 2015