1

I'd like to have a different email address that the one used for the website administrator to send registration emails and email notifications. Say for instance I installed WP using admin@example.com but I need all registration and notification emails sent from no-reply@example.com

I was told that I can change my account email address from the WP dashboard Settings > General, but I enabled the multisite option so I guess the option is now at Network Settings > Network Admin Email, however I couldn't find a way to set a different email address just for registrations an notifications in the options that appear in the settings.

Also, it was pointed out to me that using SMTP I can handle different email addresses with Gmail, but my server is behind Cloudflare, and I've installed the SendGrid plugin in the API mode, because using the SMTP plugin my real server's IP address would be sent in the email header using SMTP.

1

Since you bring up SMTP, I'll mention that you'll probably need to make that decision before you decide how you'll handle your "from" addresses because ultimately the direction you go with that will likely affect how you should handle the "from" address.

The "simple" approach is to just filter the "from" address - that's if you're not using SMTP for sending.

Here's what that looks like:

/**
 * Start by getting the subject line of the notification
 * email. This assumes the use of the use of the WP default,
 * so if some other process of registration is used, then
 * change accordingly.
 */
add_filter( 'wp_new_user_notification_email', 'my_get_notification_subject' );
function my_get_notification_subject( $args ) {
    global $my_notifiction_subject;
    $my_notifiction_subject = $args['subject'];
    return $args;
}

/**
 * Filter for wp_mail so that we can check the subject line
 * of the email being sent against the value of the default
 * notification email (set in the filter above). If the 
 * message is the notification email, then set a global 
 * that we can check in the "from" and "from_name" filters
 * to change the "from" info.
 */
add_filter( 'wp_mail', 'my_check_wp_mail' );
function my_check_wp_mail( $args ) {
    // Needed globals.
    global $my_notifiction_subject, $my_change_from;
    // Start with setting change "from" notification to false.
    $my_change_from = false;
    if ( isset( $my_notifiction_subject ) ) {
        if ( $args['subject'] == $my_notifiction_subject ) {
            $my_change_from = true;
        }
    }
    return $args;
}

/**
 * If the wp_mail filter set the $my_change_from global
 * to true, then the email being sent is the notification
 * email, so we'll use this filter to change the "from"
 * address.
 */
add_filter( 'wp_mail_from', 'my_wp_mail_from' );
function my_wp_mail_from( $from_email ) {
    global $my_change_from;
    if ( $my_change_from ) {
        $from_email = "no-reply@mydomain.com";
    }
    return $from_email;
}

/**
 * Same as above but it changes the "from name" to go
 * with the address.
 */
add_filter( 'wp_mail_from_name', 'my_wp_mail_from_name' );
function my_wp_mail_from_name( $from_name ) {
    global $my_change_from;
    if ( $my_change_from ) {
        $from_name = "No Reply";
    }
    return $from_name;
}

Explanation of the above (although it's also commented with each filter):

  1. Use the wp_new_user_notification_email filter to set a global (so we can pick it up in another function) with the value of the notification email subject line. Note that this assumes the default WP new user notification. If some other process is being used or that default is somehow changed, this needs to be changed to accommodate that.
  2. Use the wp_mail filter ("filter" - not to be confused with the wp_mail() function) to check the subject line against the global variable we set in the wp_new_user_notification_email filter. That will tell you if the email being sent is the new user notification. If it is, use a global variable to toggle a boolean (true|false) as to whether the from email needs to change.
  3. Use the wp_mail_from and wp_mail_from_name filters to check the $my_change_from (the global boolean mentioned in #2) to determine whether to change the from address and name.

There would be some other ways to do this same concept - this is just one possible approach.

If you end up using SMTP, then it's a different story because this wouldn't work. In that case, you'd need an actual address for both and you'd need to make your connection based on that information when handling phpmailer_init.

  • Given that an address like no-reply@example.com is so commonly used in all kind of websites, I'm surprised to see how complicated it's to set customs emails addresses for different tasks in WP. I'll give it a try and report back. Thanks. – rraallvv Dec 27 '18 at 23:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.