diff --git a/include/class.thread.php b/include/class.thread.php
index 3911f714b672fd98287a9ac5578fbbc3d1567473..7ef4cd110aacac03b04c0f2ed4f65e9e12e5d1c2 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -1190,9 +1190,17 @@ implements TemplateVariable {
             ) {
                 if (@$mid_info['userId']) {
                     $mailinfo['userId'] = $mid_info['userId'];
+
+                    $user = User::lookupByEmail($mailinfo['email']);
+                    if ($user && $mailinfo['userId'] != $user->getId())
+                      $mailinfo['userId'] = $user->getId();
                 }
                 elseif (@$mid_info['staffId']) {
                     $mailinfo['staffId'] = $mid_info['staffId'];
+
+                    $staffId = Staff::getIdByEmail($mailinfo['email']);
+                    if ($staffId && $mailinfo['staffId'] != $staffId)
+                      $mailinfo['staffId'] = $staffId;
                 }
 
                 // Capture the user type