public function buildMailSection()
 {
     $inlines = $this->getInlines();
     $comments = mpull($inlines, 'getComment');
     $comments = mpull($comments, null, 'getPHID');
     $parents = $this->loadParents($comments);
     $all_comments = $comments + $parents;
     $this->changesets = $this->loadChangesets($all_comments);
     $this->authors = $this->loadAuthors($all_comments);
     $groups = $this->groupInlines($inlines);
     $hunk_parser = new DifferentialHunkParser();
     $spacer_text = null;
     $spacer_html = phutil_tag('br');
     $section = new PhabricatorMetaMTAMailSection();
     $last_group_key = last_key($groups);
     foreach ($groups as $changeset_id => $group) {
         $changeset = $this->getChangeset($changeset_id);
         if (!$changeset) {
             continue;
         }
         $is_last_group = $changeset_id == $last_group_key;
         $last_inline_key = last_key($group);
         foreach ($group as $inline_key => $inline) {
             $comment = $inline->getComment();
             $parent_phid = $comment->getReplyToCommentPHID();
             $is_last_inline = $inline_key == $last_inline_key;
             $context_text = null;
             $context_html = null;
             if ($parent_phid) {
                 $parent = idx($parents, $parent_phid);
                 if ($parent) {
                     $context_text = $this->renderInline($parent, false, true);
                     $context_html = $this->renderInline($parent, true, true);
                 }
             } else {
                 $patch_text = $this->getPatch($hunk_parser, $comment, false);
                 $context_text = $this->renderPatch($comment, $patch_text, false);
                 $patch_html = $this->getPatch($hunk_parser, $comment, true);
                 $context_html = $this->renderPatch($comment, $patch_html, true);
             }
             $render_text = $this->renderInline($comment, false, false);
             $render_html = $this->renderInline($comment, true, false);
             $section->addPlaintextFragment($context_text);
             $section->addPlaintextFragment($spacer_text);
             $section->addPlaintextFragment($render_text);
             $html_fragment = $this->renderContentBox(array($context_html, $render_html));
             $section->addHTMLFragment($html_fragment);
             if (!$is_last_group || !$is_last_inline) {
                 $section->addPlaintextFragment($spacer_text);
                 $section->addHTMLFragment($spacer_html);
             }
         }
     }
     return $section;
 }
 private function renderPatchForMail(DifferentialDiff $diff)
 {
     $format = PhabricatorEnv::getEnvConfig('metamta.differential.patch-format');
     $patch = id(new DifferentialRawDiffRenderer())->setViewer($this->getActor())->setFormat($format)->setChangesets($diff->getChangesets())->buildPatch();
     $section = new PhabricatorMetaMTAMailSection();
     $section->addHTMLFragment($this->renderPatchHTMLForMail($patch));
     $section->addPlaintextFragment($patch);
     return $section;
 }