WordPress anyone can register, but no email confirmation

Email problems with wordpress

Agosto 25, 2015

Many times it has happened to me that my WordPress installation properly send emails using wp_mail directly, specifing also the headers parameter with the proper From syntax followed by the email address.
But when I use the “anyone can register” checkbox and try to register a new user from the url http://yourdomain.it/wp-login.php?action=register I don’t receive any email and can’t use the users (neither for the user nor for the admin).
So I’ve investigated a little bit more and I’ve found that the wp_new_user_notification function in the wp-includes/pluggable.php core file do not have a default value for From header. This let the PHP use the parameter in the php.ini file (I think) which on many server is not configured for your web sites, and so your emails are not sent.
To fix this, force wp_mail to use a From header by adding this two functions on your functions.php file.

add_filter( 'wp_mail_from', 'force_from' );
function force_from( $email ) {
	return "noreply@yourdomain.it";
}

add_filter( 'wp_mail_from_name', 'force_from_name' );
function force_from_name( $name ) {
	return "Wordpress of Your Domain";
}

Author

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