Track from which subdomain emails are being sent by email headers.

I've started receiving a lot of identical emails from one of my Multisite installations. The emails I receive are all identical, although, one is sent to me at demoname1@mydomain.com and the other to groupmail.mydomain.com (I get them two at a time. How do i track down which sub-domain they're coming from?

    Panos

    Hi Steve!

    From headers only I can't think of any way to see from which blog of your multisite has been sent. BTW here is an article I find informative regarding email headers: https://mediatemple.net/community/products/dv/204643950/understanding-an-email-header

    Only way I can think of is to include the blog id or/and blog name in the content of the email sent. Here is a simple snippet that you can insert in a mu-plugin:

    add_filter( 'wp_mail', function( $atts ){
    
    	$emails = array( 'emailone@gmail.com', 'emailtwo@gmail.com' );
    
    	if( is_multisite() && in_array( $atts[ 'to' ], $emails ) ){
    
    		$sent_by = '<p>Sent by: <br />';
    		$sent_by .= 'Blog id: #' . get_current_blog_id() . '<br />';
    		$sent_by .= 'Blog name: ' . '<a href="' . get_bloginfo('url') . '" >' . get_bloginfo('name'). '</a>';
    		$sent_by .= '</p>';
    
    		$atts[ 'message' ] .= $sent_by;
    
    	}
    
    	return $atts;
    
    }, 10 );

    This should add blog id and title at the end of the email content only if the receiver is one of your specified emails. You will need to modify line
    $emails = array( 'emailone@gmail.com', 'emailtwo@gmail.com' );
    and include the emails you need.

    I suggested using a mu-plugin instead of a theme so that you do not need to paste it in each theme's functions.php as each blog on your multisite may use different themes. If you don't know what is a mu-plugin or how to create on please read through here: https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    In short, all you need to do is create a folder named "mu-plugins" in your wp_content folder if it doesn't already exist. Create a new file with the file extension ".php", eg "custom-functions.php". Open file with an editor (eg notepad) and insert in the begging "<?php". Then you can paste the snippet I shared above under that line.

    Hope this helps!