Skip to content

Commit

Permalink
Updated and tested all examples. README updated.
Browse files Browse the repository at this point in the history
Update transmissions GET & DELETE deprecations
  • Loading branch information
tuck1s committed Mar 16, 2021
1 parent eeb6ba9 commit 6171690
Show file tree
Hide file tree
Showing 20 changed files with 164 additions and 95 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,9 @@ test/output/
/composer.phar
composer.lock
test.php
.php-version
.gitignore
.phpunit.result.cache
.DS_Store
.gitignore
.vscode/launch.json
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased][unreleased]

## [2.2.1] - 2021-03-08
- [#200](https://github.com/SparkPost/php-sparkpost/pull/200) PHP 8 support

- [#198](https://github.com/SparkPost/php-sparkpost/pull/198)
- [#191](https://github.com/SparkPost/php-sparkpost/pull/191)
## [2.2.1] - 2021-03-08
- [#198](https://github.com/SparkPost/php-sparkpost/pull/198) Address #197: No longer need formfeed replacement. README work.
- [#191](https://github.com/SparkPost/php-sparkpost/pull/191) Updating License

## [2.2.0] - 2019-06-04
- [#187](https://github.com/SparkPost/php-sparkpost/pull/169) Updated composer.json
- [#187](https://github.com/SparkPost/php-sparkpost/pull/187) Updated composer.json
- [#169](https://github.com/SparkPost/php-sparkpost/pull/169) Optional automatic retry on 5xx
- [#166](https://github.com/SparkPost/php-sparkpost/pull/166/files) Quick fix for using the API without composer
- [#149](https://github.com/SparkPost/php-sparkpost/pull/149) Setters should return current object
Expand Down
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,9 @@ Sends an asynchronous request to the SparkPost API and returns a `SparkPostPromi
* Type: `Array`
* See constructor


---
## Endpoints
### transmissions
* **get([transmissionID] [, payload])**
* `transmissionID` - see `uri` request options
* `payload` - see request options
* **post(payload)**
* `payload` - see request options
* `payload.cc`
Expand All @@ -186,8 +183,8 @@ Sends an asynchronous request to the SparkPost API and returns a `SparkPostPromi
* Required: No
* Type: `Array`
* Recipients to descreetly recieve a carbon copy of the transmission
* **delete(transmissionID)**
* `transmissionID` - see `uri` request options

For complete list of endpoints, refer to [API documentation](https://developers.sparkpost.com/api/).

## Examples

Expand Down Expand Up @@ -252,6 +249,26 @@ var_dump($results);
?>
```

More examples [here](./examples/):
### Transmission
- Create with attachment
- Create with recipient list
- Create with cc and bcc
- Create with template
- Create
- Delete (scheduled transmission by campaign_id *only*)

### Template
- Create
- Get
- Get (list) all
- Update
- Delete

### Message Events
- get
- get (with retry logic)

### Send An API Call Using The Base Request Function
We provide a base request function to access any of our API resources.
```php
Expand All @@ -274,7 +291,6 @@ $promise = $sparky->request('GET', 'metrics/ip-pools', [
?>
```


## Handling Responses
The API calls either return a `SparkPostPromise` or `SparkPostResponse` depending on if `async` is `true` or `false`

Expand Down
4 changes: 2 additions & 2 deletions examples/debug/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* configure options in example-options.json
*/
$sparky = new SparkPost($httpClient, [
"key" => "YOUR_API_KEY",
// This will expose your API KEY - do not use this in production.
"key" => getenv('SPARKPOST_API_KEY'),
// fetch API KEY from environment variable
"debug" => true
]);

Expand Down
6 changes: 4 additions & 2 deletions examples/message-events/get_message_events.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

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

$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY",]);
// In these examples, fetch API key from environment variable
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);

$promise = $sparky->request('GET', 'message-events', [
// New endpoint - https://developers.sparkpost.com/api/events/
$promise = $sparky->request('GET', 'events/message', [
'campaign_ids' => 'CAMPAIGN_ID',
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@

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

$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY", "retries" => 3]);
// In these examples, fetch API key from environment variable
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY'), "retries" => 3]);

$promise = $sparky->request('GET', 'message-events', [
// New endpoint - https://developers.sparkpost.com/api/events/
$promise = $sparky->request('GET', 'events/message', [
'campaign_ids' => 'CAMPAIGN_ID',
]);

Expand Down
44 changes: 40 additions & 4 deletions examples/templates/create_template.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,50 @@

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

$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
// In these examples, fetch API key from environment variable
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);

$template_name = "PHP example template";
$template_id = "PHP-example-template";

// put your own sending domain here
$sending_domain = "steve2-test.trymsys.net";

// Valid short template content examples
$plain_text = 'Write your text message part here.';

$html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<body>
<p><strong>Write your HTML message part here</strong></p>
</body>
</html>
HTML;

$amp_html = <<<HTML
<!doctype html>
<html ⚡4email>
<head>
<meta charset="utf-8">
<style amp4email-boilerplate>body{visibility:hidden}</style>
<script async src="https://cdn.ampproject.org/v0.js"></script>
</head>
<body>
Hello World! Let's get started using AMP HTML together!
</body>
</html>
HTML;

$promise = $sparky->request('POST', 'templates', [
'name' => 'PHP example template',
'name' => $template_name,
'id' => $template_id,
'content' => [
'from' => 'from@YOUR_DOMAIN',
'from' => "from@$sending_domain",
'subject' => 'Your Subject',
'html' => '<b>Write your message here.</b>',
'text' => $plain_text,
'html' => $html,
'amp_html' => $amp_html,
],
]);

Expand Down
7 changes: 5 additions & 2 deletions examples/templates/delete_template.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@

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

$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
// In these examples, fetch API key from environment variable
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);

$promise = $sparky->request('DELETE', 'templates/TEMPLATE_ID');
$template_id = "PHP-example-template";

$promise = $sparky->request('DELETE', "templates/$template_id");

try {
$response = $promise->wait();
Expand Down
3 changes: 2 additions & 1 deletion examples/templates/get_all_templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

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

$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
// In these examples, fetch API key from environment variable
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);

$promise = $sparky->request('GET', 'templates');

Expand Down
7 changes: 5 additions & 2 deletions examples/templates/get_template.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@

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

$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
// In these examples, fetch API key from environment variable
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);

$promise = $sparky->request('GET', 'templates/TEMPLATE_ID?draft=true');
$template_id = "PHP-example-template";

$promise = $sparky->request('GET', "templates/$template_id?draft=true");

try {
$response = $promise->wait();
Expand Down
7 changes: 5 additions & 2 deletions examples/templates/preview_template.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@

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

$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
// In these examples, fetch API key from environment variable
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);

$promise = $sparky->request('POST', 'templates/TEMPLATE_ID/preview?draft=true', [
$template_id = "PHP-example-template";

$promise = $sparky->request('POST', "templates/$template_id/preview?draft=true", [
'substitution_data' => [
'some_key' => 'some_value',
],
Expand Down
7 changes: 5 additions & 2 deletions examples/templates/update_template.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@

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

$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
// In these examples, fetch API key from environment variable
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);

$promise = $sparky->request('PUT', 'templates/TEMPLATE_ID', [
$template_id = "PHP-example-template";

$promise = $sparky->request('PUT', "templates/$template_id", [
'options' => [
'open_tracking' => true,
],
Expand Down
11 changes: 8 additions & 3 deletions examples/transmissions/create_transmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@

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

$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
// In these examples, fetch API key from environment variable
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);

// put your own sending domain and test recipient address here
$sending_domain = "steve2-test.trymsys.net";
$your_email = "bob@sink.sparkpostmail.com";

$promise = $sparky->transmissions->post([
'content' => [
'from' => [
'name' => 'SparkPost Team',
'email' => 'from@sparkpostbox.com',
'email' => "from@$sending_domain",
],
'subject' => 'First Mailing From PHP',
'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent your very first mailing!</p></body></html>',
Expand All @@ -27,7 +32,7 @@
[
'address' => [
'name' => 'YOUR_NAME',
'email' => 'YOUR_EMAIL',
'email' => $your_email,
],
],
],
Expand Down
11 changes: 8 additions & 3 deletions examples/transmissions/create_transmission_with_attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,23 @@

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

$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
// In these examples, fetch API key from environment variable
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);

$filePath = dirname(__FILE__).'/';
$fileName = 'sparkpost.png';
$fileType = mime_content_type($filePath.$fileName);
$fileData = base64_encode(file_get_contents($filePath.$fileName));

// put your own sending domain and test recipient address here
$sending_domain = "steve2-test.trymsys.net";
$your_email = "bob@sink.sparkpostmail.com";

$promise = $sparky->transmissions->post([
'content' => [
'from' => [
'name' => 'SparkPost Team',
'email' => 'from@sparkpostbox.com',
'email' => "from@$sending_domain",
],
'subject' => 'Mailing With Attachment From PHP',
'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent an email with an attachment!</p></body></html>',
Expand All @@ -39,7 +44,7 @@
[
'address' => [
'name' => 'YOUR_NAME',
'email' => 'YOUR_EMAIL',
'email' => $your_email,
],
],
],
Expand Down
17 changes: 12 additions & 5 deletions examples/transmissions/create_transmission_with_cc_and_bcc.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@

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

$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
// In these examples, fetch API key from environment variable
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);

// put your own sending domain and test recipient address here
$sending_domain = "steve2-test.trymsys.net";
$your_email = "bob@sink.sparkpostmail.com";
$your_cc = "alice@sink.sparkpostmail.com";
$your_bcc = "charles@sink.sparkpostmail.com";

$promise = $sparky->transmissions->post([
'content' => [
'from' => [
'name' => 'SparkPost Team',
'email' => 'from@sparkpostbox.com',
'email' => "from@$sending_domain",
],
'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>',
Expand All @@ -27,23 +34,23 @@
[
'address' => [
'name' => 'YOUR_NAME',
'email' => 'YOUR_EMAIL',
'email' => $your_email,
],
],
],
'cc' => [
[
'address' => [
'name' => 'ANOTHER_NAME',
'email' => 'ANOTHER_EMAIL',
'email' => $your_cc,
],
],
],
'bcc' => [
[
'address' => [
'name' => 'AND_ANOTHER_NAME',
'email' => 'AND_ANOTHER_EMAIL',
'email' => $your_bcc,
],
],
],
Expand Down
13 changes: 10 additions & 3 deletions examples/transmissions/create_transmission_with_recipient_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@

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

$sparky = new SparkPost($httpClient, ["key" => "YOUR_API_KEY"]);
// In these examples, fetch API key from environment variable
$sparky = new SparkPost($httpClient, ["key" => getenv('SPARKPOST_API_KEY')]);

// put your own sending domain and test recipient address here
$sending_domain = "steve2-test.trymsys.net";

// The ID of a list in your SparkPost account
$my_list = "mylist1";

$promise = $sparky->transmissions->post([
'content' => [
'from' => [
'name' => 'SparkPost Team',
'email' => 'from@sparkpostbox.com',
'email' => "from@$sending_domain",
],
'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'],
'recipients' => ['list_id' => $my_list],
]);

try {
Expand Down
Loading

0 comments on commit 6171690

Please sign in to comment.