/**
  * Identifies the sender's user account for a piece of received mail. Note
  * that this method does not validate that the sender is who they say they
  * are, just that they've presented some credential which corresponds to a
  * recognizable user.
  */
 public function loadSender(PhabricatorMetaMTAReceivedMail $mail)
 {
     $raw_from = $mail->getHeader('From');
     $from = self::getRawAddress($raw_from);
     $reasons = array();
     // Try to find a user with this email address.
     $user = PhabricatorUser::loadOneWithEmailAddress($from);
     if ($user) {
         return $user;
     } else {
         $reasons[] = pht('This email was sent from "%s", but that address is not recognized by ' . 'Phabricator and does not correspond to any known user account.', $raw_from);
     }
     // If we missed on "From", try "Reply-To" if we're configured for it.
     $raw_reply_to = $mail->getHeader('Reply-To');
     if (strlen($raw_reply_to)) {
         $reply_to_key = 'metamta.insecure-auth-with-reply-to';
         $allow_reply_to = PhabricatorEnv::getEnvConfig($reply_to_key);
         if ($allow_reply_to) {
             $reply_to = self::getRawAddress($raw_reply_to);
             $user = PhabricatorUser::loadOneWithEmailAddress($reply_to);
             if ($user) {
                 return $user;
             } else {
                 $reasons[] = pht('Phabricator is configured to authenticate users using the ' . '"Reply-To" header, but the reply address ("%s") on this ' . 'message does not correspond to any known user account.', $raw_reply_to);
             }
         } else {
             $reasons[] = pht('(Phabricator is not configured to authenticate users using the ' . '"Reply-To" header, so it was ignored.)');
         }
     }
     // If we don't know who this user is, load or create an external user
     // account for them if we're configured for it.
     $email_key = 'phabricator.allow-email-users';
     $allow_email_users = PhabricatorEnv::getEnvConfig($email_key);
     if ($allow_email_users) {
         $from_obj = new PhutilEmailAddress($from);
         $xuser = id(new PhabricatorExternalAccountQuery())->setViewer($this->getViewer())->withAccountTypes(array('email'))->withAccountDomains(array($from_obj->getDomainName(), 'self'))->withAccountIDs(array($from_obj->getAddress()))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->loadOneOrCreate();
         return $xuser->getPhabricatorUser();
     } else {
         $reasons[] = pht('Phabricator is also not configured to allow unknown external users ' . 'to send mail to the system using just an email address.');
         $reasons[] = pht('To interact with Phabricator, add this address ("%s") to your ' . 'account.', $raw_from);
     }
     if ($this->getApplicationEmail()) {
         $application_email = $this->getApplicationEmail();
         $default_user_phid = $application_email->getConfigValue(PhabricatorMetaMTAApplicationEmail::CONFIG_DEFAULT_AUTHOR);
         if ($default_user_phid) {
             $user = id(new PhabricatorUser())->loadOneWhere('phid = %s', $default_user_phid);
             if ($user) {
                 return $user;
             }
         }
         $reasons[] = pht("Phabricator is misconfigured, the application email " . "'%s' is set to user '%s' but that user does not exist.", $application_email->getAddress(), $default_user_phid);
     }
     $reasons = implode("\n\n", $reasons);
     throw new PhabricatorMetaMTAReceivedMailProcessingException(MetaMTAReceivedMailStatus::STATUS_UNKNOWN_SENDER, $reasons);
 }
 protected function processReceivedMail(PhabricatorMetaMTAReceivedMail $mail, PhabricatorUser $sender)
 {
     $task = ManiphestTask::initializeNewTask($sender);
     $task->setOriginalEmailSource($mail->getHeader('From'));
     $handler = PhabricatorEnv::newObjectFromConfig('metamta.maniphest.reply-handler');
     $handler->setMailReceiver($task);
     $handler->setActor($sender);
     $handler->setExcludeMailRecipientPHIDs($mail->loadExcludeMailRecipientPHIDs());
     $handler->processEmail($mail);
     $mail->setRelatedPHID($task->getPHID());
 }
 protected function processReceivedMail(PhabricatorMetaMTAReceivedMail $mail, PhabricatorUser $sender)
 {
     $task = ManiphestTask::initializeNewTask($sender);
     $task->setOriginalEmailSource($mail->getHeader('From'));
     $handler = new ManiphestReplyHandler();
     $handler->setMailReceiver($task);
     $handler->setActor($sender);
     $handler->setExcludeMailRecipientPHIDs($mail->loadAllRecipientPHIDs());
     if ($this->getApplicationEmail()) {
         $handler->setApplicationEmail($this->getApplicationEmail());
     }
     $handler->processEmail($mail);
     $mail->setRelatedPHID($task->getPHID());
 }