public function send()
 {
     $key = PhabricatorEnv::getEnvConfig('mailgun.api-key');
     $domain = PhabricatorEnv::getEnvConfig('mailgun.domain');
     $params = array();
     $params['to'] = implode(', ', idx($this->params, 'tos', array()));
     $params['subject'] = idx($this->params, 'subject');
     $params['text'] = idx($this->params, 'body');
     if (idx($this->params, 'html-body')) {
         $params['html'] = idx($this->params, 'html-body');
     }
     $from = idx($this->params, 'from');
     if (idx($this->params, 'from-name')) {
         $params['from'] = "{$this->params['from-name']} <{$from}>";
     } else {
         $params['from'] = $from;
     }
     if (idx($this->params, 'reply-to')) {
         $replyto = $this->params['reply-to'];
         $params['h:reply-to'] = implode(', ', $replyto);
     }
     if (idx($this->params, 'ccs')) {
         $params['cc'] = implode(', ', $this->params['ccs']);
     }
     foreach (idx($this->params, 'headers', array()) as $header) {
         list($name, $value) = $header;
         $params['h:' . $name] = $value;
     }
     $future = new HTTPSFuture("https://*****:*****@api.mailgun.net/v2/{$domain}/messages", $params);
     $future->setMethod('POST');
     foreach ($this->attachments as $attachment) {
         $future->attachFileData('attachment', $attachment['data'], $attachment['name'], $attachment['type']);
     }
     list($body) = $future->resolvex();
     $response = json_decode($body, true);
     if (!is_array($response)) {
         throw new Exception("Failed to JSON decode response: {$body}");
     }
     if (!idx($response, 'id')) {
         $message = $response['message'];
         throw new Exception("Request failed with errors: {$message}.");
     }
     return true;
 }
Exemple #2
0
#!/usr/bin/env php
<?php 
$root = dirname(dirname(dirname(__FILE__)));
require_once $root . '/scripts/__init_script__.php';
$args = new PhutilArgumentParser($argv);
$args->parseStandardArguments();
$args->parse(array(array('name' => 'attach', 'param' => 'file', 'help' => pht('Attach a file to the request.')), array('name' => 'url', 'wildcard' => true)));
$uri = $args->getArg('url');
if (count($uri) !== 1) {
    throw new PhutilArgumentUsageException("Specify exactly one URL to retrieve.");
}
$uri = head($uri);
$method = 'GET';
$data = '';
$timeout = 30;
$future = new HTTPSFuture($uri, $data);
$future->setMethod($method);
$future->setTimeout($timeout);
$attach_file = $args->getArg('attach');
if ($attach_file !== null) {
    $future->attachFileData('file', Filesystem::readFile($attach_file), basename($attach_file), Filesystem::getMimeType($attach_file));
}
print_r($future->resolve());