Differential D20012 Diff 47811 src/applications/metamta/management/PhabricatorMailManagementSendTestWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/metamta/management/PhabricatorMailManagementSendTestWorkflow.php
| Show First 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | $this | ||||
| array( | array( | ||||
| 'name' => 'html', | 'name' => 'html', | ||||
| 'help' => pht('Send as HTML mail.'), | 'help' => pht('Send as HTML mail.'), | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'name' => 'bulk', | 'name' => 'bulk', | ||||
| 'help' => pht('Send with bulk headers.'), | 'help' => pht('Send with bulk headers.'), | ||||
| ), | ), | ||||
| array( | |||||
| 'name' => 'type', | |||||
| 'param' => 'message-type', | |||||
| 'help' => pht( | |||||
| 'Send the specified type of message (email, sms, ...).'), | |||||
| ), | |||||
| )); | )); | ||||
| } | } | ||||
| public function execute(PhutilArgumentParser $args) { | public function execute(PhutilArgumentParser $args) { | ||||
| $console = PhutilConsole::getConsole(); | $console = PhutilConsole::getConsole(); | ||||
| $viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
| $type = $args->getArg('type'); | |||||
| if (!strlen($type)) { | |||||
| $type = PhabricatorMailEmailMessage::MESSAGETYPE; | |||||
| } | |||||
| $type_map = PhabricatorMailExternalMessage::getAllMessageTypes(); | |||||
| if (!isset($type_map[$type])) { | |||||
| throw new PhutilArgumentUsageException( | |||||
| pht( | |||||
| 'Message type "%s" is unknown, supported message types are: %s.', | |||||
| $type, | |||||
| implode(', ', array_keys($type_map)))); | |||||
| } | |||||
| $from = $args->getArg('from'); | $from = $args->getArg('from'); | ||||
| if ($from) { | if ($from) { | ||||
| $user = id(new PhabricatorPeopleQuery()) | $user = id(new PhabricatorPeopleQuery()) | ||||
| ->setViewer($viewer) | ->setViewer($viewer) | ||||
| ->withUsernames(array($from)) | ->withUsernames(array($from)) | ||||
| ->executeOne(); | ->executeOne(); | ||||
| if (!$user) { | if (!$user) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht("No such user '%s' exists.", $from)); | pht("No such user '%s' exists.", $from)); | ||||
| } | } | ||||
| $from = $user; | $from = $user; | ||||
| } | } | ||||
| $tos = $args->getArg('to'); | $tos = $args->getArg('to'); | ||||
| $ccs = $args->getArg('cc'); | $ccs = $args->getArg('cc'); | ||||
| if (!$tos && !$ccs) { | if (!$tos && !$ccs) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht( | pht( | ||||
| 'Specify one or more users to send mail to with `%s` and `%s`.', | 'Specify one or more users to send a message to with "--to" and/or '. | ||||
| '--to', | '"--cc".')); | ||||
| '--cc')); | |||||
| } | } | ||||
| $names = array_merge($tos, $ccs); | $names = array_merge($tos, $ccs); | ||||
| $users = id(new PhabricatorPeopleQuery()) | $users = id(new PhabricatorPeopleQuery()) | ||||
| ->setViewer($viewer) | ->setViewer($viewer) | ||||
| ->withUsernames($names) | ->withUsernames($names) | ||||
| ->execute(); | ->execute(); | ||||
| $users = mpull($users, null, 'getUsername'); | $users = mpull($users, null, 'getUsername'); | ||||
| ▲ Show 20 Lines • Show All 61 Lines • ▼ Show 20 Lines | public function execute(PhutilArgumentParser $args) { | ||||
| } else { | } else { | ||||
| $mail->setBody($body); | $mail->setBody($body); | ||||
| } | } | ||||
| if ($from) { | if ($from) { | ||||
| $mail->setFrom($from->getPHID()); | $mail->setFrom($from->getPHID()); | ||||
| } | } | ||||
| $mailer_key = $args->getArg('mailer'); | $mailers = PhabricatorMetaMTAMail::newMailers( | ||||
| if ($mailer_key !== null) { | array( | ||||
| $mailers = PhabricatorMetaMTAMail::newMailers(array()); | 'media' => array($type), | ||||
| 'outbound' => true, | |||||
| )); | |||||
| $mailers = mpull($mailers, null, 'getKey'); | $mailers = mpull($mailers, null, 'getKey'); | ||||
| if (!isset($mailers[$mailer_key])) { | |||||
| if (!$mailers) { | |||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht( | pht( | ||||
| 'Mailer key ("%s") is not configured. Available keys are: %s.', | 'No configured mailers support outbound messages of type "%s".', | ||||
| $mailer_key, | $type)); | ||||
| implode(', ', array_keys($mailers)))); | |||||
| } | } | ||||
| if (!$mailers[$mailer_key]->getSupportsOutbound()) { | $mailer_key = $args->getArg('mailer'); | ||||
| if ($mailer_key !== null) { | |||||
| if (!isset($mailers[$mailer_key])) { | |||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht( | pht( | ||||
| 'Mailer ("%s") is not configured to support outbound mail.', | 'Mailer key ("%s") is not configured, or does not support '. | ||||
| $mailer_key)); | 'outbound messages of type "%s". Available mailers are: %s.', | ||||
| $mailer_key, | |||||
| $type, | |||||
| implode(', ', array_keys($mailers)))); | |||||
| } | } | ||||
| $mail->setTryMailers(array($mailer_key)); | $mail->setTryMailers(array($mailer_key)); | ||||
| } | } | ||||
| foreach ($attach as $attachment) { | foreach ($attach as $attachment) { | ||||
| $data = Filesystem::readFile($attachment); | $data = Filesystem::readFile($attachment); | ||||
| $name = basename($attachment); | $name = basename($attachment); | ||||
| $mime = Filesystem::getMimeType($attachment); | $mime = Filesystem::getMimeType($attachment); | ||||
| $file = new PhabricatorMailAttachment($data, $name, $mime); | $file = new PhabricatorMailAttachment($data, $name, $mime); | ||||
| $mail->addAttachment($file); | $mail->addAttachment($file); | ||||
| } | } | ||||
| $mail->setMessageType($type); | |||||
| PhabricatorWorker::setRunAllTasksInProcess(true); | PhabricatorWorker::setRunAllTasksInProcess(true); | ||||
| $mail->save(); | $mail->save(); | ||||
| $console->writeErr( | $console->writeErr( | ||||
| "%s\n\n phabricator/ $ ./bin/mail show-outbound --id %d\n\n", | "%s\n\n phabricator/ $ ./bin/mail show-outbound --id %d\n\n", | ||||
| pht('Mail sent! You can view details by running this command:'), | pht('Mail sent! You can view details by running this command:'), | ||||
| $mail->getID()); | $mail->getID()); | ||||
| } | } | ||||
| } | } | ||||