Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / metamta / management / PhabricatorMailManagementSendTestWorkflow.php
blobf390ff27dfa69bdecc37de666a464b41ca74e0ea
1 <?php
3 final class PhabricatorMailManagementSendTestWorkflow
4 extends PhabricatorMailManagementWorkflow {
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.'),
21 array(
22 'name' => 'to',
23 'param' => 'user',
24 'help' => pht('Send mail "To:" the specified users.'),
25 'repeat' => true,
27 array(
28 'name' => 'cc',
29 'param' => 'user',
30 'help' => pht('Send mail which "Cc:"s the specified users.'),
31 'repeat' => true,
33 array(
34 'name' => 'subject',
35 'param' => 'text',
36 'help' => pht('Use the provided subject.'),
38 array(
39 'name' => 'tag',
40 'param' => 'text',
41 'help' => pht('Add the given mail tags.'),
42 'repeat' => true,
44 array(
45 'name' => 'attach',
46 'param' => 'file',
47 'help' => pht('Attach a file.'),
48 'repeat' => true,
50 array(
51 'name' => 'mailer',
52 'param' => 'key',
53 'help' => pht('Send with a specific configured mailer.'),
55 array(
56 'name' => 'html',
57 'help' => pht('Send as HTML mail.'),
59 array(
60 'name' => 'bulk',
61 'help' => pht('Send with bulk headers.'),
63 array(
64 'name' => 'type',
65 'param' => 'message-type',
66 'help' => pht(
67 'Send the specified type of message (email, sms, ...).'),
69 ));
72 public function execute(PhutilArgumentParser $args) {
73 $console = PhutilConsole::getConsole();
74 $viewer = $this->getViewer();
76 $type = $args->getArg('type');
77 if (!strlen($type)) {
78 $type = PhabricatorMailEmailMessage::MESSAGETYPE;
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))));
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));
100 $from = $user;
103 $tos = $args->getArg('to');
104 $ccs = $args->getArg('cc');
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".'));
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');
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;
130 if (empty($users[$username])) {
131 throw new PhutilArgumentUsageException(
132 pht("No such user '%s' exists.", $username));
134 $tos[$key] = $users[$username]->getPHID();
137 foreach ($ccs as $key => $username) {
138 if (empty($users[$username])) {
139 throw new PhutilArgumentUsageException(
140 pht("No such user '%s' exists.", $username));
142 $ccs[$key] = $users[$username]->getPHID();
145 $subject = $args->getArg('subject');
146 if ($subject === null) {
147 $subject = pht('No Subject');
150 $tags = $args->getArg('tag');
151 $attach = $args->getArg('attach');
152 $is_bulk = $args->getArg('bulk');
154 $console->writeErr("%s\n", pht('Reading message body from stdin...'));
155 $body = file_get_contents('php://stdin');
157 $mail = id(new PhabricatorMetaMTAMail())
158 ->addCCs($ccs)
159 ->setSubject($subject)
160 ->setBody($body)
161 ->setIsBulk($is_bulk)
162 ->setMailTags($tags);
164 if ($tos) {
165 $mail->addTos($tos);
168 if ($raw_tos) {
169 $mail->addRawTos($raw_tos);
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'));
179 $mail->setHTMLBody($body);
180 } else {
181 $mail->setBody($body);
184 if ($from) {
185 $mail->setFrom($from->getPHID());
188 $mailers = PhabricatorMetaMTAMail::newMailers(
189 array(
190 'media' => array($type),
191 'outbound' => true,
193 $mailers = mpull($mailers, null, 'getKey');
195 if (!$mailers) {
196 throw new PhutilArgumentUsageException(
197 pht(
198 'No configured mailers support outbound messages of type "%s".',
199 $type));
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))));
214 $mail->setTryMailers(array($mailer_key));
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);
225 $mail->setMessageType($type);
227 PhabricatorWorker::setRunAllTasksInProcess(true);
228 $mail->save();
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());