Search code examples
phpwordpressphpmailerupdatesreset-password

Wordpress updated from 4.1->4.7, got phpmailerException when resetting password


We updated Wordpress from 4.1.something to 4.7.3. Everything works, except the mail that contains the new password when a user resets the password. The password is still reset, but no mail is obtained with the reset password, making it impossible to log in. As far as I know, this is the only mail that fails, registration mails and such still works.

There shouldn't be anything wrong with the code, it has worked for years. But something was clearly messed up when updating. When trying to send the mail, the following error occurs.

PHP Fatal error:  Uncaught exception 'phpmailerException' with message 'Invalid address:  (setFrom) service' in /var/www/site-adress/public_html/wp/wp-includes/class-phpmailer.php:1023
Stack trace:
#0 /var/www/site-adress/public_html/wp/wp-includes/pluggable.php(352): PHPMailer->setFrom('service', 'WordPress', false)
#1 /var/www/site-adress/public_html/wp-content/themes/site/functions.php(320): wp_mail('dummy.mail...', 'Site: passwor...', 'Your new passwo...', 'From: service')
#2 [internal function]: {closure}('')
#3 /var/www/site-adress/public_html/wp/wp-includes/class-wp-hook.php(298): call_user_func_array(Object(Closure), Array)
#4 /var/www/site-adress/public_html/wp/wp-includes/class-wp-hook.php(323): WP_Hook->apply_filters('', Array)
 #5 /var/www/site-adress/public_html/wp/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#6 /var/www/site-adress/public_html/wp/wp-admin/admin-ajax.php(101): do_action in /var/www/site-adress/public_html/wp/wp-includes/class-phpmailer.php on line 1023

Does anyone know how this should be solved? What does this information mean? Where does the problem lie?

Thank you, ahead of time.


Solution

  • You can override the email address for these emails by adding the following into your theme functions.php:

    add_filter( 'wp_mail_from', 'new_mail_from' );
    add_filter( 'wp_mail_from_name', 'new_mail_from_name' );
    function new_mail_from( $old ) {
        return get_option( 'admin_email' );
    }
    function new_mail_from_name( $old ) {
        return get_option( 'blogname' ); 
    }
    

    Then, use Settings > General inside of WordPress to alter the email address and site name.

    Thanks to PS below who clarifies: this code can be dropped into functions.php theme file AS IS without any modifications. You then use the WordPress admin interface to change the sending email address ("Email Address" field at the Settings > General page).