Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating examples #139

Merged
merged 7 commits into from
Aug 25, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/transmissions/create_transmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
],
'subject' => 'First Mailing From PHP',
'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent your very first mailing!</p></body></html>',
'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' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
'name' => 'SparkPost Team',
'email' => '[email protected]',
],
'subject' => 'First Mailing From PHP',
'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent your very first mailing!</p></body></html>',
'text' => 'Congratulations, {{name}}!! You just sent your very first mailing!',
'subject' => 'Mailing With CC and BCC From PHP',
'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent your very first mailing with CC and BCC recipients!</p></body></html>',
'text' => 'Congratulations, {{name}}! You just sent your very first mailing with CC and BCC recipients!',
],
'substitution_data' => ['name' => 'YOUR_FIRST_NAME'],
'recipients' => [
Expand Down
39 changes: 39 additions & 0 deletions examples/transmissions/create_transmission_with_recipient_list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Examples\Transmissions;

require dirname(__FILE__).'/../bootstrap.php';

use SparkPost\SparkPost;
use GuzzleHttp\Client;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;

$httpClient = new GuzzleAdapter(new Client());

/*
* configure options in example-options.json
*/
$sparky = new SparkPost($httpClient, $options);

$promise = $sparky->transmissions->post([
'content' => [
'from' => [
'name' => 'SparkPost Team',
'email' => '[email protected]',
],
'subject' => 'Mailing With Recipient List From PHP',
'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent an email to everyone on your recipient list!</p></body></html>',
'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";
}
38 changes: 38 additions & 0 deletions examples/transmissions/create_transmission_with_template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Examples\Transmissions;

require dirname(__FILE__).'/../bootstrap.php';

use SparkPost\SparkPost;
use GuzzleHttp\Client;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;

$httpClient = new GuzzleAdapter(new Client());

/*
* configure options in example-options.json
*/
$sparky = new SparkPost($httpClient, $options);

$promise = $sparky->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";
}
27 changes: 27 additions & 0 deletions examples/transmissions/get_transmission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Examples\Transmissions;

require dirname(__FILE__).'/../bootstrap.php';

use SparkPost\SparkPost;
use GuzzleHttp\Client;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;

$httpClient = new GuzzleAdapter(new Client());

/*
* configure options in example-options.json
*/
$sparky = new SparkPost($httpClient, $options);

$promise = $sparky->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";
}
22 changes: 12 additions & 10 deletions lib/SparkPost/Transmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking we should add a test to cover this

$payload = $this->formatPayload($payload);
}

return parent::post($payload, $headers);
}
Expand All @@ -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)
{
Expand All @@ -42,7 +44,7 @@ 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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand All @@ -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 <Email address>"
*
*
* @return array - the longhand form of an email address [ "name" => "John", "email" => "[email protected]" ]
*/
private function toAddressObject($address)
Expand All @@ -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" => "[email protected]" ]
* @param string - the shorthand form of an email address "Name <Email address>"
*/
Expand All @@ -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
*/
Expand Down