|
| 1 | +<?php |
| 2 | + |
| 3 | +final class PhabricatorMailManagementReceiveTestWorkflow |
| 4 | + extends PhabricatorSearchManagementWorkflow { |
| 5 | + |
| 6 | + protected function didConstruct() { |
| 7 | + $this |
| 8 | + ->setName('receive-test') |
| 9 | + ->setSynopsis( |
| 10 | + pht( |
| 11 | + 'Simulate receiving mail. This is primarily useful if you are '. |
| 12 | + 'developing new mail receivers.')) |
| 13 | + ->setExamples( |
| 14 | + "**receive-test** --as alincoln --to D123 < body.txt") |
| 15 | + ->setArguments( |
| 16 | + array( |
| 17 | + array( |
| 18 | + 'name' => 'as', |
| 19 | + 'param' => 'user', |
| 20 | + 'help' => 'Act as the specified user.', |
| 21 | + ), |
| 22 | + array( |
| 23 | + 'name' => 'from', |
| 24 | + 'param' => 'email', |
| 25 | + 'help' => 'Simulate mail delivery "From:" the given user.', |
| 26 | + ), |
| 27 | + array( |
| 28 | + 'name' => 'to', |
| 29 | + 'param' => 'object', |
| 30 | + 'help' => 'Simulate mail delivery "To:" the given object.', |
| 31 | + ), |
| 32 | + )); |
| 33 | + } |
| 34 | + |
| 35 | + public function execute(PhutilArgumentParser $args) { |
| 36 | + $console = PhutilConsole::getConsole(); |
| 37 | + |
| 38 | + $as = $args->getArg('as'); |
| 39 | + if (!$as) { |
| 40 | + throw new PhutilArgumentUsageException( |
| 41 | + pht("Use '--as' to specify the acting user.")); |
| 42 | + } |
| 43 | + |
| 44 | + $user = id(new PhabricatorPeopleQuery()) |
| 45 | + ->setViewer(PhabricatorUser::getOmnipotentUser()) |
| 46 | + ->withUsernames(array($as)) |
| 47 | + ->executeOne(); |
| 48 | + if (!$user) { |
| 49 | + throw new PhutilArgumentUsageException( |
| 50 | + pht("No such user '%s' exists.", $as)); |
| 51 | + } |
| 52 | + |
| 53 | + $to = $args->getArg('to'); |
| 54 | + if (!$to) { |
| 55 | + throw new PhutilArgumentUsageException( |
| 56 | + "Use '--to' to specify the receiving object or email address."); |
| 57 | + } |
| 58 | + |
| 59 | + $from = $args->getArg('from'); |
| 60 | + if (!$from) { |
| 61 | + $from = $user->loadPrimaryEmail()->getAddress(); |
| 62 | + } |
| 63 | + |
| 64 | + $console->writeErr("%s\n", pht('Reading message body from stdin...')); |
| 65 | + $body = file_get_contents('php://stdin'); |
| 66 | + |
| 67 | + $received = new PhabricatorMetaMTAReceivedMail(); |
| 68 | + $header_content = array( |
| 69 | + 'Message-ID' => Filesystem::readRandomCharacters(12), |
| 70 | + 'From' => $from, |
| 71 | + ); |
| 72 | + |
| 73 | + if (preg_match('/.+@.+/', $to)) { |
| 74 | + $header_content['to'] = $to; |
| 75 | + } else { |
| 76 | + // We allow the user to use an object name instead of a real address |
| 77 | + // as a convenience. To build the mail, we build a similar message and |
| 78 | + // look for a receiver which will accept it. |
| 79 | + $pseudohash = PhabricatorObjectMailReceiver::computeMailHash('x', 'y'); |
| 80 | + $pseudomail = id(new PhabricatorMetaMTAReceivedMail()) |
| 81 | + ->setHeaders( |
| 82 | + array( |
| 83 | + 'to' => $to.'+1+'.$pseudohash, |
| 84 | + )); |
| 85 | + |
| 86 | + $receivers = id(new PhutilSymbolLoader()) |
| 87 | + ->setAncestorClass('PhabricatorMailReceiver') |
| 88 | + ->loadObjects(); |
| 89 | + |
| 90 | + $receiver = null; |
| 91 | + foreach ($receivers as $possible_receiver) { |
| 92 | + if (!$possible_receiver->isEnabled()) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + if (!$possible_receiver->canAcceptMail($pseudomail)) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + $receiver = $possible_receiver; |
| 99 | + break; |
| 100 | + } |
| 101 | + |
| 102 | + if (!$receiver) { |
| 103 | + throw new Exception( |
| 104 | + pht("No configured mail receiver can accept mail to '%s'.", $to)); |
| 105 | + } |
| 106 | + |
| 107 | + if (!($receiver instanceof PhabricatorObjectMailReceiver)) { |
| 108 | + $class = get_class($receiver); |
| 109 | + throw new Exception( |
| 110 | + "Receiver '%s' accepts mail to '%s', but is not a ". |
| 111 | + "subclass of PhabricatorObjectMailReceiver.", |
| 112 | + $class, |
| 113 | + $to); |
| 114 | + } |
| 115 | + |
| 116 | + $object = $receiver->loadMailReceiverObject($to, $user); |
| 117 | + if (!$object) { |
| 118 | + throw new Exception(pht("No such object '%s'!", $to)); |
| 119 | + } |
| 120 | + |
| 121 | + $hash = PhabricatorObjectMailReceiver::computeMailHash( |
| 122 | + $object->getMailKey(), |
| 123 | + $user->getPHID()); |
| 124 | + |
| 125 | + $header_content['to'] = $to.'+'.$user->getID().'+'.$hash.'@test.com'; |
| 126 | + } |
| 127 | + |
| 128 | + $received->setHeaders($header_content); |
| 129 | + $received->setBodies( |
| 130 | + array( |
| 131 | + 'text' => $body, |
| 132 | + )); |
| 133 | + |
| 134 | + $received->save(); |
| 135 | + $received->processReceivedMail(); |
| 136 | + |
| 137 | + $console->writeErr( |
| 138 | + "%s\n\n phabricator/ $ ./bin/mail show-inbound --id %d\n\n", |
| 139 | + pht("Mail received! You can view details by running this command:"), |
| 140 | + $received->getID()); |
| 141 | + } |
| 142 | + |
| 143 | +} |
0 commit comments