Path: blob/master/src/applications/metamta/management/PhabricatorMailManagementShowOutboundWorkflow.php
12256 views
<?php12final class PhabricatorMailManagementShowOutboundWorkflow3extends PhabricatorMailManagementWorkflow {45protected function didConstruct() {6$this7->setName('show-outbound')8->setSynopsis(pht('Show diagnostic details about outbound mail.'))9->setExamples(10'**show-outbound** --id 1 --id 2')11->setArguments(12array(13array(14'name' => 'id',15'param' => 'id',16'help' => pht('Show details about outbound mail with given ID.'),17'repeat' => true,18),19array(20'name' => 'dump-html',21'help' => pht(22'Dump the HTML body of the mail. You can redirect it to a '.23'file and then open it in a browser.'),24),25));26}2728public function execute(PhutilArgumentParser $args) {29$console = PhutilConsole::getConsole();3031$ids = $args->getArg('id');32if (!$ids) {33throw new PhutilArgumentUsageException(34pht(35"Use the '%s' flag to specify one or more messages to show.",36'--id'));37}3839foreach ($ids as $id) {40if (!ctype_digit($id)) {41throw new PhutilArgumentUsageException(42pht(43'Argument "%s" is not a valid message ID.',44$id));45}46}4748$messages = id(new PhabricatorMetaMTAMail())->loadAllWhere(49'id IN (%Ld)',50$ids);5152if ($ids) {53$ids = array_fuse($ids);54$missing = array_diff_key($ids, $messages);55if ($missing) {56throw new PhutilArgumentUsageException(57pht(58'Some specified messages do not exist: %s',59implode(', ', array_keys($missing))));60}61}6263$last_key = last_key($messages);64foreach ($messages as $message_key => $message) {65if ($args->getArg('dump-html')) {66$html_body = $message->getHTMLBody();67if (strlen($html_body)) {68$template =69"<!doctype html><html><body>{$html_body}</body></html>";70$console->writeOut("%s\n", $html_body);71} else {72$console->writeErr(73"%s\n",74pht('(This message has no HTML body.)'));75}76continue;77}7879$info = array();8081$info[] = $this->newSectionHeader(pht('PROPERTIES'));82$info[] = pht('ID: %d', $message->getID());83$info[] = pht('Status: %s', $message->getStatus());84$info[] = pht('Related PHID: %s', $message->getRelatedPHID());85$info[] = pht('Message: %s', $message->getMessage());8687$ignore = array(88'body' => true,89'body.sent' => true,90'html-body' => true,91'headers' => true,92'attachments' => true,93'headers.sent' => true,94'headers.unfiltered' => true,95'authors.sent' => true,96);9798$info[] = null;99$info[] = $this->newSectionHeader(pht('PARAMETERS'));100$parameters = $message->getParameters();101foreach ($parameters as $key => $value) {102if (isset($ignore[$key])) {103continue;104}105106if (!is_scalar($value)) {107$value = json_encode($value);108}109110$info[] = pht('%s: %s', $key, $value);111}112113$info[] = null;114$info[] = $this->newSectionHeader(pht('HEADERS'));115116$headers = $message->getDeliveredHeaders();117if (!$headers) {118$headers = array();119}120121$unfiltered = $message->getUnfilteredHeaders();122if (!$unfiltered) {123$unfiltered = array();124}125126$header_map = array();127foreach ($headers as $header) {128list($name, $value) = $header;129$header_map[$name.':'.$value] = true;130}131132foreach ($unfiltered as $header) {133list($name, $value) = $header;134$was_sent = isset($header_map[$name.':'.$value]);135136if ($was_sent) {137$marker = ' ';138} else {139$marker = '#';140}141142$info[] = "{$marker} {$name}: {$value}";143}144145$attachments = idx($parameters, 'attachments');146if ($attachments) {147$info[] = null;148149$info[] = $this->newSectionHeader(pht('ATTACHMENTS'));150151foreach ($attachments as $attachment) {152$info[] = idx($attachment, 'filename', pht('Unnamed File'));153}154}155156$all_actors = $message->loadAllActors();157158$actors = $message->getDeliveredActors();159if ($actors) {160$info[] = null;161162$info[] = $this->newSectionHeader(pht('RECIPIENTS'));163164foreach ($actors as $actor_phid => $actor_info) {165$actor = idx($all_actors, $actor_phid);166if ($actor) {167$actor_name = coalesce($actor->getName(), $actor_phid);168} else {169$actor_name = $actor_phid;170}171172$deliverable = $actor_info['deliverable'];173if ($deliverable) {174$info[] = ' '.$actor_name;175} else {176$info[] = '! '.$actor_name;177}178179$reasons = $actor_info['reasons'];180foreach ($reasons as $reason) {181$name = PhabricatorMetaMTAActor::getReasonName($reason);182$desc = PhabricatorMetaMTAActor::getReasonDescription($reason);183$info[] = ' - '.$name.': '.$desc;184}185}186}187188$info[] = null;189$info[] = $this->newSectionHeader(pht('TEXT BODY'));190if (strlen($message->getBody())) {191$info[] = tsprintf('%B', $message->getBody());192} else {193$info[] = pht('(This message has no text body.)');194}195196$delivered_body = $message->getDeliveredBody();197if ($delivered_body !== null) {198$info[] = null;199$info[] = $this->newSectionHeader(pht('BODY AS DELIVERED'), true);200$info[] = tsprintf('%B', $delivered_body);201}202203$info[] = null;204$info[] = $this->newSectionHeader(pht('HTML BODY'));205if (strlen($message->getHTMLBody())) {206$info[] = $message->getHTMLBody();207$info[] = null;208} else {209$info[] = pht('(This message has no HTML body.)');210$info[] = null;211}212213$console->writeOut('%s', implode("\n", $info));214215if ($message_key != $last_key) {216$console->writeOut("\n%s\n\n", str_repeat('-', 80));217}218}219}220221private function newSectionHeader($label, $emphasize = false) {222if ($emphasize) {223return tsprintf('**<bg:yellow> %s </bg>**', $label);224} else {225return tsprintf('**<bg:blue> %s </bg>**', $label);226}227}228229}230231232