messages to an email - User Messaging Plugin

how can I set it that when a user sends a message - It sends the message to a specific email for me to monitor it all?
It supposed to be simple code integration, can you elaborate where and how?

Thanks

  • Patrick

    Hi there @Guy

    Welcome to the forums, glad to have you aboard!

    Do you mean that you, as admin, want to receive an email notification for each message that user sends to another user?

    If so, that kinda goes against the whole concept of private messaging, don't you think?

    There is likely a way that it can be done though. Perhaps by hooking into whatever action is triggered when the submit button is clicked.

    However, I am not as proficient a coder as I'd like to be, so I'll see if another member of the support staff can lend a hand here.

  • Jose

    Hello @Guy,

    I hope you are well today.

    Unfortunately, there are no hooks to achieve what you want.
    Therefore, you will need to make a some modifications to the plugin. It doesn't require too much coding, you just need to look into the function messaging_new_message_notification() in messaging.php file. It is the function that sends email notifications to the user when a message is sent. So you will need to duplicate the call to wp_mail() in line 398 pointing to the specific email address.
    You should notice that the email is sent only when the user notification settings are enabled, so you might need to duplicate the whole block if you want to send the custom email by default.

    Hope this helps!

    Cheers,
    JJ

  • Guy

    Hi Guys
    You are amazing!

    Thanks!

    I've tried a couple of times but I'm mostly into HTML/CSS so PHP is a little bit hard for me.

    is it possible to ask, if you can modify that block and write it down for me?
    If possible - just tell me where to add it and I'll change the email adress in the relevant place..
    That would be awesome and I'd appreciate you saving me lots of time asking questions in PHP forums :slight_smile:

    Thanks in advanced,
    Guy

  • Jose

    Hi again @Guy,

    This is the modified function. Just replace the old one. I'm also attaching the entire file.

    function messaging_new_message_notification($tmp_to_uid,$tmp_from_uid,$tmp_subject,$tmp_content) {
    	global $wpdb, $current_site, $user_ID, $messaging_email_notification_subject, $messaging_email_notification_content;
    
    	if (is_multisite()) {
    		$SITE_NAME 	= $current_site->site_name;
    		$SITE_URL	= 'http://'. $current_site->domain;
    	} else {
    		$SITE_NAME 	= get_option('blogname');
    		$SITE_URL	= get_option('siteurl');
    	}
    
        $tmp_to_username =  $wpdb->get_var($wpdb->prepare("SELECT user_login FROM " . $wpdb->users . " WHERE ID = %d", $tmp_to_uid));
        $tmp_to_email =  $wpdb->get_var($wpdb->prepare("SELECT user_email FROM " . $wpdb->users . " WHERE ID = %s", $tmp_to_uid));
        $tmp_from_username =  $wpdb->get_var($wpdb->prepare("SELECT user_login FROM " . $wpdb->users . " WHERE ID = %d", $tmp_from_uid));
    
        $message_content = get_site_option('messaging_email_notification_content', $messaging_email_notification_content);
    
        $message_content = str_replace( "SITE_NAME", $SITE_NAME, $message_content );
        $message_content = str_replace( "SITE_URL", $SITE_URL, $message_content );
    
        $message_content = str_replace( "TO_USER", $tmp_to_username, $message_content );
        $message_content = str_replace( "FROM_USER", $tmp_from_username, $message_content );
        $message_content = str_replace( "\'", "'", $message_content );
    
        $subject_content = get_site_option('messaging_email_notification_subject', $messaging_email_notification_subject);
        $subject_content = str_replace( "SITE_NAME", $SITE_NAME, $subject_content );
    
        $admin_email = get_site_option('admin_email');
        if ($admin_email == ''){
            $admin_email = 'admin@' . $current_site->domain;
        }
        $from_email = $admin_email;
    
        $message_headers = "MIME-Version: 1.0\n" . "From: " . $SITE_NAME .  " <{$from_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
    
        //Notify user via email only if notification is enabled for him.
    	if (get_user_meta($tmp_to_uid,'message_email_notification') != 'no'){
    
    		wp_mail($tmp_to_email, $subject_content, $message_content, $message_headers);
    	}
    
        $admin_monitoring_email = "{YOUR_EMAIL_HERE}";//Here you might want ot use $admin_email or define a specific address for monitoring.
        //Notify always to admin via email for monitoring purposes. You better alert your users about this :)
        wp_mail($admin_monitoring_email, $subject_content, $message_content, $message_headers);
    }

    Please let me know if it works as expected.

    Cheers!
    JJ

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.