diff --git a/examples/message-events/get_message_events.php b/examples/message-events/get_message_events.php new file mode 100644 index 0000000..bff4300 --- /dev/null +++ b/examples/message-events/get_message_events.php @@ -0,0 +1,29 @@ +request('GET', 'message-events', [ + 'campaign_ids' => 'CAMPAIGN_ID' +]); + +try { + $response = $promise->wait(); + echo $response->getStatusCode()."\n"; + print_r($response->getBody())."\n"; +} catch (\Exception $e) { + echo $e->getCode()."\n"; + echo $e->getMessage()."\n"; +} diff --git a/examples/templates/create_template.php b/examples/templates/create_template.php new file mode 100644 index 0000000..c25b900 --- /dev/null +++ b/examples/templates/create_template.php @@ -0,0 +1,34 @@ +request('POST', 'templates', [ + "name" => "PHP example template", + "content" => [ + "from" => "from@YOUR_DOMAIN", + "subject" => "Your Subject", + "html" => "Write your message here." + ] +]); + +try { + $response = $promise->wait(); + echo $response->getStatusCode()."\n"; + print_r($response->getBody())."\n"; +} catch (\Exception $e) { + echo $e->getCode()."\n"; + echo $e->getMessage()."\n"; +} diff --git a/examples/templates/delete_template.php b/examples/templates/delete_template.php new file mode 100644 index 0000000..0985c5f --- /dev/null +++ b/examples/templates/delete_template.php @@ -0,0 +1,27 @@ +request('DELETE', 'templates/TEMPLATE_ID'); + +try { + $response = $promise->wait(); + echo $response->getStatusCode()."\n"; + print_r($response->getBody())."\n"; +} catch (\Exception $e) { + echo $e->getCode()."\n"; + echo $e->getMessage()."\n"; +} diff --git a/examples/templates/get_all_templates.php b/examples/templates/get_all_templates.php new file mode 100644 index 0000000..25e222c --- /dev/null +++ b/examples/templates/get_all_templates.php @@ -0,0 +1,27 @@ +request('GET', 'templates'); + +try { + $response = $promise->wait(); + echo $response->getStatusCode()."\n"; + print_r($response->getBody())."\n"; +} catch (\Exception $e) { + echo $e->getCode()."\n"; + echo $e->getMessage()."\n"; +} diff --git a/examples/templates/get_template.php b/examples/templates/get_template.php new file mode 100644 index 0000000..9ae977e --- /dev/null +++ b/examples/templates/get_template.php @@ -0,0 +1,27 @@ +request('GET', 'templates/TEMPLATE_ID?draft=true'); + +try { + $response = $promise->wait(); + echo $response->getStatusCode()."\n"; + print_r($response->getBody())."\n"; +} catch (\Exception $e) { + echo $e->getCode()."\n"; + echo $e->getMessage()."\n"; +} diff --git a/examples/templates/preview_template.php b/examples/templates/preview_template.php new file mode 100644 index 0000000..cdfa055 --- /dev/null +++ b/examples/templates/preview_template.php @@ -0,0 +1,31 @@ +request('POST', 'templates/TEMPLATE_ID/preview?draft=true', [ + 'substitution_data' => [ + 'some_key' => 'some_value' + ] +]); + +try { + $response = $promise->wait(); + echo $response->getStatusCode()."\n"; + print_r($response->getBody())."\n"; +} catch (\Exception $e) { + echo $e->getCode()."\n"; + echo $e->getMessage()."\n"; +} diff --git a/examples/templates/update_template.php b/examples/templates/update_template.php new file mode 100644 index 0000000..1f467ce --- /dev/null +++ b/examples/templates/update_template.php @@ -0,0 +1,31 @@ +request('PUT', 'templates/TEMPLATE_ID', [ + 'options' => [ + 'open_tracking' => true + ] +]); + +try { + $response = $promise->wait(); + echo $response->getStatusCode()."\n"; + print_r($response->getBody())."\n"; +} catch (\Exception $e) { + echo $e->getCode()."\n"; + echo $e->getMessage()."\n"; +} diff --git a/examples/transmissions/create_transmission.php b/examples/transmissions/create_transmission.php index 61478a8..a7a1914 100644 --- a/examples/transmissions/create_transmission.php +++ b/examples/transmissions/create_transmission.php @@ -23,7 +23,7 @@ ], 'subject' => 'First Mailing From PHP', 'html' => '

Congratulations, {{name}}!

You just sent your very first mailing!

', - 'text' => 'Congratulations, {{name}}!! You just sent your very first mailing!', + 'text' => 'Congratulations, {{name}}! You just sent your very first mailing!', ], 'substitution_data' => ['name' => 'YOUR_FIRST_NAME'], 'recipients' => [ diff --git a/examples/transmissions/create_transmission_with_cc_and_bcc.php b/examples/transmissions/create_transmission_with_cc_and_bcc.php index eab64b8..5e1bf23 100644 --- a/examples/transmissions/create_transmission_with_cc_and_bcc.php +++ b/examples/transmissions/create_transmission_with_cc_and_bcc.php @@ -21,9 +21,9 @@ 'name' => 'SparkPost Team', 'email' => 'from@sparkpostbox.com', ], - 'subject' => 'First Mailing From PHP', - 'html' => '

Congratulations, {{name}}!

You just sent your very first mailing!

', - 'text' => 'Congratulations, {{name}}!! You just sent your very first mailing!', + 'subject' => 'Mailing With CC and BCC From PHP', + 'html' => '

Congratulations, {{name}}!

You just sent your very first mailing with CC and BCC recipients!

', + 'text' => 'Congratulations, {{name}}! You just sent your very first mailing with CC and BCC recipients!', ], 'substitution_data' => ['name' => 'YOUR_FIRST_NAME'], 'recipients' => [ diff --git a/examples/transmissions/create_transmission_with_recipient_list.php b/examples/transmissions/create_transmission_with_recipient_list.php new file mode 100644 index 0000000..e658b0d --- /dev/null +++ b/examples/transmissions/create_transmission_with_recipient_list.php @@ -0,0 +1,39 @@ +transmissions->post([ + 'content' => [ + 'from' => [ + 'name' => 'SparkPost Team', + 'email' => 'from@sparkpostbox.com', + ], + 'subject' => 'Mailing With Recipient List From PHP', + 'html' => '

Congratulations, {{name}}!

You just sent an email to everyone on your recipient list!

', + 'text' => 'Congratulations, {{name}}! You just sent an email to everyone on your recipient list!', + ], + 'substitution_data' => ['name' => 'YOUR_FIRST_NAME'], + 'recipients' => ['list_id' => 'RECIPIENT_LIST_ID'], +]); + +try { + $response = $promise->wait(); + echo $response->getStatusCode()."\n"; + print_r($response->getBody())."\n"; +} catch (\Exception $e) { + echo $e->getCode()."\n"; + echo $e->getMessage()."\n"; +} diff --git a/examples/transmissions/create_transmission_with_template.php b/examples/transmissions/create_transmission_with_template.php new file mode 100644 index 0000000..e8f3fb6 --- /dev/null +++ b/examples/transmissions/create_transmission_with_template.php @@ -0,0 +1,38 @@ +transmissions->post([ + 'content' => ['template_id' => 'TEMPLATE_ID'], + 'substitution_data' => ['name' => 'YOUR_FIRST_NAME'], + 'recipients' => [ + [ + 'address' => [ + 'name' => 'YOUR_NAME', + 'email' => 'YOUR_EMAIL', + ], + ], + ], +]); + +try { + $response = $promise->wait(); + echo $response->getStatusCode()."\n"; + print_r($response->getBody())."\n"; +} catch (\Exception $e) { + echo $e->getCode()."\n"; + echo $e->getMessage()."\n"; +} diff --git a/examples/transmissions/get_transmission.php b/examples/transmissions/get_transmission.php new file mode 100644 index 0000000..b149107 --- /dev/null +++ b/examples/transmissions/get_transmission.php @@ -0,0 +1,27 @@ +transmissions->get('TRANSMISSION_ID'); + +try { + $response = $promise->wait(); + echo $response->getStatusCode()."\n"; + print_r($response->getBody())."\n"; +} catch (\Exception $e) { + echo $e->getCode()."\n"; + echo $e->getMessage()."\n"; +} diff --git a/lib/SparkPost/Transmission.php b/lib/SparkPost/Transmission.php index 4c2eda8..6e44ab5 100644 --- a/lib/SparkPost/Transmission.php +++ b/lib/SparkPost/Transmission.php @@ -16,7 +16,9 @@ public function __construct(SparkPost $sparkpost) */ public function post($payload = [], $headers = []) { - $payload = $this->formatPayload($payload); + if (isset($payload['recipients']) && !isset($payload['recipients']['list_id'])) { + $payload = $this->formatPayload($payload); + } return parent::post($payload, $headers); } @@ -26,7 +28,7 @@ public function post($payload = [], $headers = []) * * @param array $payload - the request body * - * @return array - the modified request body + * @return array - the modified request body */ public function formatPayload($payload) { @@ -42,12 +44,12 @@ public function formatPayload($payload) * * @param array $payload - the request body * - * @return array - the modified request body + * @return array - the modified request body */ private function formatBlindCarbonCopy($payload) { - //If there's a list of BCC recipients, move then into the correct format + //If there's a list of BCC recipients, move them into the correct format if (isset($payload['bcc'])) { $payload = $this->addListToRecipients($payload, 'bcc'); } @@ -60,7 +62,7 @@ private function formatBlindCarbonCopy($payload) * * @param array $payload - the request body * - * @return array - the modified request body + * @return array - the modified request body */ private function formatCarbonCopy($payload) { @@ -86,7 +88,7 @@ private function formatCarbonCopy($payload) * * @param array $payload - the request body * - * @return array - the modified request body + * @return array - the modified request body */ private function formatShorthandRecipients($payload) { @@ -107,7 +109,7 @@ private function formatShorthandRecipients($payload) * @param array $payload - the request body * @param array $listName - the name of the array in the payload to be moved to the recipients list * - * @return array - the modified request body + * @return array - the modified request body */ private function addListToRecipients($payload, $listName) { @@ -132,9 +134,9 @@ private function addListToRecipients($payload, $listName) /** * Takes the shorthand form of an email address and converts it to the long form. - * + * * @param $address - the shorthand form of an email address "Name " - * + * * @return array - the longhand form of an email address [ "name" => "John", "email" => "john@exmmple.com" ] */ private function toAddressObject($address) @@ -159,7 +161,7 @@ private function toAddressObject($address) /** * Takes the longhand form of an email address and converts it to the shorthand form. - * + * * @param $address - the longhand form of an email address [ "name" => "John", "email" => "john@exmmple.com" ] * @param string - the shorthand form of an email address "Name " */ @@ -179,7 +181,7 @@ private function toAddressString($address) /** * Checks if a string is an email. - * + * * @param string $email - a string that might be an email address * @param bool - true if the given string is an email */ diff --git a/test/unit/TransmissionTest.php b/test/unit/TransmissionTest.php index 5af876e..c956c12 100644 --- a/test/unit/TransmissionTest.php +++ b/test/unit/TransmissionTest.php @@ -145,6 +145,31 @@ public function testPost() $this->assertEquals(200, $response->getStatusCode()); } + public function testPostWithRecipientList() + { + $postTransmissionPayload = $this->postTransmissionPayload; + $postTransmissionPayload['recipients'] = ['list_id' => 'SOME_LIST_ID']; + + $responseMock = Mockery::mock('Psr\Http\Message\ResponseInterface'); + $responseBodyMock = Mockery::mock(); + + $responseBody = ['results' => 'yay']; + + $this->clientMock->shouldReceive('sendRequest')-> + once()-> + with(Mockery::type('GuzzleHttp\Psr7\Request'))-> + andReturn($responseMock); + + $responseMock->shouldReceive('getStatusCode')->andReturn(200); + $responseMock->shouldReceive('getBody')->andReturn($responseBodyMock); + $responseBodyMock->shouldReceive('__toString')->andReturn(json_encode($responseBody)); + + $response = $this->resource->transmissions->post(); + + $this->assertEquals($responseBody, $response->getBody()); + $this->assertEquals(200, $response->getStatusCode()); + } + public function testDelete() { $responseMock = Mockery::mock('Psr\Http\Message\ResponseInterface');