3 final class PhabricatorMailManagementSendTestWorkflow
4 extends PhabricatorMailManagementWorkflow
{
6 protected function didConstruct() {
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')
19 'help' => pht('Send mail from the specified user.'),
24 'help' => pht('Send mail "To:" the specified users.'),
30 'help' => pht('Send mail which "Cc:"s the specified users.'),
36 'help' => pht('Use the provided subject.'),
41 'help' => pht('Add the given mail tags.'),
47 'help' => pht('Attach a file.'),
53 'help' => pht('Send with a specific configured mailer.'),
57 'help' => pht('Send as HTML mail.'),
61 'help' => pht('Send with bulk headers.'),
65 'param' => 'message-type',
67 'Send the specified type of message (email, sms, ...).'),
72 public function execute(PhutilArgumentParser
$args) {
73 $console = PhutilConsole
::getConsole();
74 $viewer = $this->getViewer();
76 $type = $args->getArg('type');
78 $type = PhabricatorMailEmailMessage
::MESSAGETYPE
;
81 $type_map = PhabricatorMailExternalMessage
::getAllMessageTypes();
82 if (!isset($type_map[$type])) {
83 throw new PhutilArgumentUsageException(
85 'Message type "%s" is unknown, supported message types are: %s.',
87 implode(', ', array_keys($type_map))));
90 $from = $args->getArg('from');
92 $user = id(new PhabricatorPeopleQuery())
94 ->withUsernames(array($from))
97 throw new PhutilArgumentUsageException(
98 pht("No such user '%s' exists.", $from));
103 $tos = $args->getArg('to');
104 $ccs = $args->getArg('cc');
106 if (!$tos && !$ccs) {
107 throw new PhutilArgumentUsageException(
109 'Specify one or more users to send a message to with "--to" and/or '.
113 $names = array_merge($tos, $ccs);
114 $users = id(new PhabricatorPeopleQuery())
116 ->withUsernames($names)
118 $users = mpull($users, null, 'getUsername');
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;
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())
159 ->setSubject($subject)
161 ->setIsBulk($is_bulk)
162 ->setMailTags($tags);
169 $mail->addRawTos($raw_tos);
172 if ($args->getArg('html')) {
175 '(This is a placeholder plaintext email body for a test message '.
179 $mail->setHTMLBody($body);
181 $mail->setBody($body);
185 $mail->setFrom($from->getPHID());
188 $mailers = PhabricatorMetaMTAMail
::newMailers(
190 'media' => array($type),
193 $mailers = mpull($mailers, null, 'getKey');
196 throw new PhutilArgumentUsageException(
198 'No configured mailers support outbound messages of type "%s".',
202 $mailer_key = $args->getArg('mailer');
203 if ($mailer_key !== null) {
204 if (!isset($mailers[$mailer_key])) {
205 throw new PhutilArgumentUsageException(
207 'Mailer key ("%s") is not configured, or does not support '.
208 'outbound messages of type "%s". Available mailers are: %s.',
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);
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:'),