diff --git a/Mailgun/Common/BatchMessage.php b/Mailgun/Common/BatchMessage.php new file mode 100644 index 0000000..8fe308d --- /dev/null +++ b/Mailgun/Common/BatchMessage.php @@ -0,0 +1,70 @@ +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); + } + } + } + } + } +} +?> \ No newline at end of file diff --git a/Mailgun/Common/Client.php b/Mailgun/Common/Client.php index 53994ea..6555fbc 100644 --- a/Mailgun/Common/Client.php +++ b/Mailgun/Common/Client.php @@ -1,4 +1,5 @@ domain = $domain; $this->debug = $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->setUserAgent($this->sdkUserAgent . '/' . $this->sdkVersion); + $this->validateCredentials(); } else{ $this->client = new Guzzler('https://' . $this->apiEndpoint . '/' . $this->apiVersion . '/'); @@ -70,18 +73,45 @@ class Client{ public function sendMessage($message){ // This is the grand daddy function to send the message and flush all data from variables. - $domain = $this->domain; - if($this->debug){ - $request = $this->client->post("303980cc", array(), $message); - $response = $request->send(); - } - else{ - $request = $this->client->post("$domain/messages", array(), $message); - $response = $request->send(); - } + 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; + if($this->debug){ + $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(); + } + else{ + $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(); + } return $response; - } + //Throw an exception here! Missing required parameters. + } } ?> \ No newline at end of file diff --git a/Mailgun/Common/Messages.php b/Mailgun/Common/Message.php similarity index 57% rename from Mailgun/Common/Messages.php rename to Mailgun/Common/Message.php index 968cf3d..a5f0312 100644 --- a/Mailgun/Common/Messages.php +++ b/Mailgun/Common/Message.php @@ -1,7 +1,8 @@ message = array(); + if(isset($message)){ + $this->message = $message; + } $this->toRecipientCount = 0; $this->ccRecipientCount = 0; $this->bccRecipientCount = 0; @@ -32,42 +35,68 @@ class Message{ $this->customOptionCount = 0; } - public function addToRecipient($address, $name = NULL){ - if($name != NULL){ + public function addToRecipient($address, $attributes){ + 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 . ">"; + if(isset($this->message["to"])){ + array_push($this->message["to"], $addr); + } + else{ + $this->message["to"] = array($addr); + } + $this->toRecipientCount++; + return true; } - else{ - $addr = $address . " <" . $address . ">"; - } - $arr = "to[".$this->toRecipientCount."]"; - $this->message[$arr] = $addr; - $this->toRecipientCount++; - return true; } + 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 . ">"; - } - else{ - $addr = $address . " <" . $address . ">"; - } - $arr = "cc[".$this->ccRecipientCount."]"; - $this->message[$arr] = $addr; - $this->ccRecipientCount++; - return true; - + + if(isset($this->message["cc"])){ + array_push($this->message["cc"], $addr); + } + else{ + $this->message["cc"] = array($addr); + } + $this->ccRecipientCount++; + return true; + } } 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 . ">"; + + if(isset($this->message["bcc"])){ + array_push($this->message["bcc"], $addr); + } + else{ + $this->message["bcc"] = array($addr); + } + $this->bccRecipientCount++; + return true; } - else{ - $addr = $address . " <" . $address . ">"; - } - $arr = "bcc[".$this->bccRecipientCount."]"; - $this->message[$arr] = $addr; - $this->bccRecipientCount++; - return true; } public function setFromAddress($address, $name = NULL){ if($name != NULL){ @@ -111,9 +140,12 @@ class Message{ return true; } public function addAttachment($data){ - $arr = "attachment[".$this->attachmentCount."]"; - $this->message[$arr] = $data; - $this->attachmentCount++; + if(isset($this->message["attachment"])){ + array_push($this->message["attachment"], $data); + } + else{ + $this->message["attachment"] = array($data); + } return true; } public function addInlineImage($data){ @@ -176,14 +208,31 @@ class Message{ $this->message['o:tracking-clicks'] = $data; return true; } - - public function setDeliveryTime($data){ - //BLAH + public function setDeliveryTime($timeDate, $timeZone = NULL){ + if($timeZone == NULL){ + $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. - 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){ if(isset($this->message['options'][$optionName])){ array_push($this->message['options'][$optionName], $data); @@ -194,41 +243,8 @@ class Message{ return true; } } - public function getMessage(){ - return $this->message; - } + } } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ?> \ No newline at end of file diff --git a/batman-logo-big.gif b/batman-logo-big.gif new file mode 100644 index 0000000..4152bf7 Binary files /dev/null and b/batman-logo-big.gif differ diff --git a/test.php b/test.php index 46eab58..3fe9a5a 100644 --- a/test.php +++ b/test.php @@ -2,59 +2,53 @@ //require 'vendor/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\Exceptions\NoDomainsConfigured; 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{ - $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 +$message = new Mailgun\Common\BatchMessage($client, true); - 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->setSubject("This is the subject of the message!"); -$message->setTextBody("This is the text body of the message!"); -$message->setHtmlBody("This is the html body of the message!"); +$message->setSubject("%recipient.first%, This is the subject of the message!"); +$message->setTextBody("%recipient.first%, This is the text 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("@batman-logo-big.gif"); $message->setTestMode("yes"); $message->setDkim("yes"); +//$message->setDeliveryTime("January 15, 2014 8:00AM", "CST"); $message->setOpenTracking("yes"); $message->setClickTracking("yes"); $message->addCustomOption("o:myoption", "true"); $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!")); - - ?> \ No newline at end of file