diff options
-rw-r--r-- | lib/SparkPost/Resource.php | 26 | ||||
-rw-r--r-- | lib/SparkPost/Transmission.php | 105 |
2 files changed, 131 insertions, 0 deletions
diff --git a/lib/SparkPost/Resource.php b/lib/SparkPost/Resource.php new file mode 100644 index 0000000..57dfd24 --- /dev/null +++ b/lib/SparkPost/Resource.php @@ -0,0 +1,26 @@ +<?php + +namespace SparkPost; + +class Resource +{ + protected $sparkpost; + protected $endpoint; + + public function __construct(SparkPost $sparkpost, $endpoint) + { + $this->sparkpost = $sparkpost; + $this->endpoint = $endpoint; + } + + public function get($uri, $payload, $header) + { + return $this->sparkpost->request('GET', $this->endpoint.'/'.$uri, $payload, $header); + } + + public function post($payload, $header) + { + echo $payload; + return $this->sparkpost->request('POST', $this->endpoint, $payload, $header); + } +} diff --git a/lib/SparkPost/Transmission.php b/lib/SparkPost/Transmission.php new file mode 100644 index 0000000..ddcde2f --- /dev/null +++ b/lib/SparkPost/Transmission.php @@ -0,0 +1,105 @@ +<?php + +namespace SparkPost; + +class Transmission extends Resource +{ + protected $endpoint = 'transmissions'; + protected $customHeaders = array(); + + public function __construct(SparkPost $sparkpost) + { + parent::__construct($sparkpost, $endpoint); + } + + public function fixBlindCarbonCopy($payload) + { + //TODO: Manage recipients. "Vincent Song <vincentsong@sparkpost.com>" + + $modifiedPayload = $payload; + $bccList = &$modifiedPayload['bcc']; + $recipientsList = &$modifiedPayload['recipients']; + + //Format: Original Recipient" <original.recipient@example.com> + //if a name exists, then do "name" <email>. Otherwise, just do <email> + if(isset($modifiedPayload['recipients'][0]['name'])) + { + $originalRecipient = '"' . $modifiedPayload['recipients'][0]['name'] + . '" <' . $modifiedPayload['recipients'][0]['address'] . '>'; + } else { + $originalRecipient = '<' . $modifiedPayload['recipients'][0]['address'] + . '>'; + } + + //loop through all BCC recipients + if(isset($bccList)){ + foreach ($bccList as $bccRecipient) { + $newRecipient = [ + 'address' => $bccRecipient['address'], + 'header_to' => $originalRecipient, + ]; + array_push($recipientsList, $newRecipient); + } + } + + //Delete the BCC object/array + unset($modifiedPayload['bcc']); + + return $modifiedPayload; + } + + public function fixCarbonCopy($payload) + { + $ccCustomHeadersList = ""; + $modifiedPayload = $payload; + $ccList = &$modifiedPayload['cc']; + $recipientsList = &$modifiedPayload['recipients']; + + //if a name exists, then do "name" <email>. Otherwise, just do <email> + if(isset($modifiedPayload['recipients'][0]['name'])) { + $originalRecipient = '"' . $modifiedPayload['recipients'][0]['name'] + . '" <' . $modifiedPayload['recipients'][0]['address'] . '>'; + } else { + $originalRecipient = '<' . $modifiedPayload['recipients'][0]['address'] + . '<'; + } + + if(isset($ccList)){ + foreach ($ccList as $ccRecipient) { + $newRecipient = [ + 'address' => $ccRecipient['address'], + 'header_to' => $originalRecipient, + ]; + + //if name exists, then use "Name" <Email> format. Otherwise, just email will suffice. + if(isset($ccRecipient['name'])) { + $ccCustomHeadersList = $ccCustomHeadersList . ' "' . $ccRecipient['name'] + . '" <' . $ccRecipient['address'] . '>,'; + } else { + $ccCustomHeadersList = $ccCustomHeadersList . ' ' . $ccRecipient['address']; + } + + } + + if(!empty($ccCustomHeadersList)){ //If there are CC'd people + $this->customHeaders = array("CC" => $ccCustomHeadersList); + } + //Edits customHeaders and adds array of CSV list of CC emails + + } + + //delete CC + unset($modifiedPayload['cc']); + + return $modifiedPayload; + } + + public function post($payload) + { + $modifiedPayload = $this->fixBlindCarbonCopy($payload); //Accounts for any BCCs + $modifiedPayload = $this->fixCarbonCopy($modifiedPayload); //Accounts for any CCs + return parent::post($modifiedPayload, $this->customHeaders); + } +} + +?>
\ No newline at end of file |