diff --git a/include/class.thread.php b/include/class.thread.php
index 9ce4fa5169486b392634733caacbc35e3fa6a457..93774d9220c94e4e3dbcdf16259ccbb7f5d7c034 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -1365,9 +1365,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