Path: blob/master/src/applications/metamta/management/PhabricatorMailManagementReceiveTestWorkflow.php
12256 views
<?php12final class PhabricatorMailManagementReceiveTestWorkflow3extends PhabricatorMailManagementWorkflow {45protected function didConstruct() {6$this7->setName('receive-test')8->setSynopsis(9pht(10'Simulate receiving mail. This is primarily useful if you are '.11'developing new mail receivers.'))12->setExamples(13'**receive-test** --as alincoln --to D123 < body.txt')14->setArguments(15array(16array(17'name' => 'as',18'param' => 'user',19'help' => pht('Act as the specified user.'),20),21array(22'name' => 'from',23'param' => 'email',24'help' => pht('Simulate mail delivery "From:" the given user.'),25),26array(27'name' => 'to',28'param' => 'object',29'help' => pht('Simulate mail delivery "To:" the given object.'),30),31array(32'name' => 'cc',33'param' => 'address',34'help' => pht('Simulate a mail delivery "Cc:" address.'),35'repeat' => true,36),37));38}3940public function execute(PhutilArgumentParser $args) {41$viewer = $this->getViewer();42$console = PhutilConsole::getConsole();4344$to = $args->getArg('to');45if (!$to) {46throw new PhutilArgumentUsageException(47pht(48"Use '%s' to specify the receiving object or email address.",49'--to'));50}5152$to_application_email = id(new PhabricatorMetaMTAApplicationEmailQuery())53->setViewer($this->getViewer())54->withAddresses(array($to))55->executeOne();5657$as = $args->getArg('as');58if (!$as && $to_application_email) {59$default_phid = $to_application_email->getConfigValue(60PhabricatorMetaMTAApplicationEmail::CONFIG_DEFAULT_AUTHOR);61if ($default_phid) {62$default_user = id(new PhabricatorPeopleQuery())63->setViewer($this->getViewer())64->withPHIDs(array($default_phid))65->executeOne();66if ($default_user) {67$as = $default_user->getUsername();68}69}70}7172if (!$as) {73throw new PhutilArgumentUsageException(74pht("Use '--as' to specify the acting user."));75}7677$user = id(new PhabricatorPeopleQuery())78->setViewer($this->getViewer())79->withUsernames(array($as))80->executeOne();81if (!$user) {82throw new PhutilArgumentUsageException(83pht("No such user '%s' exists.", $as));84}858687$from = $args->getArg('from');88if (!$from) {89$from = $user->loadPrimaryEmail()->getAddress();90}9192$cc = $args->getArg('cc');9394$console->writeErr("%s\n", pht('Reading message body from stdin...'));95$body = file_get_contents('php://stdin');9697$received = new PhabricatorMetaMTAReceivedMail();98$header_content = array(99'Message-ID' => Filesystem::readRandomCharacters(12),100'From' => $from,101'Cc' => implode(', ', $cc),102);103104if (preg_match('/.+@.+/', $to)) {105$header_content['to'] = $to;106} else {107108// We allow the user to use an object name instead of a real address109// as a convenience. To build the mail, we build a similar message and110// look for a receiver which will accept it.111112// In the general case, mail may be processed by multiple receivers,113// but mail to objects only ever has one receiver today.114115$pseudohash = PhabricatorObjectMailReceiver::computeMailHash('x', 'y');116117$raw_target = $to.'+1+'.$pseudohash;118$target = new PhutilEmailAddress($raw_target.'@local.cli');119120$pseudomail = id(new PhabricatorMetaMTAReceivedMail())121->setHeaders(122array(123'to' => $raw_target,124));125126$receivers = id(new PhutilClassMapQuery())127->setAncestorClass('PhabricatorMailReceiver')128->setFilterMethod('isEnabled')129->execute();130131$receiver = null;132foreach ($receivers as $possible_receiver) {133$possible_receiver = id(clone $possible_receiver)134->setViewer($viewer)135->setSender($user);136137if (!$possible_receiver->canAcceptMail($pseudomail, $target)) {138continue;139}140$receiver = $possible_receiver;141break;142}143144if (!$receiver) {145throw new Exception(146pht("No configured mail receiver can accept mail to '%s'.", $to));147}148149if (!($receiver instanceof PhabricatorObjectMailReceiver)) {150$class = get_class($receiver);151throw new Exception(152pht(153"Receiver '%s' accepts mail to '%s', but is not a ".154"subclass of PhabricatorObjectMailReceiver.",155$class,156$to));157}158159$object = $receiver->loadMailReceiverObject($to, $user);160if (!$object) {161throw new Exception(pht("No such object '%s'!", $to));162}163164$mail_key = PhabricatorMetaMTAMailProperties::loadMailKey($object);165166$hash = PhabricatorObjectMailReceiver::computeMailHash(167$mail_key,168$user->getPHID());169170$header_content['to'] = $to.'+'.$user->getID().'+'.$hash.'@test.com';171}172173$received->setHeaders($header_content);174$received->setBodies(175array(176'text' => $body,177));178179$received->save();180$received->processReceivedMail();181182$console->writeErr(183"%s\n\n phabricator/ $ ./bin/mail show-inbound --id %d\n\n",184pht('Mail received! You can view details by running this command:'),185$received->getID());186}187188}189190191