diff options
author | Vincent Song <vincent.wsong@gmail.com> | 2016-06-14 10:36:53 -0400 |
---|---|---|
committer | Vincent Song <vincent.wsong@gmail.com> | 2016-06-14 10:36:53 -0400 |
commit | 7f8548cf61b478df2b9172afe2a8f77448058a58 (patch) | |
tree | b404898681d297872354c5468baf03b731922ef2 | |
parent | 39abd627feb8ccd0803880c272e04073ed9fd41f (diff) | |
download | php-sparkpost-7f8548cf61b478df2b9172afe2a8f77448058a58.zip php-sparkpost-7f8548cf61b478df2b9172afe2a8f77448058a58.tar.gz php-sparkpost-7f8548cf61b478df2b9172afe2a8f77448058a58.tar.bz2 |
Added parser to change shorthand recipients to array of name and address, wherever possible
-rw-r--r-- | lib/SparkPost/Resource.php | 2 | ||||
-rw-r--r-- | lib/SparkPost/Transmission.php | 68 |
2 files changed, 68 insertions, 2 deletions
diff --git a/lib/SparkPost/Resource.php b/lib/SparkPost/Resource.php index 91038d5..06318b7 100644 --- a/lib/SparkPost/Resource.php +++ b/lib/SparkPost/Resource.php @@ -23,4 +23,4 @@ class Resource echo "<textarea>" . json_encode($payload) . "</textarea>"; return $this->sparkpost->request('POST', $this->endpoint, $payload, $header); } -} +}
\ No newline at end of file diff --git a/lib/SparkPost/Transmission.php b/lib/SparkPost/Transmission.php index a37a13c..4014631 100644 --- a/lib/SparkPost/Transmission.php +++ b/lib/SparkPost/Transmission.php @@ -11,6 +11,71 @@ class Transmission extends Resource parent::__construct($sparkpost, 'transmissions'); } + public function fixShorthandRecipients($payload){ + $modifiedPayload = $payload; + $recipients = &$modifiedPayload['recipients']; + $from = &$modifiedPayload['content']['from']; + $from = $this->shorthandRecipientsParser($from); + for($i = 0; $i < count($recipients); $i++){ + $recipients[$i]['address'] = $this->shorthandRecipientsParser($recipients[$i]['address']); + } + return $modifiedPayload; + } + + + /* + cases: + 1) 'vincentwsong@gmail.com' + 2) '"Vincent Song" <vincentwsong@gmail.com>' + 3) 'vincent song vincentwsong@gmail.com' + */ + public function shorthandRecipientsParser($value){ + + if(!is_array($value)){ //if the given value isn't an array + $name = ""; + $email = ""; + $newPerson = array(); + + if(preg_match('/"(.+)"/', $value, $matches)){ //if "NAME" is found + $name = $matches[0]; + if(preg_match('/<(.+)>/', $value, $matches)){ //if <EMAIL> is found + $email = $matches[1]; + if(!$this->isEmail($email)){ + throw new \Exception("Invalid email address. Use format \"NAME_HERE\" <EMAIL_HERE>"); + } else { + $newPerson = [ + 'name' => trim($name,'""'), + 'email' => $email + ]; + return $newPerson; + } + } else { //Has name, needs email in <EMAIL> format + throw new \Exception("Invalid email address. Use format \"NAME_HERE\" <EMAIL_HERE>"); + } + } else if ($this->isEmail($value)){ //if the original $value is just an email, like postmaster@sparkpost.com + $newPerson = [ + 'email' => $value + ]; + + return $newPerson; + } else { //$value isn't a valid email at all. E.g. postmastersparkpost.com + //echo $value; + throw new \Exception("Invalid email address."); + } + } else { //it's already an object, nothing we can do here + return $value; + } + } + + private function isEmail($email){ + if(filter_var($email, FILTER_VALIDATE_EMAIL)){ + return true; + } else { + return false; + } + } + + public function fixBlindCarbonCopy($payload) { //TODO: Manage recipients. "Vincent Song <vincentsong@sparkpost.com>" @@ -69,7 +134,7 @@ class Transmission extends Resource 'header_to' => $originalRecipient, ]; //if name exists, then use "Name" <Email> format. Otherwise, just email will suffice. - if(is_array($ccRecipient['address'])) { + if(is_array($ccRecipient['address'])) { $ccRecipientData = ' "' . $ccRecipient['address']['name'] . '" ' . '<' . $ccRecipient['address']['email'] . '>'; } else { @@ -99,6 +164,7 @@ class Transmission extends Resource { $modifiedPayload = $this->fixBlindCarbonCopy($payload); //Fixes BCCs into payload $modifiedPayload = $this->fixCarbonCopy($modifiedPayload); //Fixes CCs into payload + $modifiedPayload = $this->fixShorthandRecipients($modifiedPayload); return parent::post($modifiedPayload, $this->customHeaders); } } |