private function sendMail(PhabricatorMailTarget $target, PhabricatorRepository $repository, PhabricatorRepositoryPushEvent $event)
 {
     $task_data = $this->getTaskData();
     $viewer = $target->getViewer();
     $locale = PhabricatorEnv::beginScopedLocale($viewer->getTranslation());
     $logs = $event->getLogs();
     list($ref_lines, $ref_list) = $this->renderRefs($logs);
     list($commit_lines, $subject_line) = $this->renderCommits($repository, $logs, idx($task_data, 'info', array()));
     $ref_count = count($ref_lines);
     $commit_count = count($commit_lines);
     $handles = id(new PhabricatorHandleQuery())->setViewer($viewer)->withPHIDs(array($event->getPusherPHID()))->execute();
     $pusher_name = $handles[$event->getPusherPHID()]->getName();
     $repo_name = $repository->getMonogram();
     if ($commit_count) {
         $overview = pht('%s pushed %d commit(s) to %s.', $pusher_name, $commit_count, $repo_name);
     } else {
         $overview = pht('%s pushed to %s.', $pusher_name, $repo_name);
     }
     $details_uri = PhabricatorEnv::getProductionURI('/diffusion/pushlog/view/' . $event->getID() . '/');
     $body = new PhabricatorMetaMTAMailBody();
     $body->addRawSection($overview);
     $body->addLinkSection(pht('DETAILS'), $details_uri);
     if ($commit_lines) {
         $body->addTextSection(pht('COMMITS'), implode("\n", $commit_lines));
     }
     if ($ref_lines) {
         $body->addTextSection(pht('REFERENCES'), implode("\n", $ref_lines));
     }
     $prefix = PhabricatorEnv::getEnvConfig('metamta.diffusion.subject-prefix');
     $parts = array();
     if ($commit_count) {
         $parts[] = pht('%s commit(s)', $commit_count);
     }
     if ($ref_count) {
         $parts[] = implode(', ', $ref_list);
     }
     $parts = implode(', ', $parts);
     if ($subject_line) {
         $subject = pht('(%s) %s', $parts, $subject_line);
     } else {
         $subject = pht('(%s)', $parts);
     }
     $mail = id(new PhabricatorMetaMTAMail())->setRelatedPHID($event->getPHID())->setSubjectPrefix($prefix)->setVarySubjectPrefix(pht('[Push]'))->setSubject($subject)->setFrom($event->getPusherPHID())->setBody($body->render())->setThreadID($event->getPHID(), $is_new = true)->addHeader('Thread-Topic', $subject)->setIsBulk(true);
     $target->sendMail($mail);
 }
 private function buildMailForTarget(PhabricatorLiskDAO $object, array $xactions, PhabricatorMailTarget $target)
 {
     // Check if any of the transactions are visible for this viewer. If we
     // don't have any visible transactions, don't send the mail.
     $any_visible = false;
     foreach ($xactions as $xaction) {
         if (!$xaction->shouldHideForMail($xactions)) {
             $any_visible = true;
             break;
         }
     }
     if (!$any_visible) {
         return null;
     }
     $mail = $this->buildMailTemplate($object);
     $body = $this->buildMailBody($object, $xactions);
     $mail_tags = $this->getMailTags($object, $xactions);
     $action = $this->getMailAction($object, $xactions);
     if (PhabricatorEnv::getEnvConfig('metamta.email-preferences')) {
         $this->addEmailPreferenceSectionToMailBody($body, $object, $xactions);
     }
     $mail->setSensitiveContent(false)->setFrom($this->getActingAsPHID())->setSubjectPrefix($this->getMailSubjectPrefix())->setVarySubjectPrefix('[' . $action . ']')->setThreadID($this->getMailThreadID($object), $this->getIsNewObject())->setRelatedPHID($object->getPHID())->setExcludeMailRecipientPHIDs($this->getExcludeMailRecipientPHIDs())->setForceHeraldMailRecipientPHIDs($this->heraldForcedEmailPHIDs)->setMailTags($mail_tags)->setIsBulk(true)->setBody($body->render())->setHTMLBody($body->renderHTML());
     foreach ($body->getAttachments() as $attachment) {
         $mail->addAttachment($attachment);
     }
     if ($this->heraldHeader) {
         $mail->addHeader('X-Herald-Rules', $this->heraldHeader);
     }
     if ($object instanceof PhabricatorProjectInterface) {
         $this->addMailProjectMetadata($object, $mail);
     }
     if ($this->getParentMessageID()) {
         $mail->setParentMessageID($this->getParentMessageID());
     }
     return $target->willSendMail($mail);
 }