[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/metamta/management/ -> PhabricatorMailManagementSendTestWorkflow.php (source)

   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(
  14          '**send-test** --to alincoln --subject hi < body.txt')
  15        ->setArguments(
  16          array(
  17            array(
  18              'name'    => 'from',
  19              'param'   => 'user',
  20              'help'    => 'Send mail from the specified user.',
  21            ),
  22            array(
  23              'name'    => 'to',
  24              'param'   => 'user',
  25              'help'    => 'Send mail "To:" the specified users.',
  26              'repeat'  => true,
  27            ),
  28            array(
  29              'name'    => 'cc',
  30              'param'   => 'user',
  31              'help'    => 'Send mail which "Cc:"s the specified users.',
  32              'repeat'  => true,
  33            ),
  34            array(
  35              'name'    => 'subject',
  36              'param'   => 'text',
  37              'help'    => 'Use the provided subject.',
  38            ),
  39            array(
  40              'name'    => 'tag',
  41              'param'   => 'text',
  42              'help'    => 'Add the given mail tags.',
  43              'repeat'  => true,
  44            ),
  45            array(
  46              'name'    => 'attach',
  47              'param'   => 'file',
  48              'help'    => 'Attach a file.',
  49              'repeat'  => true,
  50            ),
  51            array(
  52              'name'    => 'html',
  53              'help'    => 'Send as HTML mail.',
  54            ),
  55            array(
  56              'name'    => 'bulk',
  57              'help'    => 'Send with bulk headers.',
  58            ),
  59          ));
  60    }
  61  
  62    public function execute(PhutilArgumentParser $args) {
  63      $console = PhutilConsole::getConsole();
  64      $viewer = $this->getViewer();
  65  
  66      $from = $args->getArg('from');
  67      if ($from) {
  68        $user = id(new PhabricatorPeopleQuery())
  69          ->setViewer($viewer)
  70          ->withUsernames(array($from))
  71          ->executeOne();
  72        if (!$user) {
  73          throw new PhutilArgumentUsageException(
  74            pht("No such user '%s' exists.", $from));
  75        }
  76        $from = $user;
  77      }
  78  
  79      $tos = $args->getArg('to');
  80      $ccs = $args->getArg('cc');
  81  
  82      if (!$tos && !$ccs) {
  83        throw new PhutilArgumentUsageException(
  84          pht(
  85            'Specify one or more users to send mail to with `--to` and '.
  86            '`--cc`.'));
  87      }
  88  
  89      $names = array_merge($tos, $ccs);
  90      $users = id(new PhabricatorPeopleQuery())
  91        ->setViewer($viewer)
  92        ->withUsernames($names)
  93        ->execute();
  94      $users = mpull($users, null, 'getUsername');
  95  
  96      foreach ($tos as $key => $username) {
  97        if (empty($users[$username])) {
  98          throw new PhutilArgumentUsageException(
  99            pht("No such user '%s' exists.", $username));
 100        }
 101        $tos[$key] = $users[$username]->getPHID();
 102      }
 103  
 104      foreach ($ccs as $key => $username) {
 105        if (empty($users[$username])) {
 106          throw new PhutilArgumentUsageException(
 107            pht("No such user '%s' exists.", $username));
 108        }
 109        $ccs[$key] = $users[$username]->getPHID();
 110      }
 111  
 112      $subject = $args->getArg('subject');
 113      if ($subject === null) {
 114        $subject = pht('No Subject');
 115      }
 116  
 117      $tags = $args->getArg('tag');
 118      $attach = $args->getArg('attach');
 119      $is_bulk = $args->getArg('bulk');
 120  
 121      $console->writeErr("%s\n", pht('Reading message body from stdin...'));
 122      $body = file_get_contents('php://stdin');
 123  
 124      $mail = id(new PhabricatorMetaMTAMail())
 125        ->addTos($tos)
 126        ->addCCs($ccs)
 127        ->setSubject($subject)
 128        ->setBody($body)
 129        ->setIsBulk($is_bulk)
 130        ->setMailTags($tags);
 131  
 132      if ($args->getArg('html')) {
 133        $mail->setBody(
 134          pht('(This is a placeholder plaintext email body for a test message '.
 135              'sent with --html.)'));
 136  
 137        $mail->setHTMLBody($body);
 138      } else {
 139        $mail->setBody($body);
 140      }
 141  
 142      if ($from) {
 143        $mail->setFrom($from->getPHID());
 144      }
 145  
 146      foreach ($attach as $attachment) {
 147        $data = Filesystem::readFile($attachment);
 148        $name = basename($attachment);
 149        $mime = Filesystem::getMimeType($attachment);
 150        $file = new PhabricatorMetaMTAAttachment($data, $name, $mime);
 151        $mail->addAttachment($file);
 152      }
 153  
 154      PhabricatorWorker::setRunAllTasksInProcess(true);
 155      $mail->save();
 156  
 157      $console->writeErr(
 158        "%s\n\n    phabricator/ $ ./bin/mail show-outbound --id %d\n\n",
 159        pht('Mail sent! You can view details by running this command:'),
 160        $mail->getID());
 161    }
 162  
 163  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1