Overview

Namespaces

  • None
  • PHP

Classes

  • Mandrill
  • Mandrill_Exports
  • Mandrill_Inbound
  • Mandrill_Internal
  • Mandrill_Messages
  • Mandrill_Rejects
  • Mandrill_Senders
  • Mandrill_Tags
  • Mandrill_Templates
  • Mandrill_Urls
  • Mandrill_Users
  • Mandrill_Webhooks
  • Mandrill_Whitelists

Exceptions

  • Mandrill_Error
  • Mandrill_HttpError
  • Mandrill_Invalid_Key
  • Mandrill_Invalid_Reject
  • Mandrill_Invalid_Tag_Name
  • Mandrill_Invalid_Template
  • Mandrill_ServiceUnavailable
  • Mandrill_Unknown_Export
  • Mandrill_Unknown_InboundDomain
  • Mandrill_Unknown_Sender
  • Mandrill_Unknown_Template
  • Mandrill_Unknown_Url
  • Mandrill_Unknown_Webhook
  • Mandrill_ValidationError
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: require_once 'Mandrill/Templates.php';
  4: require_once 'Mandrill/Exports.php';
  5: require_once 'Mandrill/Users.php';
  6: require_once 'Mandrill/Rejects.php';
  7: require_once 'Mandrill/Inbound.php';
  8: require_once 'Mandrill/Tags.php';
  9: require_once 'Mandrill/Messages.php';
 10: require_once 'Mandrill/Whitelists.php';
 11: require_once 'Mandrill/Internal.php';
 12: require_once 'Mandrill/Urls.php';
 13: require_once 'Mandrill/Webhooks.php';
 14: require_once 'Mandrill/Senders.php';
 15: require_once 'Mandrill/Exceptions.php';
 16: 
 17: class Mandrill {
 18:     
 19:     public $apikey;
 20:     public $ch;
 21:     public $root = 'https://mandrillapp.com/api/1.0';
 22:     public $debug = false;
 23: 
 24:     public static $error_map = array(
 25:         "ValidationError" => "Mandrill_ValidationError",
 26:         "Invalid_Key" => "Mandrill_Invalid_Key",
 27:         "Unknown_Template" => "Mandrill_Unknown_Template",
 28:         "ServiceUnavailable" => "Mandrill_ServiceUnavailable",
 29:         "Invalid_Tag_Name" => "Mandrill_Invalid_Tag_Name",
 30:         "Invalid_Reject" => "Mandrill_Invalid_Reject",
 31:         "Unknown_Sender" => "Mandrill_Unknown_Sender",
 32:         "Unknown_Url" => "Mandrill_Unknown_Url",
 33:         "Invalid_Template" => "Mandrill_Invalid_Template",
 34:         "Unknown_Webhook" => "Mandrill_Unknown_Webhook",
 35:         "Unknown_InboundDomain" => "Mandrill_Unknown_InboundDomain",
 36:         "Unknown_Export" => "Mandrill_Unknown_Export"
 37:     );
 38: 
 39:     public function __construct($apikey=null) {
 40:         if(!$apikey) $apikey = getenv('MANDRILL_APIKEY');
 41:         if(!$apikey) $apikey = $this->readConfigs();
 42:         if(!$apikey) throw new Mandrill_Error('You must provide a Mandrill API key');
 43:         $this->apikey = $apikey;
 44: 
 45:         $this->ch = curl_init();
 46:         curl_setopt($this->ch, CURLOPT_USERAGENT, 'Mandrill-PHP/1.0.32');
 47:         curl_setopt($this->ch, CURLOPT_POST, true);
 48:         curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
 49:         curl_setopt($this->ch, CURLOPT_HEADER, false);
 50:         curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
 51:         curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 30);
 52:         curl_setopt($this->ch, CURLOPT_TIMEOUT, 600);
 53: 
 54:         $this->root = rtrim($this->root, '/') . '/';
 55: 
 56:         $this->templates = new Mandrill_Templates($this);
 57:         $this->exports = new Mandrill_Exports($this);
 58:         $this->users = new Mandrill_Users($this);
 59:         $this->rejects = new Mandrill_Rejects($this);
 60:         $this->inbound = new Mandrill_Inbound($this);
 61:         $this->tags = new Mandrill_Tags($this);
 62:         $this->messages = new Mandrill_Messages($this);
 63:         $this->whitelists = new Mandrill_Whitelists($this);
 64:         $this->internal = new Mandrill_Internal($this);
 65:         $this->urls = new Mandrill_Urls($this);
 66:         $this->webhooks = new Mandrill_Webhooks($this);
 67:         $this->senders = new Mandrill_Senders($this);
 68:     }
 69: 
 70:     public function __destruct() {
 71:         curl_close($this->ch);
 72:     }
 73: 
 74:     public function call($url, $params) {
 75:         $params['key'] = $this->apikey;
 76:         $params = json_encode($params);
 77:         $ch = $this->ch;
 78: 
 79:         curl_setopt($ch, CURLOPT_URL, $this->root . $url . '.json');
 80:         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
 81:         curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
 82:         curl_setopt($ch, CURLOPT_VERBOSE, $this->debug);
 83: 
 84:         $start = microtime(true);
 85:         $this->log('Call to ' . $this->root . $url . '.json: ' . $params);
 86:         if($this->debug) {
 87:             $curl_buffer = fopen('php://memory', 'w+');
 88:             curl_setopt($ch, CURLOPT_STDERR, $curl_buffer);
 89:         }
 90: 
 91:         $response_body = curl_exec($ch);
 92:         $info = curl_getinfo($ch);
 93:         $time = microtime(true) - $start;
 94:         if($this->debug) {
 95:             rewind($curl_buffer);
 96:             $this->log(stream_get_contents($curl_buffer));
 97:             fclose($curl_buffer);
 98:         }
 99:         $this->log('Completed in ' . number_format($time * 1000, 2) . 'ms');
100:         $this->log('Got response: ' . $response_body);
101: 
102:         if(curl_error($ch)) {
103:             throw new Mandrill_HttpError("API call to $url failed: " . curl_error($ch));
104:         }
105:         $result = json_decode($response_body, true);
106:         if($result === null) throw new Mandrill_Error('We were unable to decode the JSON response from the Mandrill API: ' . $response_body);
107:         
108:         if(floor($info['http_code'] / 100) >= 4) {
109:             throw $this->castError($result);
110:         }
111: 
112:         return $result;
113:     }
114: 
115:     public function readConfigs() {
116:         $paths = array('~/.mandrill.key', '/etc/mandrill.key');
117:         foreach($paths as $path) {
118:             if(file_exists($path)) {
119:                 $apikey = trim(file_get_contents($path));
120:                 if($apikey) return $apikey;
121:             }
122:         }
123:         return false;
124:     }
125: 
126:     public function castError($result) {
127:         if($result['status'] !== 'error' || !$result['name']) throw new Mandrill_Error('We received an unexpected error: ' . json_encode($result));
128: 
129:         $class = (isset(self::$error_map[$result['name']])) ? self::$error_map[$result['name']] : 'Mandrill_Error';
130:         return new $class($result['message'], $result['code']);
131:     }
132: 
133:     public function log($msg) {
134:         if($this->debug) error_log($msg);
135:     }
136: }
137: 
138: 
139: 
API documentation generated by ApiGen 2.8.0