Overview

Namespaces

  • None
  • PHP

Classes

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

Exceptions

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