Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/metamta/management/PhabricatorMailManagementSendTestWorkflow.php
12256 views
1
<?php
2
3
final class PhabricatorMailManagementSendTestWorkflow
4
extends PhabricatorMailManagementWorkflow {
5
6
protected function didConstruct() {
7
$this
8
->setName('send-test')
9
->setSynopsis(
10
pht(
11
'Simulate sending mail. This may be useful to test your mail '.
12
'configuration, or while developing new mail adapters.'))
13
->setExamples('**send-test** --to alincoln --subject hi < body.txt')
14
->setArguments(
15
array(
16
array(
17
'name' => 'from',
18
'param' => 'user',
19
'help' => pht('Send mail from the specified user.'),
20
),
21
array(
22
'name' => 'to',
23
'param' => 'user',
24
'help' => pht('Send mail "To:" the specified users.'),
25
'repeat' => true,
26
),
27
array(
28
'name' => 'cc',
29
'param' => 'user',
30
'help' => pht('Send mail which "Cc:"s the specified users.'),
31
'repeat' => true,
32
),
33
array(
34
'name' => 'subject',
35
'param' => 'text',
36
'help' => pht('Use the provided subject.'),
37
),
38
array(
39
'name' => 'tag',
40
'param' => 'text',
41
'help' => pht('Add the given mail tags.'),
42
'repeat' => true,
43
),
44
array(
45
'name' => 'attach',
46
'param' => 'file',
47
'help' => pht('Attach a file.'),
48
'repeat' => true,
49
),
50
array(
51
'name' => 'mailer',
52
'param' => 'key',
53
'help' => pht('Send with a specific configured mailer.'),
54
),
55
array(
56
'name' => 'html',
57
'help' => pht('Send as HTML mail.'),
58
),
59
array(
60
'name' => 'bulk',
61
'help' => pht('Send with bulk headers.'),
62
),
63
array(
64
'name' => 'type',
65
'param' => 'message-type',
66
'help' => pht(
67
'Send the specified type of message (email, sms, ...).'),
68
),
69
));
70
}
71
72
public function execute(PhutilArgumentParser $args) {
73
$console = PhutilConsole::getConsole();
74
$viewer = $this->getViewer();
75
76
$type = $args->getArg('type');
77
if ($type === null || !strlen($type)) {
78
$type = PhabricatorMailEmailMessage::MESSAGETYPE;
79
}
80
81
$type_map = PhabricatorMailExternalMessage::getAllMessageTypes();
82
if (!isset($type_map[$type])) {
83
throw new PhutilArgumentUsageException(
84
pht(
85
'Message type "%s" is unknown, supported message types are: %s.',
86
$type,
87
implode(', ', array_keys($type_map))));
88
}
89
90
$from = $args->getArg('from');
91
if ($from) {
92
$user = id(new PhabricatorPeopleQuery())
93
->setViewer($viewer)
94
->withUsernames(array($from))
95
->executeOne();
96
if (!$user) {
97
throw new PhutilArgumentUsageException(
98
pht("No such user '%s' exists.", $from));
99
}
100
$from = $user;
101
}
102
103
$tos = $args->getArg('to');
104
$ccs = $args->getArg('cc');
105
106
if (!$tos && !$ccs) {
107
throw new PhutilArgumentUsageException(
108
pht(
109
'Specify one or more users to send a message to with "--to" and/or '.
110
'"--cc".'));
111
}
112
113
$names = array_merge($tos, $ccs);
114
$users = id(new PhabricatorPeopleQuery())
115
->setViewer($viewer)
116
->withUsernames($names)
117
->execute();
118
$users = mpull($users, null, 'getUsername');
119
120
$raw_tos = array();
121
foreach ($tos as $key => $username) {
122
// If the recipient has an "@" in any noninitial position, treat this as
123
// a raw email address.
124
if (preg_match('/.@/', $username)) {
125
$raw_tos[] = $username;
126
unset($tos[$key]);
127
continue;
128
}
129
130
if (empty($users[$username])) {
131
throw new PhutilArgumentUsageException(
132
pht("No such user '%s' exists.", $username));
133
}
134
$tos[$key] = $users[$username]->getPHID();
135
}
136
137
foreach ($ccs as $key => $username) {
138
if (empty($users[$username])) {
139
throw new PhutilArgumentUsageException(
140
pht("No such user '%s' exists.", $username));
141
}
142
$ccs[$key] = $users[$username]->getPHID();
143
}
144
145
$subject = $args->getArg('subject');
146
if ($subject === null) {
147
$subject = pht('No Subject');
148
}
149
150
$tags = $args->getArg('tag');
151
$attach = $args->getArg('attach');
152
$is_bulk = $args->getArg('bulk');
153
154
$console->writeErr("%s\n", pht('Reading message body from stdin...'));
155
$body = file_get_contents('php://stdin');
156
157
$mail = id(new PhabricatorMetaMTAMail())
158
->addCCs($ccs)
159
->setSubject($subject)
160
->setBody($body)
161
->setIsBulk($is_bulk)
162
->setMailTags($tags);
163
164
if ($tos) {
165
$mail->addTos($tos);
166
}
167
168
if ($raw_tos) {
169
$mail->addRawTos($raw_tos);
170
}
171
172
if ($args->getArg('html')) {
173
$mail->setBody(
174
pht(
175
'(This is a placeholder plaintext email body for a test message '.
176
'sent with %s.)',
177
'--html'));
178
179
$mail->setHTMLBody($body);
180
} else {
181
$mail->setBody($body);
182
}
183
184
if ($from) {
185
$mail->setFrom($from->getPHID());
186
}
187
188
$mailers = PhabricatorMetaMTAMail::newMailers(
189
array(
190
'media' => array($type),
191
'outbound' => true,
192
));
193
$mailers = mpull($mailers, null, 'getKey');
194
195
if (!$mailers) {
196
throw new PhutilArgumentUsageException(
197
pht(
198
'No configured mailers support outbound messages of type "%s".',
199
$type));
200
}
201
202
$mailer_key = $args->getArg('mailer');
203
if ($mailer_key !== null) {
204
if (!isset($mailers[$mailer_key])) {
205
throw new PhutilArgumentUsageException(
206
pht(
207
'Mailer key ("%s") is not configured, or does not support '.
208
'outbound messages of type "%s". Available mailers are: %s.',
209
$mailer_key,
210
$type,
211
implode(', ', array_keys($mailers))));
212
}
213
214
$mail->setTryMailers(array($mailer_key));
215
}
216
217
foreach ($attach as $attachment) {
218
$data = Filesystem::readFile($attachment);
219
$name = basename($attachment);
220
$mime = Filesystem::getMimeType($attachment);
221
$file = new PhabricatorMailAttachment($data, $name, $mime);
222
$mail->addAttachment($file);
223
}
224
225
$mail->setMessageType($type);
226
227
PhabricatorWorker::setRunAllTasksInProcess(true);
228
$mail->save();
229
230
$console->writeErr(
231
"%s\n\n phabricator/ $ ./bin/mail show-outbound --id %d\n\n",
232
pht('Mail sent! You can view details by running this command:'),
233
$mail->getID());
234
}
235
236
}
237
238