2013-07-11 00:18:24 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorMailManagementSendTestWorkflow
|
2013-12-27 22:15:48 +01:00
|
|
|
extends PhabricatorMailManagementWorkflow {
|
2013-07-11 00:18:24 +02:00
|
|
|
|
|
|
|
protected function didConstruct() {
|
|
|
|
$this
|
|
|
|
->setName('send-test')
|
|
|
|
->setSynopsis(
|
|
|
|
pht(
|
|
|
|
'Simulate sending mail. This may be useful to test your mail '.
|
|
|
|
'configuration, or while developing new mail adapters.'))
|
2015-05-22 09:27:56 +02:00
|
|
|
->setExamples('**send-test** --to alincoln --subject hi < body.txt')
|
2013-07-11 00:18:24 +02:00
|
|
|
->setArguments(
|
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'name' => 'from',
|
|
|
|
'param' => 'user',
|
2015-05-22 09:27:56 +02:00
|
|
|
'help' => pht('Send mail from the specified user.'),
|
2013-07-11 00:18:24 +02:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'to',
|
|
|
|
'param' => 'user',
|
2015-05-22 09:27:56 +02:00
|
|
|
'help' => pht('Send mail "To:" the specified users.'),
|
2013-07-11 00:18:24 +02:00
|
|
|
'repeat' => true,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'cc',
|
|
|
|
'param' => 'user',
|
2015-05-22 09:27:56 +02:00
|
|
|
'help' => pht('Send mail which "Cc:"s the specified users.'),
|
2013-07-11 00:18:24 +02:00
|
|
|
'repeat' => true,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'subject',
|
|
|
|
'param' => 'text',
|
2015-05-22 09:27:56 +02:00
|
|
|
'help' => pht('Use the provided subject.'),
|
2013-07-11 00:18:24 +02:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'tag',
|
|
|
|
'param' => 'text',
|
2015-05-22 09:27:56 +02:00
|
|
|
'help' => pht('Add the given mail tags.'),
|
2013-07-11 00:18:24 +02:00
|
|
|
'repeat' => true,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'attach',
|
|
|
|
'param' => 'file',
|
2015-05-22 09:27:56 +02:00
|
|
|
'help' => pht('Attach a file.'),
|
2013-07-11 00:18:24 +02:00
|
|
|
'repeat' => true,
|
|
|
|
),
|
2018-02-06 18:29:40 +01:00
|
|
|
array(
|
|
|
|
'name' => 'mailer',
|
|
|
|
'param' => 'key',
|
|
|
|
'help' => pht('Send with a specific configured mailer.'),
|
|
|
|
),
|
2013-07-11 00:18:24 +02:00
|
|
|
array(
|
|
|
|
'name' => 'html',
|
2015-05-22 09:27:56 +02:00
|
|
|
'help' => pht('Send as HTML mail.'),
|
2013-07-11 00:18:24 +02:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
'name' => 'bulk',
|
2015-05-22 09:27:56 +02:00
|
|
|
'help' => pht('Send with bulk headers.'),
|
2013-07-11 00:18:24 +02:00
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute(PhutilArgumentParser $args) {
|
|
|
|
$console = PhutilConsole::getConsole();
|
2013-12-27 22:15:40 +01:00
|
|
|
$viewer = $this->getViewer();
|
2013-07-11 00:18:24 +02:00
|
|
|
|
|
|
|
$from = $args->getArg('from');
|
|
|
|
if ($from) {
|
|
|
|
$user = id(new PhabricatorPeopleQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withUsernames(array($from))
|
|
|
|
->executeOne();
|
|
|
|
if (!$user) {
|
|
|
|
throw new PhutilArgumentUsageException(
|
|
|
|
pht("No such user '%s' exists.", $from));
|
|
|
|
}
|
|
|
|
$from = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tos = $args->getArg('to');
|
|
|
|
$ccs = $args->getArg('cc');
|
|
|
|
|
2014-08-21 20:25:44 +02:00
|
|
|
if (!$tos && !$ccs) {
|
|
|
|
throw new PhutilArgumentUsageException(
|
|
|
|
pht(
|
2015-05-22 09:27:56 +02:00
|
|
|
'Specify one or more users to send mail to with `%s` and `%s`.',
|
|
|
|
'--to',
|
|
|
|
'--cc'));
|
2014-08-21 20:25:44 +02:00
|
|
|
}
|
|
|
|
|
2013-07-11 00:18:24 +02:00
|
|
|
$names = array_merge($tos, $ccs);
|
|
|
|
$users = id(new PhabricatorPeopleQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withUsernames($names)
|
|
|
|
->execute();
|
|
|
|
$users = mpull($users, null, 'getUsername');
|
|
|
|
|
2017-03-10 23:46:51 +01:00
|
|
|
$raw_tos = array();
|
2013-07-11 00:18:24 +02:00
|
|
|
foreach ($tos as $key => $username) {
|
2017-03-10 23:46:51 +01:00
|
|
|
// If the recipient has an "@" in any noninitial position, treat this as
|
|
|
|
// a raw email address.
|
|
|
|
if (preg_match('/.@/', $username)) {
|
|
|
|
$raw_tos[] = $username;
|
|
|
|
unset($tos[$key]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-07-11 00:18:24 +02:00
|
|
|
if (empty($users[$username])) {
|
|
|
|
throw new PhutilArgumentUsageException(
|
|
|
|
pht("No such user '%s' exists.", $username));
|
|
|
|
}
|
|
|
|
$tos[$key] = $users[$username]->getPHID();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($ccs as $key => $username) {
|
|
|
|
if (empty($users[$username])) {
|
|
|
|
throw new PhutilArgumentUsageException(
|
|
|
|
pht("No such user '%s' exists.", $username));
|
|
|
|
}
|
|
|
|
$ccs[$key] = $users[$username]->getPHID();
|
|
|
|
}
|
|
|
|
|
|
|
|
$subject = $args->getArg('subject');
|
2014-08-21 20:25:44 +02:00
|
|
|
if ($subject === null) {
|
|
|
|
$subject = pht('No Subject');
|
|
|
|
}
|
|
|
|
|
2013-07-11 00:18:24 +02:00
|
|
|
$tags = $args->getArg('tag');
|
|
|
|
$attach = $args->getArg('attach');
|
|
|
|
$is_bulk = $args->getArg('bulk');
|
|
|
|
|
|
|
|
$console->writeErr("%s\n", pht('Reading message body from stdin...'));
|
|
|
|
$body = file_get_contents('php://stdin');
|
|
|
|
|
|
|
|
$mail = id(new PhabricatorMetaMTAMail())
|
|
|
|
->addCCs($ccs)
|
|
|
|
->setSubject($subject)
|
|
|
|
->setBody($body)
|
|
|
|
->setIsBulk($is_bulk)
|
|
|
|
->setMailTags($tags);
|
|
|
|
|
2017-03-10 23:46:51 +01:00
|
|
|
if ($tos) {
|
|
|
|
$mail->addTos($tos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($raw_tos) {
|
|
|
|
$mail->addRawTos($raw_tos);
|
|
|
|
}
|
|
|
|
|
2014-08-15 17:04:10 +02:00
|
|
|
if ($args->getArg('html')) {
|
|
|
|
$mail->setBody(
|
2015-05-22 09:27:56 +02:00
|
|
|
pht(
|
|
|
|
'(This is a placeholder plaintext email body for a test message '.
|
|
|
|
'sent with %s.)',
|
|
|
|
'--html'));
|
2014-08-15 17:04:10 +02:00
|
|
|
|
|
|
|
$mail->setHTMLBody($body);
|
|
|
|
} else {
|
|
|
|
$mail->setBody($body);
|
|
|
|
}
|
|
|
|
|
2013-07-11 00:18:24 +02:00
|
|
|
if ($from) {
|
|
|
|
$mail->setFrom($from->getPHID());
|
|
|
|
}
|
|
|
|
|
2018-02-06 18:29:40 +01:00
|
|
|
$mailer_key = $args->getArg('mailer');
|
|
|
|
if ($mailer_key !== null) {
|
|
|
|
$mailers = PhabricatorMetaMTAMail::newMailers();
|
|
|
|
$mailers = mpull($mailers, null, 'getKey');
|
|
|
|
if (!isset($mailers[$mailer_key])) {
|
|
|
|
throw new PhutilArgumentUsageException(
|
|
|
|
pht(
|
|
|
|
'Mailer key ("%s") is not configured. Available keys are: %s.',
|
|
|
|
$mailer_key,
|
|
|
|
implode(', ', array_keys($mailers))));
|
|
|
|
}
|
|
|
|
|
|
|
|
$mail->setTryMailers(array($mailer_key));
|
|
|
|
}
|
|
|
|
|
2014-04-22 00:45:29 +02:00
|
|
|
foreach ($attach as $attachment) {
|
|
|
|
$data = Filesystem::readFile($attachment);
|
|
|
|
$name = basename($attachment);
|
|
|
|
$mime = Filesystem::getMimeType($attachment);
|
|
|
|
$file = new PhabricatorMetaMTAAttachment($data, $name, $mime);
|
|
|
|
$mail->addAttachment($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
PhabricatorWorker::setRunAllTasksInProcess(true);
|
2013-07-11 00:18:24 +02:00
|
|
|
$mail->save();
|
|
|
|
|
|
|
|
$console->writeErr(
|
|
|
|
"%s\n\n phabricator/ $ ./bin/mail show-outbound --id %d\n\n",
|
2014-06-09 20:36:49 +02:00
|
|
|
pht('Mail sent! You can view details by running this command:'),
|
2013-07-11 00:18:24 +02:00
|
|
|
$mail->getID());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|