How to change email address used for registrations?

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 = "[email protected]";
    }
    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.