-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3cd398
commit 6992702
Showing
17 changed files
with
2,617 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
|
||
require_once 'Mandrill/Templates.php'; | ||
require_once 'Mandrill/Exports.php'; | ||
require_once 'Mandrill/Users.php'; | ||
require_once 'Mandrill/Rejects.php'; | ||
require_once 'Mandrill/Inbound.php'; | ||
require_once 'Mandrill/Tags.php'; | ||
require_once 'Mandrill/Messages.php'; | ||
require_once 'Mandrill/Whitelists.php'; | ||
require_once 'Mandrill/Ips.php'; | ||
require_once 'Mandrill/Internal.php'; | ||
require_once 'Mandrill/Subaccounts.php'; | ||
require_once 'Mandrill/Urls.php'; | ||
require_once 'Mandrill/Webhooks.php'; | ||
require_once 'Mandrill/Senders.php'; | ||
require_once 'Mandrill/Metadata.php'; | ||
require_once 'Mandrill/Exceptions.php'; | ||
|
||
class Mandrill { | ||
|
||
public $apikey; | ||
public $ch; | ||
public $root = 'https://mandrillapp.com/api/1.0'; | ||
public $debug = false; | ||
|
||
public static $error_map = array( | ||
"ValidationError" => "Mandrill_ValidationError", | ||
"Invalid_Key" => "Mandrill_Invalid_Key", | ||
"PaymentRequired" => "Mandrill_PaymentRequired", | ||
"Unknown_Subaccount" => "Mandrill_Unknown_Subaccount", | ||
"Unknown_Template" => "Mandrill_Unknown_Template", | ||
"ServiceUnavailable" => "Mandrill_ServiceUnavailable", | ||
"Unknown_Message" => "Mandrill_Unknown_Message", | ||
"Invalid_Tag_Name" => "Mandrill_Invalid_Tag_Name", | ||
"Invalid_Reject" => "Mandrill_Invalid_Reject", | ||
"Unknown_Sender" => "Mandrill_Unknown_Sender", | ||
"Unknown_Url" => "Mandrill_Unknown_Url", | ||
"Unknown_TrackingDomain" => "Mandrill_Unknown_TrackingDomain", | ||
"Invalid_Template" => "Mandrill_Invalid_Template", | ||
"Unknown_Webhook" => "Mandrill_Unknown_Webhook", | ||
"Unknown_InboundDomain" => "Mandrill_Unknown_InboundDomain", | ||
"Unknown_InboundRoute" => "Mandrill_Unknown_InboundRoute", | ||
"Unknown_Export" => "Mandrill_Unknown_Export", | ||
"IP_ProvisionLimit" => "Mandrill_IP_ProvisionLimit", | ||
"Unknown_Pool" => "Mandrill_Unknown_Pool", | ||
"NoSendingHistory" => "Mandrill_NoSendingHistory", | ||
"PoorReputation" => "Mandrill_PoorReputation", | ||
"Unknown_IP" => "Mandrill_Unknown_IP", | ||
"Invalid_EmptyDefaultPool" => "Mandrill_Invalid_EmptyDefaultPool", | ||
"Invalid_DeleteDefaultPool" => "Mandrill_Invalid_DeleteDefaultPool", | ||
"Invalid_DeleteNonEmptyPool" => "Mandrill_Invalid_DeleteNonEmptyPool", | ||
"Invalid_CustomDNS" => "Mandrill_Invalid_CustomDNS", | ||
"Invalid_CustomDNSPending" => "Mandrill_Invalid_CustomDNSPending", | ||
"Metadata_FieldLimit" => "Mandrill_Metadata_FieldLimit", | ||
"Unknown_MetadataField" => "Mandrill_Unknown_MetadataField" | ||
); | ||
|
||
public function __construct($apikey=null) { | ||
if(!$apikey) $apikey = getenv('MANDRILL_APIKEY'); | ||
if(!$apikey) $apikey = $this->readConfigs(); | ||
if(!$apikey) throw new Mandrill_Error('You must provide a Mandrill API key'); | ||
$this->apikey = $apikey; | ||
|
||
$this->ch = curl_init(); | ||
curl_setopt($this->ch, CURLOPT_USERAGENT, 'Mandrill-PHP/1.0.54'); | ||
curl_setopt($this->ch, CURLOPT_POST, true); | ||
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true); | ||
curl_setopt($this->ch, CURLOPT_HEADER, false); | ||
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true); | ||
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 30); | ||
curl_setopt($this->ch, CURLOPT_TIMEOUT, 600); | ||
|
||
$this->root = rtrim($this->root, '/') . '/'; | ||
|
||
$this->templates = new Mandrill_Templates($this); | ||
$this->exports = new Mandrill_Exports($this); | ||
$this->users = new Mandrill_Users($this); | ||
$this->rejects = new Mandrill_Rejects($this); | ||
$this->inbound = new Mandrill_Inbound($this); | ||
$this->tags = new Mandrill_Tags($this); | ||
$this->messages = new Mandrill_Messages($this); | ||
$this->whitelists = new Mandrill_Whitelists($this); | ||
$this->ips = new Mandrill_Ips($this); | ||
$this->internal = new Mandrill_Internal($this); | ||
$this->subaccounts = new Mandrill_Subaccounts($this); | ||
$this->urls = new Mandrill_Urls($this); | ||
$this->webhooks = new Mandrill_Webhooks($this); | ||
$this->senders = new Mandrill_Senders($this); | ||
$this->metadata = new Mandrill_Metadata($this); | ||
} | ||
|
||
public function __destruct() { | ||
curl_close($this->ch); | ||
} | ||
|
||
public function call($url, $params) { | ||
$params['key'] = $this->apikey; | ||
$params = json_encode($params); | ||
$ch = $this->ch; | ||
|
||
curl_setopt($ch, CURLOPT_URL, $this->root . $url . '.json'); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | ||
curl_setopt($ch, CURLOPT_VERBOSE, $this->debug); | ||
|
||
$start = microtime(true); | ||
$this->log('Call to ' . $this->root . $url . '.json: ' . $params); | ||
if($this->debug) { | ||
$curl_buffer = fopen('php://memory', 'w+'); | ||
curl_setopt($ch, CURLOPT_STDERR, $curl_buffer); | ||
} | ||
|
||
$response_body = curl_exec($ch); | ||
$info = curl_getinfo($ch); | ||
$time = microtime(true) - $start; | ||
if($this->debug) { | ||
rewind($curl_buffer); | ||
$this->log(stream_get_contents($curl_buffer)); | ||
fclose($curl_buffer); | ||
} | ||
$this->log('Completed in ' . number_format($time * 1000, 2) . 'ms'); | ||
$this->log('Got response: ' . $response_body); | ||
|
||
if(curl_error($ch)) { | ||
throw new Mandrill_HttpError("API call to $url failed: " . curl_error($ch)); | ||
} | ||
$result = json_decode($response_body, true); | ||
if($result === null) throw new Mandrill_Error('We were unable to decode the JSON response from the Mandrill API: ' . $response_body); | ||
|
||
if(floor($info['http_code'] / 100) >= 4) { | ||
throw $this->castError($result); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
public function readConfigs() { | ||
$paths = array('~/.mandrill.key', '/etc/mandrill.key'); | ||
foreach($paths as $path) { | ||
if(file_exists($path)) { | ||
$apikey = trim(file_get_contents($path)); | ||
if($apikey) return $apikey; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public function castError($result) { | ||
if($result['status'] !== 'error' || !$result['name']) throw new Mandrill_Error('We received an unexpected error: ' . json_encode($result)); | ||
|
||
$class = (isset(self::$error_map[$result['name']])) ? self::$error_map[$result['name']] : 'Mandrill_Error'; | ||
return new $class($result['message'], $result['code']); | ||
} | ||
|
||
public function log($msg) { | ||
if($this->debug) error_log($msg); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
class Mandrill_Error extends Exception {} | ||
class Mandrill_HttpError extends Mandrill_Error {} | ||
|
||
/** | ||
* The parameters passed to the API call are invalid or not provided when required | ||
*/ | ||
class Mandrill_ValidationError extends Mandrill_Error {} | ||
|
||
/** | ||
* The provided API key is not a valid Mandrill API key | ||
*/ | ||
class Mandrill_Invalid_Key extends Mandrill_Error {} | ||
|
||
/** | ||
* The requested feature requires payment. | ||
*/ | ||
class Mandrill_PaymentRequired extends Mandrill_Error {} | ||
|
||
/** | ||
* The provided subaccount id does not exist. | ||
*/ | ||
class Mandrill_Unknown_Subaccount extends Mandrill_Error {} | ||
|
||
/** | ||
* The requested template does not exist | ||
*/ | ||
class Mandrill_Unknown_Template extends Mandrill_Error {} | ||
|
||
/** | ||
* The subsystem providing this API call is down for maintenance | ||
*/ | ||
class Mandrill_ServiceUnavailable extends Mandrill_Error {} | ||
|
||
/** | ||
* The provided message id does not exist. | ||
*/ | ||
class Mandrill_Unknown_Message extends Mandrill_Error {} | ||
|
||
/** | ||
* The requested tag does not exist or contains invalid characters | ||
*/ | ||
class Mandrill_Invalid_Tag_Name extends Mandrill_Error {} | ||
|
||
/** | ||
* The requested email is not in the rejection list | ||
*/ | ||
class Mandrill_Invalid_Reject extends Mandrill_Error {} | ||
|
||
/** | ||
* The requested sender does not exist | ||
*/ | ||
class Mandrill_Unknown_Sender extends Mandrill_Error {} | ||
|
||
/** | ||
* The requested URL has not been seen in a tracked link | ||
*/ | ||
class Mandrill_Unknown_Url extends Mandrill_Error {} | ||
|
||
/** | ||
* The provided tracking domain does not exist. | ||
*/ | ||
class Mandrill_Unknown_TrackingDomain extends Mandrill_Error {} | ||
|
||
/** | ||
* The given template name already exists or contains invalid characters | ||
*/ | ||
class Mandrill_Invalid_Template extends Mandrill_Error {} | ||
|
||
/** | ||
* The requested webhook does not exist | ||
*/ | ||
class Mandrill_Unknown_Webhook extends Mandrill_Error {} | ||
|
||
/** | ||
* The requested inbound domain does not exist | ||
*/ | ||
class Mandrill_Unknown_InboundDomain extends Mandrill_Error {} | ||
|
||
/** | ||
* The provided inbound route does not exist. | ||
*/ | ||
class Mandrill_Unknown_InboundRoute extends Mandrill_Error {} | ||
|
||
/** | ||
* The requested export job does not exist | ||
*/ | ||
class Mandrill_Unknown_Export extends Mandrill_Error {} | ||
|
||
/** | ||
* A dedicated IP cannot be provisioned while another request is pending. | ||
*/ | ||
class Mandrill_IP_ProvisionLimit extends Mandrill_Error {} | ||
|
||
/** | ||
* The provided dedicated IP pool does not exist. | ||
*/ | ||
class Mandrill_Unknown_Pool extends Mandrill_Error {} | ||
|
||
/** | ||
* The user hasn't started sending yet. | ||
*/ | ||
class Mandrill_NoSendingHistory extends Mandrill_Error {} | ||
|
||
/** | ||
* The user's reputation is too low to continue. | ||
*/ | ||
class Mandrill_PoorReputation extends Mandrill_Error {} | ||
|
||
/** | ||
* The provided dedicated IP does not exist. | ||
*/ | ||
class Mandrill_Unknown_IP extends Mandrill_Error {} | ||
|
||
/** | ||
* You cannot remove the last IP from your default IP pool. | ||
*/ | ||
class Mandrill_Invalid_EmptyDefaultPool extends Mandrill_Error {} | ||
|
||
/** | ||
* The default pool cannot be deleted. | ||
*/ | ||
class Mandrill_Invalid_DeleteDefaultPool extends Mandrill_Error {} | ||
|
||
/** | ||
* Non-empty pools cannot be deleted. | ||
*/ | ||
class Mandrill_Invalid_DeleteNonEmptyPool extends Mandrill_Error {} | ||
|
||
/** | ||
* The domain name is not configured for use as the dedicated IP's custom reverse DNS. | ||
*/ | ||
class Mandrill_Invalid_CustomDNS extends Mandrill_Error {} | ||
|
||
/** | ||
* A custom DNS change for this dedicated IP is currently pending. | ||
*/ | ||
class Mandrill_Invalid_CustomDNSPending extends Mandrill_Error {} | ||
|
||
/** | ||
* Custom metadata field limit reached. | ||
*/ | ||
class Mandrill_Metadata_FieldLimit extends Mandrill_Error {} | ||
|
||
/** | ||
* The provided metadata field name does not exist. | ||
*/ | ||
class Mandrill_Unknown_MetadataField extends Mandrill_Error {} | ||
|
||
|
Oops, something went wrong.