summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.travis.yml1
-rw-r--r--README.md81
-rw-r--r--examples/transmission/send_transmission_all_fields.php83
-rw-r--r--examples/transmission/simple_send.php31
-rw-r--r--examples/transmission/stored_recipients_inline_content.php23
-rw-r--r--examples/transmission/stored_template_send.php29
-rw-r--r--lib/SparkPost/APIResource.php3
-rw-r--r--lib/SparkPost/SparkPost.php2
-rw-r--r--test/unit/APIResourceTest.php15
9 files changed, 153 insertions, 115 deletions
diff --git a/.travis.yml b/.travis.yml
index 0547235..4d0c97b 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -2,6 +2,7 @@ language: php
php:
- '5.5'
- '5.6'
+ - '7.0'
install:
- composer install
before_script:
diff --git a/README.md b/README.md
index 49eae5c..3a8098f 100644
--- a/README.md
+++ b/README.md
@@ -68,26 +68,29 @@ $httpAdapter = new Guzzle6HttpAdapter(new Client());
$sparky = new SparkPost($httpAdapter, ['key'=>'YOUR API KEY']);
try {
- // Build your email and send it!
- $results = $sparky->transmission->send([
- 'from'=>'From Envelope <from@sparkpostbox.com>',
- '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!',
- 'substitutionData'=>['name'=>'YOUR FIRST NAME'],
- 'subject'=>'First Mailing From PHP',
- 'recipients'=>[
- [
- 'address'=>[
- 'name'=>'YOUR FULL NAME',
- 'email'=>'YOUR EMAIL ADDRESS'
+ // Build your email and send it!
+ $results = $sparky->transmission->send([
+ 'from'=>[
+ 'name' => 'From Envelope',
+ 'email' => 'from@sparkpostbox.com>'
+ ],
+ '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!',
+ 'substitutionData'=>['name'=>'YOUR FIRST NAME'],
+ 'subject'=>'First Mailing From PHP',
+ 'recipients'=>[
+ [
+ 'address'=>[
+ 'name'=>'YOUR FULL NAME',
+ 'email'=>'YOUR EMAIL ADDRESS'
+ ]
+ ]
]
- ]
- ]
- ]);
- echo 'Woohoo! You just sent your first mailing!';
+ ]);
+ echo 'Woohoo! You just sent your first mailing!';
} catch (\Exception $err) {
- echo 'Whoops! Something went wrong';
- var_dump($err);
+ echo 'Whoops! Something went wrong';
+ var_dump($err);
}
```
@@ -98,25 +101,29 @@ try {
## Field Descriptions
### Transmissions
-| Field Name | Required? | Description | Data Type |
-| ------------ | ----------- | ------------- | ----------- |
-| description | no | Field for describing what this transmission is for the user | String |
-| campaign | no | Field for assigning a given transmission to a specific campaign, which is a logical container for similar transmissions | String |
-| metadata | no | Field for adding arbitrary key/value pairs which will be included in open/click tracking | Object (Simple) |
-| substitutionData | no | Field for adding transmission level substitution data, which can be used in a variety of fields and in content | Object (Complex) |
-| trackOpens | no | Field for enabling/disabling transmission level open tracking (default: true) | Boolean |
-| trackClicks | no | Field for enabling/disabling transmission level click tracking (default: true) | Boolean |
-| useDraftTemplate | no | Field for allowing the sending of a transmission using a draft of a stored template (default: false) | Boolean |
-| replyTo | no | Field for specifying the email address that should be used when a recipient hits the reply button | String |
-| subject | yes | Field for setting the subject line of a given transmission | String |
-| from | yes** | Field for setting the from line of a given transmission | String or Object |
-| html | yes** | Field for setting the HTML content of a given transmission | String |
-| text | yes** | Field for setting the Plain Text content of a given transmission | String |
-| rfc822 | no** | Field for setting the RFC-822 encoded content of a given transmission | String |
-| template | no** | Field for specifying the Template ID of a stored template to be used when sending a given transmission | String |
-| customHeaders | no | Field for specifying additional headers to be applied to a given transmission (other than Subject, From, To, and Reply-To) | Object (Simple) |
-| recipients | yes** | Field for specifying who a given transmission should be sent to | Array of Objects |
-| recipientList | no** | Field for specifying a stored recipient list ID to be used for a given transmission | String |
+| Field Name | Required? | Description | Data Type |
+| ------------ | ----------- | ------------- | ----------- |
+| attachments | no | Field for attaching files - see Attachment Attributes in the [Transmssions API docs](https://developers.sparkpost.com/api/#/reference/transmissions) | Array of Objects |
+| campaign | no | Field for assigning a given transmission to a specific campaign, which is a logical container for similar transmissions | String |
+| customHeaders | no | Field for specifying additional headers to be applied to a given transmission (other than Subject, From, To, and Reply-To) | Object (Simple) |
+| description | no | Field for describing what this transmission is for the user | String |
+| from | yes** | Field for setting the from line of a given transmission | Object |
+| html | yes** | Field for setting the HTML content of a given transmission | String |
+| inlineCss | no | Field for enabling/disabling CSS inlining | Boolean |
+| inlineImages | no | Field for providing inline images - see Inline Image Attributes in the [Transmssions API docs](https://developers.sparkpost.com/api/#/reference/transmissions) | Array of Objects |
+| metadata | no | Field for adding arbitrary key/value pairs which will be included in open/click tracking | Object (Simple) |
+| recipientList | no** | Field for specifying a stored recipient list ID to be used for a given transmission | String |
+| recipients | yes** | Field for specifying who a given transmission should be sent to | Array of Objects |
+| replyTo | no | Field for specifying the email address that should be used when a recipient hits the reply button | String |
+| rfc822 | no** | Field for setting the RFC-822 encoded content of a given transmission | String |
+| subject | yes | Field for setting the subject line of a given transmission | String |
+| substitutionData | no | Field for adding transmission level substitution data, which can be used in a variety of fields and in content | Object (Complex) |
+| template | no** | Field for specifying the Template ID of a stored template to be used when sending a given transmission | String |
+| text | yes** | Field for setting the Plain Text content of a given transmission | String |
+| trackClicks | no | Field for enabling/disabling transmission level click tracking (default: true) | Boolean |
+| trackOpens | no | Field for enabling/disabling transmission level open tracking (default: true) | Boolean |
+| transactional | no | Field for marking email as transactional (default: false) | Boolean |
+| useDraftTemplate | no | Field for allowing the sending of a transmission using a draft of a stored template (default: false) | Boolean |
** - If using inline content then html or text are required. If using RFC-822 Inline Content, then rfc822 is required. If using a stored recipient list, then recipientList is required. If using a stored template, then template is required but from is not as the values from the template will be used.
diff --git a/examples/transmission/send_transmission_all_fields.php b/examples/transmission/send_transmission_all_fields.php
index 7d43918..35115e7 100644
--- a/examples/transmission/send_transmission_all_fields.php
+++ b/examples/transmission/send_transmission_all_fields.php
@@ -16,48 +16,51 @@ $sparky = new SparkPost($httpAdapter, ['key'=>$config['api-key']]);
$data = file_get_contents('/path/to/test.csv');
try{
- $results = $sparky->transmission->send([
- 'campaign'=>'my-campaign',
- 'metadata'=>[
- 'sample_campaign'=>true,
- 'type'=>'these are custom fields'
- ],
- 'substitutionData'=>[
- 'name'=>'Test Name'
- ],
- 'description'=>'my description',
- 'replyTo'=>'reply@test.com',
- 'customHeaders'=>[
- 'X-Custom-Header'=>'Sample Custom Header'
- ],
- 'trackOpens'=>false,
- 'trackClicks'=>false,
- 'sandbox'=>false,
- 'inlineCss'=>true,
- 'transactional'=>true,
- 'startTime'=>'2016-03-17T08:00:00-04:00',
- 'from'=>'From Envelope <from@sparkpostbox.com>',
- 'html'=>'<p>Hello World! Your name is: {{name}}</p>',
- 'text'=>'Hello World!',
- 'subject'=>'Example Email: {{name}}',
- 'recipients'=>[
- [
- 'address'=>[
- 'email'=>'john.doe@example.com'
+ $results = $sparky->transmission->send([
+ 'campaign'=>'my-campaign',
+ 'metadata'=>[
+ 'sample_campaign'=>true,
+ 'type'=>'these are custom fields'
+ ],
+ 'substitutionData'=>[
+ 'name'=>'Test Name'
+ ],
+ 'description'=>'my description',
+ 'replyTo'=>'reply@test.com',
+ 'customHeaders'=>[
+ 'X-Custom-Header'=>'Sample Custom Header'
+ ],
+ 'trackOpens'=>false,
+ 'trackClicks'=>false,
+ 'sandbox'=>false,
+ 'inlineCss'=>true,
+ 'transactional'=>true,
+ 'startTime'=>'2016-03-17T08:00:00-04:00',
+ 'from'=>[
+ 'name' => 'From Envelope',
+ 'email' => 'from@sparkpostbox.com>'
+ ],
+ 'html'=>'<p>Hello World! Your name is: {{name}}</p>',
+ 'text'=>'Hello World!',
+ 'subject'=>'Example Email: {{name}}',
+ 'recipients'=>[
+ [
+ 'address'=>[
+ 'email'=>'john.doe@example.com'
+ ]
+ ]
+ ],
+ 'attachments'=>[
+ [
+ 'type'=>'text/csv',
+ 'name'=>'testing.csv',
+ 'data'=>base64_encode($data)
+ ]
]
- ]
- ],
- 'attachments'=>[
- [
- 'type'=>'text/csv',
- 'name'=>'testing.csv',
- 'data'=>base64_encode($data)
- ]
- ]
- ]);
+ ]);
- echo 'Congrats you can use your SDK!';
+ echo 'Congrats you can use your SDK!';
} catch (\Exception $exception) {
- echo $exception->getMessage();
+ echo $exception->getMessage();
}
?>
diff --git a/examples/transmission/simple_send.php b/examples/transmission/simple_send.php
index 5844ecc..245af70 100644
--- a/examples/transmission/simple_send.php
+++ b/examples/transmission/simple_send.php
@@ -14,21 +14,24 @@ $httpAdapter = new Guzzle6HttpAdapter(new Client());
$sparky = new SparkPost($httpAdapter, ['key'=>$config['api-key']]);
try {
- $results = $sparky->transmission->send([
- 'from'=>'From Envelope <from@sparkpostbox.com>',
- 'html'=>'<p>Hello World!</p>',
- 'text'=>'Hello World!',
- 'subject'=>'Example Email',
- 'recipients'=>[
- [
- 'address'=>[
- 'email'=>'john.doe@example.com'
+ $results = $sparky->transmission->send([
+ 'from'=>[
+ 'name' => 'From Envelope',
+ 'email' => 'from@sparkpostbox.com>'
+ ],
+ 'html'=>'<p>Hello World!</p>',
+ 'text'=>'Hello World!',
+ 'subject'=>'Example Email',
+ 'recipients'=>[
+ [
+ 'address'=>[
+ 'email'=>'john.doe@example.com'
+ ]
+ ]
]
- ]
- ]
- ]);
- echo 'Congrats you can use your SDK!';
+ ]);
+ echo 'Congrats you can use your SDK!';
} catch (\Exception $exception) {
- echo $exception->getMessage();
+ echo $exception->getMessage();
}
?>
diff --git a/examples/transmission/stored_recipients_inline_content.php b/examples/transmission/stored_recipients_inline_content.php
index 9348f9b..8a605f5 100644
--- a/examples/transmission/stored_recipients_inline_content.php
+++ b/examples/transmission/stored_recipients_inline_content.php
@@ -15,17 +15,20 @@ $sparky = new SparkPost($httpAdapter, ['key'=>$config['api-key']]);
try {
- $results = $sparky->transmission->send([
- 'campaign'=>'my-campaign',
- 'from'=>'From Envelope <from@sparkpostbox.com>',
- 'html'=>'<p>Hello World! Your name is: {{name}}</p>',
- 'text'=>'Hello World!',
- 'subject'=>'Example Email: {{name}}',
- 'recipientList'=>'Example List'
- ]);
+ $results = $sparky->transmission->send([
+ 'campaign'=>'my-campaign',
+ 'from'=>[
+ 'name' => 'From Envelope',
+ 'email' => 'from@sparkpostbox.com>'
+ ],
+ 'html'=>'<p>Hello World! Your name is: {{name}}</p>',
+ 'text'=>'Hello World!',
+ 'subject'=>'Example Email: {{name}}',
+ 'recipientList'=>'Example List'
+ ]);
- echo 'Congrats you can use your SDK!';
+ echo 'Congrats you can use your SDK!';
} catch (\Exception $exception) {
- echo $exception->getMessage();
+ echo $exception->getMessage();
}
?>
diff --git a/examples/transmission/stored_template_send.php b/examples/transmission/stored_template_send.php
index 2e22609..3df4a95 100644
--- a/examples/transmission/stored_template_send.php
+++ b/examples/transmission/stored_template_send.php
@@ -14,19 +14,22 @@ $httpAdapter = new Guzzle6HttpAdapter(new Client());
$sparky = new SparkPost($httpAdapter, ['key'=>$config['api-key']]);
try {
- $results = $sparky->transmission->send([
- 'from'=>'From Envelope <from@sparkpostbox.com>',
- 'recipients'=>[
- [
- 'address'=>[
- 'email'=>'john.doe@example.com'
- ]
- ]
- ],
- 'template'=>'my-first-email'
- ]);
- echo 'Congrats you can use your SDK!';
+ $results = $sparky->transmission->send([
+ 'from'=>[
+ 'name' => 'From Envelope',
+ 'email' => 'from@sparkpostbox.com>'
+ ],
+ 'recipients'=>[
+ [
+ 'address'=>[
+ 'email'=>'john.doe@example.com'
+ ]
+ ]
+ ],
+ 'template'=>'my-first-email'
+ ]);
+ echo 'Congrats you can use your SDK!';
} catch (\Exception $exception) {
- echo $exception->getMessage();
+ echo $exception->getMessage();
}
?>
diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php
index c4a08ae..10305f2 100644
--- a/lib/SparkPost/APIResource.php
+++ b/lib/SparkPost/APIResource.php
@@ -201,6 +201,9 @@ class APIResource {
if ($statusCode < 400) {
return json_decode($response->getBody()->getContents(), true);
}
+ elseif ($statusCode === 403) {
+ throw new APIResponseException('Request forbidden. Does this API Key have the correct SparkPost permissions?');
+ }
elseif ($statusCode === 404) {
throw new APIResponseException('The specified resource does not exist', 404);
}
diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php
index 48d6a9c..e5f029a 100644
--- a/lib/SparkPost/SparkPost.php
+++ b/lib/SparkPost/SparkPost.php
@@ -117,7 +117,7 @@ class SparkPost {
}
// Validate API key because its required
- if (!isset($settingsConfig['key']) || empty(trim($settingsConfig['key']))){
+ if (!isset($settingsConfig['key']) || !preg_match('/\S/', $settingsConfig['key'])){
throw new \Exception('You must provide an API key');
}
diff --git a/test/unit/APIResourceTest.php b/test/unit/APIResourceTest.php
index 0c4fb16..c2b0c2a 100644
--- a/test/unit/APIResourceTest.php
+++ b/test/unit/APIResourceTest.php
@@ -130,6 +130,21 @@ class APIResourceTest extends \PHPUnit_Framework_TestCase {
}
}
+ public function testAdapter403Exception() {
+ try {
+ $responseMock = Mockery::mock();
+ $this->sparkPostMock->httpAdapter->shouldReceive('send')->
+ once()->
+ andReturn($responseMock);
+ $responseMock->shouldReceive('getStatusCode')->andReturn(403);
+
+ $this->resource->get('test');
+ }
+ catch(\Exception $e) {
+ $this->assertRegExp('/Request forbidden/', $e->getMessage());
+ }
+ }
+
public function testAdapter4XXException() {
try {
$testBody = ['errors'=>['my'=>'test']];