Skip to content

Commit 7fa2343

Browse files
author
epriestley
committed
Move mail "Receive Test" from web UI to CLI
Summary: Ref T3306. Moves this from the web to the CLI, which is a tiny bit clunkier but way better as far as policies go and more repeatable for development. See discussion in D6413. Test Plan: Ran `bin/mail receive-test`, verified mail was received. Used and abused various options. Reviewers: btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T3306 Differential Revision: https://secure.phabricator.com/D6417
1 parent 70fd3dd commit 7fa2343

File tree

6 files changed

+146
-146
lines changed

6 files changed

+146
-146
lines changed

scripts/mail/manage_mail.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
new PhutilHelpArgumentWorkflow(),
2020
new PhabricatorMailManagementShowOutboundWorkflow(),
2121
new PhabricatorMailManagementShowInboundWorkflow(),
22+
new PhabricatorMailManagementReceiveTestWorkflow(),
2223
);
2324

2425
$args->parseWorkflows($workflows);

src/__phutil_library_map__.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,7 @@
12111211
'PhabricatorMailImplementationPHPMailerLiteAdapter' => 'applications/metamta/adapter/PhabricatorMailImplementationPHPMailerLiteAdapter.php',
12121212
'PhabricatorMailImplementationSendGridAdapter' => 'applications/metamta/adapter/PhabricatorMailImplementationSendGridAdapter.php',
12131213
'PhabricatorMailImplementationTestAdapter' => 'applications/metamta/adapter/PhabricatorMailImplementationTestAdapter.php',
1214+
'PhabricatorMailManagementReceiveTestWorkflow' => 'applications/metamta/management/PhabricatorMailManagementReceiveTestWorkflow.php',
12141215
'PhabricatorMailManagementResendWorkflow' => 'applications/metamta/management/PhabricatorMailManagementResendWorkflow.php',
12151216
'PhabricatorMailManagementShowInboundWorkflow' => 'applications/metamta/management/PhabricatorMailManagementShowInboundWorkflow.php',
12161217
'PhabricatorMailManagementShowOutboundWorkflow' => 'applications/metamta/management/PhabricatorMailManagementShowOutboundWorkflow.php',
@@ -1244,7 +1245,6 @@
12441245
'PhabricatorMetaMTAMailBodyTestCase' => 'applications/metamta/view/__tests__/PhabricatorMetaMTAMailBodyTestCase.php',
12451246
'PhabricatorMetaMTAMailTestCase' => 'applications/metamta/storage/__tests__/PhabricatorMetaMTAMailTestCase.php',
12461247
'PhabricatorMetaMTAMailingList' => 'applications/mailinglists/storage/PhabricatorMetaMTAMailingList.php',
1247-
'PhabricatorMetaMTAReceiveController' => 'applications/metamta/controller/PhabricatorMetaMTAReceiveController.php',
12481248
'PhabricatorMetaMTAReceivedListController' => 'applications/metamta/controller/PhabricatorMetaMTAReceivedListController.php',
12491249
'PhabricatorMetaMTAReceivedMail' => 'applications/metamta/storage/PhabricatorMetaMTAReceivedMail.php',
12501250
'PhabricatorMetaMTAReceivedMailProcessingException' => 'applications/metamta/exception/PhabricatorMetaMTAReceivedMailProcessingException.php',
@@ -3166,6 +3166,7 @@
31663166
'PhabricatorMailImplementationPHPMailerLiteAdapter' => 'PhabricatorMailImplementationAdapter',
31673167
'PhabricatorMailImplementationSendGridAdapter' => 'PhabricatorMailImplementationAdapter',
31683168
'PhabricatorMailImplementationTestAdapter' => 'PhabricatorMailImplementationAdapter',
3169+
'PhabricatorMailManagementReceiveTestWorkflow' => 'PhabricatorSearchManagementWorkflow',
31693170
'PhabricatorMailManagementResendWorkflow' => 'PhabricatorSearchManagementWorkflow',
31703171
'PhabricatorMailManagementShowInboundWorkflow' => 'PhabricatorSearchManagementWorkflow',
31713172
'PhabricatorMailManagementShowOutboundWorkflow' => 'PhabricatorSearchManagementWorkflow',
@@ -3191,7 +3192,6 @@
31913192
'PhabricatorMetaMTAMailBodyTestCase' => 'PhabricatorTestCase',
31923193
'PhabricatorMetaMTAMailTestCase' => 'PhabricatorTestCase',
31933194
'PhabricatorMetaMTAMailingList' => 'PhabricatorMetaMTADAO',
3194-
'PhabricatorMetaMTAReceiveController' => 'PhabricatorMetaMTAController',
31953195
'PhabricatorMetaMTAReceivedListController' => 'PhabricatorMetaMTAController',
31963196
'PhabricatorMetaMTAReceivedMail' => 'PhabricatorMetaMTADAO',
31973197
'PhabricatorMetaMTAReceivedMailProcessingException' => 'Exception',

src/applications/metamta/application/PhabricatorApplicationMetaMTA.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public function getRoutes() {
3232
'' => 'PhabricatorMetaMTAListController',
3333
'send/' => 'PhabricatorMetaMTASendController',
3434
'view/(?P<id>[1-9]\d*)/' => 'PhabricatorMetaMTAViewController',
35-
'receive/' => 'PhabricatorMetaMTAReceiveController',
3635
'received/' => 'PhabricatorMetaMTAReceivedListController',
3736
'sendgrid/' => 'PhabricatorMetaMTASendGridReceiveController',
3837
),

src/applications/metamta/controller/PhabricatorMetaMTAController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public function buildSideNavView() {
1717
if ($this->getRequest()->getUser()->getIsAdmin()) {
1818
$nav->addLabel(pht('Diagnostics'));
1919
$nav->addFilter('send', pht('Send Test'));
20-
$nav->addFilter('receive', pht('Receive Test'));
2120
}
2221

2322
return $nav;

src/applications/metamta/controller/PhabricatorMetaMTAReceiveController.php

Lines changed: 0 additions & 142 deletions
This file was deleted.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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

Comments
 (0)