Closing the day

This commit is contained in:
Travis Swientek 2013-07-19 17:00:07 -07:00
parent 7c5af95c8b
commit 313f154c4c
4 changed files with 178 additions and 200 deletions

BIN
GitHub_Logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

View File

@ -1,4 +1,4 @@
<? <?PHP
namespace Mailgun\Common; namespace Mailgun\Common;
require_once 'Globals.php'; require_once 'Globals.php';
@ -12,6 +12,7 @@ class Client{
protected $apiKey; protected $apiKey;
protected $domain; protected $domain;
protected $client; protected $client;
protected $debug;
protected $apiEndpoint = API_ENDPOINT; protected $apiEndpoint = API_ENDPOINT;
protected $apiVersion = API_VERSION; protected $apiVersion = API_VERSION;
@ -19,19 +20,27 @@ class Client{
protected $sdkVersion = SDK_VERSION; protected $sdkVersion = SDK_VERSION;
protected $sdkUserAgent = SDK_USER_AGENT; protected $sdkUserAgent = SDK_USER_AGENT;
public function __construct($apiKey, $domain){ public function __construct($apiKey, $domain, $debug = false){
$this->apiKey = $apiKey; $this->apiKey = $apiKey;
$this->domain = $domain; $this->domain = $domain;
$this->client = new Guzzler('https://' . $this->apiEndpoint . '/' . $this->apiVersion . '/'); $this->debug = $debug;
$this->client->setDefaultOption('auth', array ($this->apiUser, $this->apiKey)); if($this->debug){
$this->client->setDefaultOption('exceptions', false); $this->client = new Guzzler('http://postbin.ryanbigg.com/');
$this->client->setUserAgent($this->sdkUserAgent . '/' . $this->sdkVersion); $this->client->setDefaultOption('exceptions', false);
$this->validateCredentials(); $this->client->setUserAgent($this->sdkUserAgent . '/' . $this->sdkVersion);
}
else{
$this->client = new Guzzler('https://' . $this->apiEndpoint . '/' . $this->apiVersion . '/');
$this->client->setDefaultOption('auth', array ($this->apiUser, $this->apiKey));
$this->client->setDefaultOption('exceptions', false);
$this->client->setUserAgent($this->sdkUserAgent . '/' . $this->sdkVersion);
$this->validateCredentials();
}
} }
public function validateCredentials(){ public function validateCredentials(){
$url = "domains"; $url = "domains";
$data = null;
$request = $this->client->get($url, array(), $data); $request = $this->client->get($url, array(), $data);
$response = $request->send(); $response = $request->send();
@ -59,43 +68,19 @@ class Client{
} }
} }
public function getRequest($options, $data){ 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();
}
return $response;
$url = $options['url'];
$request = $this->client->get($url, array(), $data);
$response = $request->send();
return $response->getBody();
}
public function postRequest($options, $data){
$url = $options['url'];
$request = $this->client->post($url, array(), $data);
$response = $request->send();
return $response->getBody();
}
public function putRequest($options, $data){
$url = $options['url'];
$request = $this->client->put($url, array(), $data);
$response = $request->send();
return $response->getBody();
}
public function deleteRequest($options){
$url = $options['url'];
$request = $this->client->delete($url);
$response = $request->send();
return $response->getBody();
} }
} }

View File

@ -12,221 +12,192 @@ use Mailgun\Exceptions\HTTPError;
class Message{ class Message{
private $message = array(); private $message;
private $toRecipient = array(); private $sanitized;
private $ccRecipient = array(); private $toRecipientCount;
private $bccRecipient = array(); private $ccRecipientCount;
private $fromAddress; private $bccRecipientCount;
private $subject; private $attachmentCount;
private $customHeader = array(); private $campaignIdCount;
private $textBody; private $customOptionCount;
private $htmlBody;
private $attachment = array();
private $inlineImage = array();
private $options = array();
private $customData = array();
private $customOption = array();
public function __construct($headers = NULL, $content = NULL, $options = NULL){
public function __construct($message = null){
$this->message = array();
$this->toRecipientCount = 0;
$this->ccRecipientCount = 0;
$this->bccRecipientCount = 0;
$this->attachmentCount = 0;
$this->campaignIdCount = 0;
$this->customOptionCount = 0;
} }
/*
This section includes all headers that can be programmatically
added to the email. Each attribute is broken down in to a single
function to make it easier and more intuitive for new users.
Dealing with complex arrays on the client side is usually no fun.
Plus most people iterate through an array of data from a database, so
why not just iterate and add each recipient to the "Message" object instead?
*/
// This function adds a recipient item to the recipient array. If the name is Null,
// the address will be included in the typical name field so it displays nicely.
public function addToRecipient($address, $name = NULL){ public function addToRecipient($address, $name = NULL){
if($name != NULL){ if($name != NULL){
$addr = $name . " <" . $address . ">"; $addr = $name . " <" . $address . ">";
array_push($this->toRecipient, $addr);
return true;
} }
else{ else{
$addr = $address . " <" . $address . ">"; $addr = $address . " <" . $address . ">";
array_push($this->toRecipient, $addr);
return true;
} }
$arr = "to[".$this->toRecipientCount."]";
$this->message[$arr] = $addr;
$this->toRecipientCount++;
return true;
} }
public function addCcRecipient($address, $name = NULL){ public function addCcRecipient($address, $name = NULL){
if($name != NULL){ if($name != NULL){
$addr = $name . " <" . $address . ">"; $addr = $name . " <" . $address . ">";
array_push($this->ccRecipient, $addr);
return true;
} }
else{ else{
$addr = $address . " <" . $address . ">"; $addr = $address . " <" . $address . ">";
array_push($this->ccRecipient, $addr);
return true;
} }
$arr = "cc[".$this->ccRecipientCount."]";
$this->message[$arr] = $addr;
$this->ccRecipientCount++;
return true;
} }
public function addBccRecipient($address, $name = NULL){ public function addBccRecipient($address, $name = NULL){
if($name != NULL){ if($name != NULL){
$addr = $name . " <" . $address . ">"; $addr = $name . " <" . $address . ">";
array_push($this->bccRecipient, $addr);
return true;
} }
else{ else{
$addr = $address . " <" . $address . ">"; $addr = $address . " <" . $address . ">";
array_push($this->bccRecipient, $addr);
return true;
} }
$arr = "bcc[".$this->bccRecipientCount."]";
$this->message[$arr] = $addr;
$this->bccRecipientCount++;
return true;
} }
public function setFromAddress($address, $name = NULL){ public function setFromAddress($address, $name = NULL){
if($name != NULL){ if($name != NULL){
$this->fromAddress = $name . " <" . $address . ">"; $addr = $name . " <" . $address . ">";
return true;
} }
else{ else{
$this->fromAddress = $address . " <" . $address . ">"; $addr = $address . " <" . $address . ">";
return true;
} }
$this->message['from'] = $addr;
return true;
} }
public function setSubject($data = NULL){ public function setSubject($data = NULL){
if($data != NULL){ if($data == NULL || $data == ""){
$this->subject = $data; $data = " ";
return true;
}
else{
$this->subject = "";
return false;
} }
$this->message['subject'] = $data;
return true;
} }
public function addCustomHeader($data){ public function addCustomHeader($headerName, $data){
//Need to check if "X-Mailgun" exists via Regular Expression. Then either add it or not. if(!preg_match("/^h:/i", $headerName)){
//if(preg_match("\^X-Mailgun", $data)){ $headerName = "h:" . $headerName;
if(true){
array_push($this->customHeader, $data);
return true;
} }
else{
$header = "X-Mailgun-" . $data; $this->addCustomOption($headerName, $data);
array_push($this->customHeader, $header); return true;
return true;
}
return;
} }
//Content //Content
public function setTextBody($data){ public function setTextBody($data){
//Not sure what validation we should do here. Just assigning the data for now. if($data == NULL || $data == ""){
$this->textBody = $data; $data = " ";
}
$this->message['text'] = $data;
return true; return true;
} }
public function setHTMLBody($data){ public function setHtmlBody($data){
//Not sure what validation we should do here. Just assigning the data for now. if($data == NULL || $data == ""){
$this->htmlBody = $data; $data = " ";
}
$this->message['html'] = $data;
return true; return true;
} }
public function addAttachment($data){ public function addAttachment($data){
$postFields["attachment[$j]"] ="@/path-to-doc/".$mailObj["filenames"][$j]; $arr = "attachment[".$this->attachmentCount."]";
$this->message[$arr] = $data;
$this->attachmentCount++;
return true;
} }
public function addInlineImage(){} public function addInlineImage($data){
if(isset($this->message['inline'])){
//Options array_push($this->message['inline'] , $data);
public function setTestMode($data){ return true;
if(is_bool($data)){
if($data == true){
array_push($this->options, array("o:testmode" => true));
}
} }
return; else{
} $this->message['inline'] = array($data);
public function setCampaignId($data){
if(is_array(isset($this->options['o:campaign']))){
$arrCount = count($this->options['o:campaign']);
if($arrCount <= 3){
$this->options['o:campaign'] = $data;
}
else{
return false;
}
}
else {
$this->options['o:campaign'] = $data;
return true; return true;
} }
} }
public function setDKIM(){
if(is_bool($data)){ //Options
if($data == true){ public function setTestMode($data){
array_push($this->options, array("o:dkim" => true)); if(filter_var($data, FILTER_VALIDATE_BOOLEAN)){
} $data = "yes";
else{
array_push($this->options, array("o:dkim" => false));
}
} }
return; else{
$data = "no";
}
$this->message['o:testmode'] = $data;
return true;
}
public function addCampaignId($data){
if($this->campaignIdCount < 3){
$arr = "o:campaign[".$this->campaignIdCount."]";
$this->message[$arr] = $data;
$this->campaignIdCount++;
return true;
}
}
public function setDkim($data){
if(filter_var($data, FILTER_VALIDATE_BOOLEAN)){
$data = "yes";
}
else{
$data = "no";
}
$this->message["o:dkim"] = $data;
return true;
} }
public function setOpenTracking($data){ public function setOpenTracking($data){
if(is_bool($data)){ if(filter_var($data, FILTER_VALIDATE_BOOLEAN)){
if($data == true){ $data = "yes";
array_push($this->options, array("o:tracking-opens" => true));
}
else{
array_push($this->options, array("o:tracking-opens" => false));
}
} }
return; else{
$data = "no";
}
$this->message['o:tracking-opens'] = $data;
return true;
} }
public function setClickTracking($data){ public function setClickTracking($data){
if(is_bool($data)){ if(filter_var($data, FILTER_VALIDATE_BOOLEAN)){
if($data == true){ $data = "yes";
array_push($this->options, array("o:tracking-clicks" => true));
}
else{
array_push($this->options, array("o:tracking-clicks" => false));
}
} }
return; else{
$data = "no";
}
$this->message['o:tracking-clicks'] = $data;
return true;
}
public function setDeliveryTime($data){
//BLAH
} }
//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(){}
public function addCustomOptions(){}
public function sendMessage(){
// This is the grand daddy function to send the message and flush all data from variables.
public function addCustomOption($optionName, $data){
if(isset($this->message['options'][$optionName])){
array_push($this->message['options'][$optionName], $data);
return true;
}
else{
$this->message['options'][$optionName] = array($data);
return true;
}
} }
public function getToRecipients(){ public function getMessage(){
return $this->toRecipient;
}
public function getCcRecipients(){ return $this->message;
return $this->ccRecipient;
}
public function getBccRecipients(){
return $this->bccRecipient;
}
public function getFromAddress(){
return $this->bccRecipient;
}
public function getSubject(){
return $this->subject;
}
public function getTextBody(){
return $this->textBody;
}
public function getHTMLBody(){
return $this->htmlBody;
}
public function getCampaignId(){
return $this->options;
} }
} }

View File

@ -1,13 +1,16 @@
<?php <?php
//require 'vendor/autoload.php'; //require 'vendor/autoload.php';
/*
require_once('Mailgun/autoload.php'); require_once('Mailgun/autoload.php');
require('Mailgun/Common/Messages.php');
use Mailgun\Common; use Mailgun\Common;
use Mailgun\Exceptions\NoDomainsConfigured; use Mailgun\Exceptions\NoDomainsConfigured;
use Mailgun\Exceptions\HTTPError; use Mailgun\Exceptions\HTTPError;
/*
try{ try{
$client = new Common\Client("key-6e4jujnt879vqn2gx702wov0kg2hl1a6", "trstx.com"); $client = new Common\Client("key-6e4jujnt879vqn2gx702wov0kg2hl1a6", "trstx.com");
} }
@ -22,17 +25,36 @@ catch (HTTPError $e) {
echo $client->postRequest(array('url' => 'trstx.com/unsubscribes'), array('address' => 'travis@whatever.com', 'tag' => '*')); 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->postRequest(array('url' => 'trstx.com/bounces'), array('address' => 'travis@whatever.com'));
echo $client->deleteRequest(array('url' => 'trstx.com/bounces/travis@whatever.com')); echo $client->deleteRequest(array('url' => 'trstx.com/bounces/travis@whatever.com'));
*/
require('Mailgun/Common/Messages.php'); require('Mailgun/Common/Messages.php');
$client = new Common\Client("key-6e4jujnt879vqn2gx702wov0kg2hl1a6", "trstx.com", true);
echo $client->sendMessage($email);
$message = new Mailgun\Common\Message(); $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->addAttachment("@GitHub_Logo.png");
$message->setTestMode("yes");
$message->setDkim("yes");
$message->setOpenTracking("yes");
$message->setClickTracking("yes");
$message->addCustomOption("o:myoption", "true");
$message->addCampaignId("askldf");
$message->setCampaignId("My-Super-Awesome-Campaign"); $email = $message->getMessage();
var_dump($message->getCampaignId()); 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!"));
?> ?>