Path: blob/master/src/applications/metamta/management/PhabricatorMailManagementSendTestWorkflow.php
12256 views
<?php12final class PhabricatorMailManagementSendTestWorkflow3extends PhabricatorMailManagementWorkflow {45protected function didConstruct() {6$this7->setName('send-test')8->setSynopsis(9pht(10'Simulate sending mail. This may be useful to test your mail '.11'configuration, or while developing new mail adapters.'))12->setExamples('**send-test** --to alincoln --subject hi < body.txt')13->setArguments(14array(15array(16'name' => 'from',17'param' => 'user',18'help' => pht('Send mail from the specified user.'),19),20array(21'name' => 'to',22'param' => 'user',23'help' => pht('Send mail "To:" the specified users.'),24'repeat' => true,25),26array(27'name' => 'cc',28'param' => 'user',29'help' => pht('Send mail which "Cc:"s the specified users.'),30'repeat' => true,31),32array(33'name' => 'subject',34'param' => 'text',35'help' => pht('Use the provided subject.'),36),37array(38'name' => 'tag',39'param' => 'text',40'help' => pht('Add the given mail tags.'),41'repeat' => true,42),43array(44'name' => 'attach',45'param' => 'file',46'help' => pht('Attach a file.'),47'repeat' => true,48),49array(50'name' => 'mailer',51'param' => 'key',52'help' => pht('Send with a specific configured mailer.'),53),54array(55'name' => 'html',56'help' => pht('Send as HTML mail.'),57),58array(59'name' => 'bulk',60'help' => pht('Send with bulk headers.'),61),62array(63'name' => 'type',64'param' => 'message-type',65'help' => pht(66'Send the specified type of message (email, sms, ...).'),67),68));69}7071public function execute(PhutilArgumentParser $args) {72$console = PhutilConsole::getConsole();73$viewer = $this->getViewer();7475$type = $args->getArg('type');76if ($type === null || !strlen($type)) {77$type = PhabricatorMailEmailMessage::MESSAGETYPE;78}7980$type_map = PhabricatorMailExternalMessage::getAllMessageTypes();81if (!isset($type_map[$type])) {82throw new PhutilArgumentUsageException(83pht(84'Message type "%s" is unknown, supported message types are: %s.',85$type,86implode(', ', array_keys($type_map))));87}8889$from = $args->getArg('from');90if ($from) {91$user = id(new PhabricatorPeopleQuery())92->setViewer($viewer)93->withUsernames(array($from))94->executeOne();95if (!$user) {96throw new PhutilArgumentUsageException(97pht("No such user '%s' exists.", $from));98}99$from = $user;100}101102$tos = $args->getArg('to');103$ccs = $args->getArg('cc');104105if (!$tos && !$ccs) {106throw new PhutilArgumentUsageException(107pht(108'Specify one or more users to send a message to with "--to" and/or '.109'"--cc".'));110}111112$names = array_merge($tos, $ccs);113$users = id(new PhabricatorPeopleQuery())114->setViewer($viewer)115->withUsernames($names)116->execute();117$users = mpull($users, null, 'getUsername');118119$raw_tos = array();120foreach ($tos as $key => $username) {121// If the recipient has an "@" in any noninitial position, treat this as122// a raw email address.123if (preg_match('/.@/', $username)) {124$raw_tos[] = $username;125unset($tos[$key]);126continue;127}128129if (empty($users[$username])) {130throw new PhutilArgumentUsageException(131pht("No such user '%s' exists.", $username));132}133$tos[$key] = $users[$username]->getPHID();134}135136foreach ($ccs as $key => $username) {137if (empty($users[$username])) {138throw new PhutilArgumentUsageException(139pht("No such user '%s' exists.", $username));140}141$ccs[$key] = $users[$username]->getPHID();142}143144$subject = $args->getArg('subject');145if ($subject === null) {146$subject = pht('No Subject');147}148149$tags = $args->getArg('tag');150$attach = $args->getArg('attach');151$is_bulk = $args->getArg('bulk');152153$console->writeErr("%s\n", pht('Reading message body from stdin...'));154$body = file_get_contents('php://stdin');155156$mail = id(new PhabricatorMetaMTAMail())157->addCCs($ccs)158->setSubject($subject)159->setBody($body)160->setIsBulk($is_bulk)161->setMailTags($tags);162163if ($tos) {164$mail->addTos($tos);165}166167if ($raw_tos) {168$mail->addRawTos($raw_tos);169}170171if ($args->getArg('html')) {172$mail->setBody(173pht(174'(This is a placeholder plaintext email body for a test message '.175'sent with %s.)',176'--html'));177178$mail->setHTMLBody($body);179} else {180$mail->setBody($body);181}182183if ($from) {184$mail->setFrom($from->getPHID());185}186187$mailers = PhabricatorMetaMTAMail::newMailers(188array(189'media' => array($type),190'outbound' => true,191));192$mailers = mpull($mailers, null, 'getKey');193194if (!$mailers) {195throw new PhutilArgumentUsageException(196pht(197'No configured mailers support outbound messages of type "%s".',198$type));199}200201$mailer_key = $args->getArg('mailer');202if ($mailer_key !== null) {203if (!isset($mailers[$mailer_key])) {204throw new PhutilArgumentUsageException(205pht(206'Mailer key ("%s") is not configured, or does not support '.207'outbound messages of type "%s". Available mailers are: %s.',208$mailer_key,209$type,210implode(', ', array_keys($mailers))));211}212213$mail->setTryMailers(array($mailer_key));214}215216foreach ($attach as $attachment) {217$data = Filesystem::readFile($attachment);218$name = basename($attachment);219$mime = Filesystem::getMimeType($attachment);220$file = new PhabricatorMailAttachment($data, $name, $mime);221$mail->addAttachment($file);222}223224$mail->setMessageType($type);225226PhabricatorWorker::setRunAllTasksInProcess(true);227$mail->save();228229$console->writeErr(230"%s\n\n phabricator/ $ ./bin/mail show-outbound --id %d\n\n",231pht('Mail sent! You can view details by running this command:'),232$mail->getID());233}234235}236237238