End of day commit

This commit is contained in:
Travis Swientek 2013-07-18 18:09:13 -07:00
parent e5163bb90b
commit 7c5af95c8b
4 changed files with 361 additions and 7 deletions

View File

@ -1,7 +1,7 @@
<?
namespace Mailgun\Common;
require_once 'globals.php';
require_once 'Globals.php';
use Guzzle\Http\Client as Guzzler;
use Mailgun\Exceptions\NoDomainsConfigured;
@ -24,7 +24,9 @@ class Client{
$this->domain = $domain;
$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(){
@ -47,6 +49,10 @@ class Client{
return false;
}
}
elseif($response->getStatusCode() == 401){
//Need to override Guzzle's Error Handling
throw new HTTPError("Your credentials are incorrect.");
}
else{
throw new HTTPError("An HTTP Error has occurred! Try again.");
return false;
@ -82,7 +88,7 @@ class Client{
return $response->getBody();
}
public function deleteRequest($options, $data){
public function deleteRequest($options){
$url = $options['url'];
$request = $this->client->delete($url);

263
Mailgun/Common/Messages.php Normal file
View File

@ -0,0 +1,263 @@
<?PHP
//Message.php - Getters and setters for sending a message.
namespace Mailgun\Common;
require_once 'Globals.php';
use Guzzle\Http\Client as Guzzler;
use Mailgun\Exceptions\NoDomainsConfigured;
use Mailgun\Exceptions\HTTPError;
class Message{
private $message = array();
private $toRecipient = array();
private $ccRecipient = array();
private $bccRecipient = array();
private $fromAddress;
private $subject;
private $customHeader = array();
private $textBody;
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){
}
/*
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){
if($name != NULL){
$addr = $name . " <" . $address . ">";
array_push($this->toRecipient, $addr);
return true;
}
else{
$addr = $address . " <" . $address . ">";
array_push($this->toRecipient, $addr);
return true;
}
}
public function addCcRecipient($address, $name = NULL){
if($name != NULL){
$addr = $name . " <" . $address . ">";
array_push($this->ccRecipient, $addr);
return true;
}
else{
$addr = $address . " <" . $address . ">";
array_push($this->ccRecipient, $addr);
return true;
}
}
public function addBccRecipient($address, $name = NULL){
if($name != NULL){
$addr = $name . " <" . $address . ">";
array_push($this->bccRecipient, $addr);
return true;
}
else{
$addr = $address . " <" . $address . ">";
array_push($this->bccRecipient, $addr);
return true;
}
}
public function setFromAddress($address, $name = NULL){
if($name != NULL){
$this->fromAddress = $name . " <" . $address . ">";
return true;
}
else{
$this->fromAddress = $address . " <" . $address . ">";
return true;
}
}
public function setSubject($data = NULL){
if($data != NULL){
$this->subject = $data;
return true;
}
else{
$this->subject = "";
return false;
}
}
public function addCustomHeader($data){
//Need to check if "X-Mailgun" exists via Regular Expression. Then either add it or not.
//if(preg_match("\^X-Mailgun", $data)){
if(true){
array_push($this->customHeader, $data);
return true;
}
else{
$header = "X-Mailgun-" . $data;
array_push($this->customHeader, $header);
return true;
}
return;
}
//Content
public function setTextBody($data){
//Not sure what validation we should do here. Just assigning the data for now.
$this->textBody = $data;
return true;
}
public function setHTMLBody($data){
//Not sure what validation we should do here. Just assigning the data for now.
$this->htmlBody = $data;
return true;
}
public function addAttachment($data){
$postFields["attachment[$j]"] ="@/path-to-doc/".$mailObj["filenames"][$j];
}
public function addInlineImage(){}
//Options
public function setTestMode($data){
if(is_bool($data)){
if($data == true){
array_push($this->options, array("o:testmode" => true));
}
}
return;
}
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;
}
}
public function setDKIM(){
if(is_bool($data)){
if($data == true){
array_push($this->options, array("o:dkim" => true));
}
else{
array_push($this->options, array("o:dkim" => false));
}
}
return;
}
public function setOpenTracking($data){
if(is_bool($data)){
if($data == true){
array_push($this->options, array("o:tracking-opens" => true));
}
else{
array_push($this->options, array("o:tracking-opens" => false));
}
}
return;
}
public function setClickTracking($data){
if(is_bool($data)){
if($data == true){
array_push($this->options, array("o:tracking-clicks" => true));
}
else{
array_push($this->options, array("o:tracking-clicks" => false));
}
}
return;
}
//Handlers for any new features not defined as a concrete function.
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 getToRecipients(){
return $this->toRecipient;
}
public function getCcRecipients(){
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;
}
}
?>

70
MessagesTest.php Normal file
View File

@ -0,0 +1,70 @@
<?PHP
require('Mailgun/Common/Messages.php');
class MessagesClassTests extends PHPUnit_Framework_TestCase{
public function setUp(){ }
public function tearDown(){ }
public function testPropertySetGetMethods(){
$message = new Mailgun\Common\Message();
//Adds a recipient
$this->assertTrue($message->addToRecipient("travis@trstx.com", "travis swientek") == true);
//Checks if recipient was added to array.
$this->assertContains('travis swientek <travis@trstx.com>', $message->getToRecipients());
//Add a CC recipient
$this->assertTrue($message->addCcRecipient("travis@trstx.com", "travis swientek") == true);
//Checks if recipient was added to array.
$this->assertContains('travis swientek <travis@trstx.com>', $message->getCcRecipients());
//Add a BCC recipient
$this->assertTrue($message->addBccRecipient("travis@trstx.com", "travis swientek") == true);
//Checks if recipient was added to array.
$this->assertContains('travis swientek <travis@trstx.com>', $message->getBccRecipients());
//Add a From address
$this->assertTrue($message->setFromAddress("travis@trstx.com", "travis swientek") == true);
//Checks if recipient was added to array.
$this->assertContains('travis swientek <travis@trstx.com>', $message->getFromAddress());
//Set a subject for the email
$this->assertTrue($message->setSubject("This is my subject!") == true);
//Checks if subject is added
$this->assertEquals('This is my subject!', $message->getSubject());
//Fail to set a subject by calling an empty method
$this->assertTrue($message->setSubject() == false);
//If failing to set a subject, set the subject to "".
$this->assertEquals('', $message->getSubject());
//Set a Text body for the email
$this->assertTrue($message->setTextBody("This is my email text.") == true);
//Checks if Text body is added
$this->assertEquals("This is my email text.", $message->getTextBody());
//Set an HTML body for the email
$this->assertTrue($message->setHTMLBody("<html><head></head><body>This is my HTML email.</body></html>") == true);
//Checks if an HTML body is added
$this->assertEquals("<html><head></head><body>This is my HTML email.</body></html>", $message->getHTMLBody());
//Set a Campaign ID
$this->assertTrue($message->setCampaignId("My-Super-Awesome-Campaign") == true);
//Checks if Campaign ID exists
$this->assertContains("My-Super-Awesome-Campaign", $message->getCampaignId());
}
}
?>

View File

@ -1,22 +1,37 @@
<?
<?php
//require 'vendor/autoload.php';
/*
require_once('Mailgun/autoload.php');
use Mailgun\Common;
use Mailgun\Exceptions\NoDomainsConfigured;
use Mailgun\Exceptions\HTTPError;
$client = new Common\Client("key-6e4jujnt879vqn2gx702wov0kg2hl1a6", "trstx.com");
try{
echo $client->validateCredentials();
$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');
$message = new Mailgun\Common\Message();
$message->setCampaignId("My-Super-Awesome-Campaign");
var_dump($message->getCampaignId());
//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));