Need a bit of help with email headers please

I'm using the Join My Multisite plugin to enable users to register on domain mapped client subsites and have ironed out all the wrinkles exceopt for one bit of branding: can't seem to get the email headers quite right to display the subsite name in the email header, as well as the "Thanks" note in the footer of the email body. Can you fine folk help?

I provided screenshots during our live chat session, but would prefer if they remained hidden as client emails & site addresses are showing on them. So they should be accessible in the chat history.

  • Panos

    Hi Greg ,

    It's not that straight forward as the registration and activation happen on main blog (it you notice the form redirects to wordpress signup.php page). There are a few hooks that we can use and set the blog id. I have prepared a snippet using those hooks and comments explaining what each does:

    //Adds hidden field with blog_id in registration form
    add_action( 'signup_extra_fields', function(){
    
        if( is_main_site() ) {
            return;
        }
        ?>
        <input type="hidden" name="blog_id" value="<?php echo get_current_blog_id(); ?>" />
        <?php
    
    }, 20 );
    
    // Adds the blog_id in the signup meta so we can use it later on in the emails
    add_action( 'signup_user_meta', function( $meta, $user, $user_email, $key ){
    
        if( ! isset( $meta['blog_id'] ) && isset( $_REQUEST['blog_id'] ) && get_network()->site_id != $_REQUEST['blog_id']  )  {
            $meta['blog_id'] = (int) $_REQUEST['blog_id'];
        }
        return $meta;
    
    }, 20, 4 );
    
    // Includes the activaton link:
    add_filter( 'wpmu_signup_user_notification_email', function( $content, $user_login, $user_email, $key, $meta ){
    
        if ( isset( $meta['blog_id'] ) && get_network()->site_id != $meta['blog_id'] ) {
            switch_to_blog( $meta['blog_id'] );
    
            add_filter( 'wp_mail', 'wpmudev_mu_filter_mail_headers', 20 );
        }
    
        return $content;
    
    }, 20, 5 );
    
    //Notification sent after activation. It should be the email in the screenshot:
    //https://www.screencast.com/t/c1KQVVcret
    add_filter( 'update_welcome_user_email', function( $welcome_email, $user_id, $password, $meta ){
    
        if ( isset( $meta['blog_id'] ) && get_network()->site_id != $meta['blog_id'] ) {
            switch_to_blog( $meta['blog_id'] );
    
            add_filter( 'wp_mail', 'wpmudev_mu_filter_mail_headers', 20 );
        }
    
        return $welcome_email;
    }, 20, 4 );
    
    // Filters the email headers. This is only triggered on signup and activation emails
    function wpmudev_mu_filter_mail_headers( $mail_args ){
    
        remove_filter( 'wp_mail', 'wpmudev_mu_filter_mail_headers', 20 );
    
        if ( is_array( $mail_args ) ) {
    
            $admin_email = get_site_option( 'admin_email' );
            if ( $admin_email == '' ) {
                $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
            }
            $from_name = esc_html( get_bloginfo('name') );
            $mail_args['headers'] = "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
    
        }
    
        restore_current_blog();
    	return $mail_args;
    }

    As for removing that part from the email the admin receives (screenshot : https://www.screencast.com/t/TatHnttQfrU)
    you can use the following to filter the email content:

    // Admin notification change:
    add_filter( 'newuser_notify_siteadmin', function( $msg, $user ){
    
        $msg = sprintf(
                __("New User: %s\r\n Remote IP address: %s"),
                $user->user_login, wp_unslash( $_SERVER['REMOTE_ADDR'] )
            );
    
        return $msg;
    }, 50, 2 );

    You can change the content according to your needs, I have kept the same excluding that specific line.

    I would strongly recommend to test the above in a staging site first.

    Hope this helps!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.