diff --git a/Mailgun/Common/Client.php b/Mailgun/Common/Client.php index b830158..88a1d90 100644 --- a/Mailgun/Common/Client.php +++ b/Mailgun/Common/Client.php @@ -1,7 +1,7 @@ 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); diff --git a/Mailgun/Common/Messages.php b/Mailgun/Common/Messages.php new file mode 100644 index 0000000..79c6edf --- /dev/null +++ b/Mailgun/Common/Messages.php @@ -0,0 +1,263 @@ +"; + 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; + } +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +?> \ No newline at end of file diff --git a/MessagesTest.php b/MessagesTest.php new file mode 100644 index 0000000..f134f71 --- /dev/null +++ b/MessagesTest.php @@ -0,0 +1,70 @@ +assertTrue($message->addToRecipient("travis@trstx.com", "travis swientek") == true); + + //Checks if recipient was added to array. + $this->assertContains('travis swientek ', $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 ', $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 ', $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 ', $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("This is my HTML email.") == true); + + //Checks if an HTML body is added + $this->assertEquals("This is my HTML email.", $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()); + + + + } +} + +?> \ No newline at end of file diff --git a/test.php b/test.php index 016af06..90974b9 100644 --- a/test.php +++ b/test.php @@ -1,22 +1,37 @@ -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));