Refactored the client and message relationship. Created new BatchSending class that extends Message Class.

This commit is contained in:
Travis Swientek 2013-07-22 17:20:32 -07:00
parent 313f154c4c
commit f0af3328f0
5 changed files with 238 additions and 128 deletions

View File

@ -0,0 +1,70 @@
<?PHP
//BatchMessage.php - Extends the Message class and provides continuous recipient addition.
namespace Mailgun\Common;
require_once 'Globals.php';
use Guzzle\Http\Client as Guzzler;
use Mailgun\Exceptions\NoDomainsConfigured;
use Mailgun\Exceptions\HTTPError;
class BatchMessage extends Message{
private $batchRecipientAttributes;
private $client;
public function __construct($client){
parent::__construct($this->client);
$this->batchRecipientAttributes = array();
$this->client = $client;
}
public function addBatchRecipient($address, $attributes){
if($this->toRecipientCount == 1000){
$this->sendBatchMessage();
$this->batchRecipientAttributes = array();
$this->toRecipientCount = 0;
unset($this->message['to']);
}
if(array_key_exists("first", $attributes)){
if(array_key_exists("last", $attributes)){
$name = $attributes["first"] . " " . $attributes["last"];
}
$name = $attributes["first"];
}
$addr = $name . " <" . $address . ">";
if(isset($this->message["to"])){
array_push($this->message["to"], $addr);
}
else{
$this->message["to"] = array($addr);
}
$attributes["id"] = $this->toRecipientCount;
$this->batchRecipientAttributes["$address"] = $attributes;
$this->toRecipientCount++;
return true;
}
public function endBatchMessage(){
$this->sendBatchMessage();
$this->batchRecipientAttributes = array();
$this->toRecipientCount = 0;
unset($this->message['to']);
}
private function sendBatchMessage(){
if(array_key_exists("from", $this->message)){
if($this->toRecipientCount > 0){
if(array_key_exists("subject", $this->message)){
if(array_key_exists("text", $this->message) || array_key_exists("html", $this->message)){
$this->message["recipient-variables"] = json_encode($this->batchRecipientAttributes);
return $this->client->sendMessage($this->message);
}
}
}
}
}
}
?>

View File

@ -1,4 +1,5 @@
<?PHP <?PHP
namespace Mailgun\Common; namespace Mailgun\Common;
require_once 'Globals.php'; require_once 'Globals.php';
@ -25,9 +26,11 @@ class Client{
$this->domain = $domain; $this->domain = $domain;
$this->debug = $debug; $this->debug = $debug;
if($this->debug){ if($this->debug){
$this->client = new Guzzler('http://postbin.ryanbigg.com/'); $this->client = new Guzzler('https://api.ninomail.com/' . $this->apiVersion . '/', array('ssl.certificate_authority' => false));
$this->client->setDefaultOption('auth', array ($this->apiUser, $this->apiKey));
$this->client->setDefaultOption('exceptions', false); $this->client->setDefaultOption('exceptions', false);
$this->client->setUserAgent($this->sdkUserAgent . '/' . $this->sdkVersion); $this->client->setUserAgent($this->sdkUserAgent . '/' . $this->sdkVersion);
$this->validateCredentials();
} }
else{ else{
$this->client = new Guzzler('https://' . $this->apiEndpoint . '/' . $this->apiVersion . '/'); $this->client = new Guzzler('https://' . $this->apiEndpoint . '/' . $this->apiVersion . '/');
@ -70,17 +73,44 @@ class Client{
public function sendMessage($message){ public function sendMessage($message){
// This is the grand daddy function to send the message and flush all data from variables. // This is the grand daddy function to send the message and flush all data from variables.
if(array_key_exists("from", $message) &&
array_key_exists("to", $message) &&
array_key_exists("subject", $message) &&
(array_key_exists("text", $message) || array_key_exists("html", $message))){
$domain = $this->domain; $domain = $this->domain;
if($this->debug){ if($this->debug){
$request = $this->client->post("303980cc", array(), $message); $request = $this->client->post("$domain/messages", array(), $message);
if(isset($message["attachment"])){
foreach($message["attachment"] as $attachments){
$request->addPostFile("attachment", $attachments);
}
unset($message["attachment"]);
}
if(isset($message["inline"])){
foreach($message["inline"] as $inlineAttachments){
$request->addPostFile("inline", $inlineAttachments);
}
}
$response = $request->send(); $response = $request->send();
} }
else{ else{
$request = $this->client->post("$domain/messages", array(), $message); $request = $this->client->post("$domain/messages", array(), $message);
if(isset($message["attachment"])){
foreach($message["attachment"] as $attachments){
$request->addPostFile("attachment", $attachments);
}
unset($message["attachment"]);
}
if(isset($message["inline"])){
foreach($message["inline"] as $inlineAttachments){
$request->addPostFile("inline", $inlineAttachments);
}
}
$response = $request->send(); $response = $request->send();
} }
return $response; return $response;
}
//Throw an exception here! Missing required parameters.
} }
} }

View File

@ -1,7 +1,8 @@
<?PHP <?PHP
//Message.php - Getters and setters for sending a message. /*
* Message.php - Message builder for creating a message object. Pass the message object to the client to send the message.
*/
namespace Mailgun\Common; namespace Mailgun\Common;
require_once 'Globals.php'; require_once 'Globals.php';
@ -12,18 +13,20 @@ use Mailgun\Exceptions\HTTPError;
class Message{ class Message{
private $message; protected $message;
private $sanitized; protected $sanitized;
private $toRecipientCount; protected $toRecipientCount;
private $ccRecipientCount; protected $ccRecipientCount;
private $bccRecipientCount; protected $bccRecipientCount;
private $attachmentCount; protected $attachmentCount;
private $campaignIdCount; protected $campaignIdCount;
private $customOptionCount; protected $customOptionCount;
public function __construct($message = null){ public function __construct($message = null){
$this->message = array(); $this->message = array();
if(isset($message)){
$this->message = $message;
}
$this->toRecipientCount = 0; $this->toRecipientCount = 0;
$this->ccRecipientCount = 0; $this->ccRecipientCount = 0;
$this->bccRecipientCount = 0; $this->bccRecipientCount = 0;
@ -32,43 +35,69 @@ class Message{
$this->customOptionCount = 0; $this->customOptionCount = 0;
} }
public function addToRecipient($address, $name = NULL){ public function addToRecipient($address, $attributes){
if($name != NULL){ if($this->toRecipientCount < 1000){
if(array_key_exists("first", $attributes)){
if(array_key_exists("last", $attributes)){
$name = $attributes["first"] . " " . $attributes["last"];
}
$name = $attributes["first"];
}
$addr = $name . " <" . $address . ">"; $addr = $name . " <" . $address . ">";
if(isset($this->message["to"])){
array_push($this->message["to"], $addr);
} }
else{ else{
$addr = $address . " <" . $address . ">"; $this->message["to"] = array($addr);
} }
$arr = "to[".$this->toRecipientCount."]";
$this->message[$arr] = $addr;
$this->toRecipientCount++; $this->toRecipientCount++;
return true; return true;
} }
}
public function addCcRecipient($address, $name = NULL){ public function addCcRecipient($address, $name = NULL){
if($name != NULL){ if($this->ccRecipientCount < 1000){
if(array_key_exists("first", $attributes)){
if(array_key_exists("last", $attributes)){
$name = $attributes["first"] . " " . $attributes["last"];
}
$name = $attributes["first"];
}
$addr = $name . " <" . $address . ">"; $addr = $name . " <" . $address . ">";
if(isset($this->message["cc"])){
array_push($this->message["cc"], $addr);
} }
else{ else{
$addr = $address . " <" . $address . ">"; $this->message["cc"] = array($addr);
} }
$arr = "cc[".$this->ccRecipientCount."]";
$this->message[$arr] = $addr;
$this->ccRecipientCount++; $this->ccRecipientCount++;
return true; return true;
}
} }
public function addBccRecipient($address, $name = NULL){ public function addBccRecipient($address, $name = NULL){
if($name != NULL){ if($this->bccRecipientCount < 1000){
if(array_key_exists("first", $attributes)){
if(array_key_exists("last", $attributes)){
$name = $attributes["first"] . " " . $attributes["last"];
}
$name = $attributes["first"];
}
$addr = $name . " <" . $address . ">"; $addr = $name . " <" . $address . ">";
if(isset($this->message["bcc"])){
array_push($this->message["bcc"], $addr);
} }
else{ else{
$addr = $address . " <" . $address . ">"; $this->message["bcc"] = array($addr);
} }
$arr = "bcc[".$this->bccRecipientCount."]";
$this->message[$arr] = $addr;
$this->bccRecipientCount++; $this->bccRecipientCount++;
return true; return true;
} }
}
public function setFromAddress($address, $name = NULL){ public function setFromAddress($address, $name = NULL){
if($name != NULL){ if($name != NULL){
$addr = $name . " <" . $address . ">"; $addr = $name . " <" . $address . ">";
@ -111,9 +140,12 @@ class Message{
return true; return true;
} }
public function addAttachment($data){ public function addAttachment($data){
$arr = "attachment[".$this->attachmentCount."]"; if(isset($this->message["attachment"])){
$this->message[$arr] = $data; array_push($this->message["attachment"], $data);
$this->attachmentCount++; }
else{
$this->message["attachment"] = array($data);
}
return true; return true;
} }
public function addInlineImage($data){ public function addInlineImage($data){
@ -176,14 +208,31 @@ class Message{
$this->message['o:tracking-clicks'] = $data; $this->message['o:tracking-clicks'] = $data;
return true; return true;
} }
public function setDeliveryTime($timeDate, $timeZone = NULL){
public function setDeliveryTime($data){ if($timeZone == NULL){
//BLAH $timeZoneObj = new \DateTimeZone(DEFAULT_TIME_ZONE);
}
else{
$timeZoneObj = new \DateTimeZone("$timeZone");
} }
$dateTimeObj = new \DateTime($timeDate, $timeZoneObj);
$formattedTimeDate = $dateTimeObj->format(\DateTime::RFC2822);
$this->message['o:deliverytime'] = $formattedTimeDate;
return true;
}
//Handlers for any new features not defined as a concrete function. //Handlers for any new features not defined as a concrete function.
public function addCustomData(){} public function addCustomData($customName, $data){
if(is_array($data)){
$jsonArray = json_encode($data);
$this->message['v:'.$customName] = $jsonArray;
}
else{
//throw exception here!
return false;
}
}
public function addCustomOption($optionName, $data){ public function addCustomOption($optionName, $data){
if(isset($this->message['options'][$optionName])){ if(isset($this->message['options'][$optionName])){
array_push($this->message['options'][$optionName], $data); array_push($this->message['options'][$optionName], $data);
@ -194,41 +243,8 @@ class Message{
return true; return true;
} }
} }
public function getMessage(){ public function getMessage(){
return $this->message; return $this->message;
} }
} }
?> ?>

BIN
batman-logo-big.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

View File

@ -2,59 +2,53 @@
//require 'vendor/autoload.php'; //require 'vendor/autoload.php';
require_once('Mailgun/autoload.php'); require_once('Mailgun/autoload.php');
require('Mailgun/Common/Messages.php'); require('Mailgun/Common/Message.php');
require('Mailgun/Common/BatchMessage.php');
use Mailgun\Common; use Mailgun\Common;
use Mailgun\Exceptions\NoDomainsConfigured; use Mailgun\Exceptions\NoDomainsConfigured;
use Mailgun\Exceptions\HTTPError; use Mailgun\Exceptions\HTTPError;
$client = new Common\Client("key-ca6d168e492611df8307001d60d24a9c-0b27e", "aawdawdad.ninomail.com", true);
$message = new Mailgun\Common\Message(array('from' =>"travis@aawdawdad.ninomail.com", 'to' => "travis@tswientek.com", "subject" => "subject here", "text" => "hello"));
$response = $client->sendMessage($message->getMessage());
echo $response->getBody();
//$message = new Mailgun\Common\Message($client);
/* /*
try{ $message = new Mailgun\Common\BatchMessage($client, true);
$client = new Common\Client("key-6e4jujnt879vqn2gx702wov0kg2hl1a6", "trstx.com");
}
catch (HTTPError $e) {
echo "An HTTP error has occurred! Please try again later\r\n";
}
//Post a Message
echo $client->postRequest(array('url' => 'trstx.com/messages'), array('from'=>'test@trstx.com', 'to'=>'travis.swientek@rackspace.com', 'subject' => 'test', 'text' => 'asdf', 'o:testmode'=>true));
echo $client->getRequest(array('url' => 'trstx.com/unsubscribes'), array());
echo $client->postRequest(array('url' => 'trstx.com/unsubscribes'), array('address' => 'travis@whatever.com', 'tag' => '*'));
echo $client->postRequest(array('url' => 'trstx.com/bounces'), array('address' => 'travis@whatever.com'));
echo $client->deleteRequest(array('url' => 'trstx.com/bounces/travis@whatever.com'));
require('Mailgun/Common/Messages.php');
$client = new Common\Client("key-6e4jujnt879vqn2gx702wov0kg2hl1a6", "trstx.com", true);
echo $client->sendMessage($email);
$message = new Mailgun\Common\Message();
$message->addToRecipient("travis@tswientek.com", "travis swientek");
$message->addCcRecipient("travis@trstx.com", "CC Recipient");
$message->addBccRecipient("travis@trstx.com", "BCC Recipient");
$message->setFromAddress("travis@tswientek.com", "From Name"); $message->setFromAddress("travis@tswientek.com", "From Name");
$message->setSubject("This is the subject of the message!"); $message->setSubject("%recipient.first%, This is the subject of the message!");
$message->setTextBody("This is the text body of the message!"); $message->setTextBody("%recipient.first%, This is the text body of the message!");
$message->setHtmlBody("This is the html body of the message!"); $message->setHtmlBody("%recipient.first%, %recipient.my.id% This is the html body of the message!");
$message->addAttachment("@GitHub_Logo.png"); $message->addAttachment("@GitHub_Logo.png");
$message->addAttachment("@batman-logo-big.gif");
$message->setTestMode("yes"); $message->setTestMode("yes");
$message->setDkim("yes"); $message->setDkim("yes");
//$message->setDeliveryTime("January 15, 2014 8:00AM", "CST");
$message->setOpenTracking("yes"); $message->setOpenTracking("yes");
$message->setClickTracking("yes"); $message->setClickTracking("yes");
$message->addCustomOption("o:myoption", "true"); $message->addCustomOption("o:myoption", "true");
$message->addCampaignId("askldf"); $message->addCampaignId("askldf");
$message->addCustomData("mycustomdata", array("name"=> "travis"));
//echo $message->sendMessage();
//$message->addBatchRecipient("travis@tswientek.com", array("first" => "Travis", "last" => "Swientek", "my.id" => "ABC12345"));
for($i = 0; $i<5; $i++){
$message->addBatchRecipient("travis@".$i."test.com", array("first" => "$i - First", "last" => "$i - Last", "my.id" => "ABC12345"));
}
$message->endBatchMessage();
//echo $client->sendMessage($message->getMessage())->getBody();
$email = $message->getMessage();
var_dump($email);
echo $client->sendMessage($email);
*/ */
$client = new Common\Client("key-6e4jujnt879vqn2gx702wov0kg2hl1a6", "trstx.com", false);
echo $client->sendMessage(array("from" => "travis@trstx.com", "to" => "travis@tswientek.com", "subject" => "This is the email subject!", "text" => "Hi from the SDK!"));
?> ?>