Skip to content

Commit 1bbefef

Browse files
authored
Updating examples (#139)
* added example of getting transmission by ID * updated example messages and added example with recipient list and template * fixed lib to work with recipient lists * fixed lib to work with recipient lists * added test covering recipient list * added message event example * added template examples
1 parent e325c04 commit 1bbefef

14 files changed

+352
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Examples\Templates;
4+
5+
require dirname(__FILE__).'/../bootstrap.php';
6+
7+
use SparkPost\SparkPost;
8+
use GuzzleHttp\Client;
9+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
10+
11+
$httpClient = new GuzzleAdapter(new Client());
12+
13+
/*
14+
* configure options in example-options.json
15+
*/
16+
$sparky = new SparkPost($httpClient, $options);
17+
18+
$promise = $sparky->request('GET', 'message-events', [
19+
'campaign_ids' => 'CAMPAIGN_ID'
20+
]);
21+
22+
try {
23+
$response = $promise->wait();
24+
echo $response->getStatusCode()."\n";
25+
print_r($response->getBody())."\n";
26+
} catch (\Exception $e) {
27+
echo $e->getCode()."\n";
28+
echo $e->getMessage()."\n";
29+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Examples\Templates;
4+
5+
require dirname(__FILE__).'/../bootstrap.php';
6+
7+
use SparkPost\SparkPost;
8+
use GuzzleHttp\Client;
9+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
10+
11+
$httpClient = new GuzzleAdapter(new Client());
12+
13+
/*
14+
* configure options in example-options.json
15+
*/
16+
$sparky = new SparkPost($httpClient, $options);
17+
18+
$promise = $sparky->request('POST', 'templates', [
19+
"name" => "PHP example template",
20+
"content" => [
21+
"from" => "from@YOUR_DOMAIN",
22+
"subject" => "Your Subject",
23+
"html" => "<b>Write your message here.</b>"
24+
]
25+
]);
26+
27+
try {
28+
$response = $promise->wait();
29+
echo $response->getStatusCode()."\n";
30+
print_r($response->getBody())."\n";
31+
} catch (\Exception $e) {
32+
echo $e->getCode()."\n";
33+
echo $e->getMessage()."\n";
34+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Examples\Templates;
4+
5+
require dirname(__FILE__).'/../bootstrap.php';
6+
7+
use SparkPost\SparkPost;
8+
use GuzzleHttp\Client;
9+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
10+
11+
$httpClient = new GuzzleAdapter(new Client());
12+
13+
/*
14+
* configure options in example-options.json
15+
*/
16+
$sparky = new SparkPost($httpClient, $options);
17+
18+
$promise = $sparky->request('DELETE', 'templates/TEMPLATE_ID');
19+
20+
try {
21+
$response = $promise->wait();
22+
echo $response->getStatusCode()."\n";
23+
print_r($response->getBody())."\n";
24+
} catch (\Exception $e) {
25+
echo $e->getCode()."\n";
26+
echo $e->getMessage()."\n";
27+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Examples\Templates;
4+
5+
require dirname(__FILE__).'/../bootstrap.php';
6+
7+
use SparkPost\SparkPost;
8+
use GuzzleHttp\Client;
9+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
10+
11+
$httpClient = new GuzzleAdapter(new Client());
12+
13+
/*
14+
* configure options in example-options.json
15+
*/
16+
$sparky = new SparkPost($httpClient, $options);
17+
18+
$promise = $sparky->request('GET', 'templates');
19+
20+
try {
21+
$response = $promise->wait();
22+
echo $response->getStatusCode()."\n";
23+
print_r($response->getBody())."\n";
24+
} catch (\Exception $e) {
25+
echo $e->getCode()."\n";
26+
echo $e->getMessage()."\n";
27+
}

examples/templates/get_template.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Examples\Templates;
4+
5+
require dirname(__FILE__).'/../bootstrap.php';
6+
7+
use SparkPost\SparkPost;
8+
use GuzzleHttp\Client;
9+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
10+
11+
$httpClient = new GuzzleAdapter(new Client());
12+
13+
/*
14+
* configure options in example-options.json
15+
*/
16+
$sparky = new SparkPost($httpClient, $options);
17+
18+
$promise = $sparky->request('GET', 'templates/TEMPLATE_ID?draft=true');
19+
20+
try {
21+
$response = $promise->wait();
22+
echo $response->getStatusCode()."\n";
23+
print_r($response->getBody())."\n";
24+
} catch (\Exception $e) {
25+
echo $e->getCode()."\n";
26+
echo $e->getMessage()."\n";
27+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Examples\Templates;
4+
5+
require dirname(__FILE__).'/../bootstrap.php';
6+
7+
use SparkPost\SparkPost;
8+
use GuzzleHttp\Client;
9+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
10+
11+
$httpClient = new GuzzleAdapter(new Client());
12+
13+
/*
14+
* configure options in example-options.json
15+
*/
16+
$sparky = new SparkPost($httpClient, $options);
17+
18+
$promise = $sparky->request('POST', 'templates/TEMPLATE_ID/preview?draft=true', [
19+
'substitution_data' => [
20+
'some_key' => 'some_value'
21+
]
22+
]);
23+
24+
try {
25+
$response = $promise->wait();
26+
echo $response->getStatusCode()."\n";
27+
print_r($response->getBody())."\n";
28+
} catch (\Exception $e) {
29+
echo $e->getCode()."\n";
30+
echo $e->getMessage()."\n";
31+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Examples\Templates;
4+
5+
require dirname(__FILE__).'/../bootstrap.php';
6+
7+
use SparkPost\SparkPost;
8+
use GuzzleHttp\Client;
9+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
10+
11+
$httpClient = new GuzzleAdapter(new Client());
12+
13+
/*
14+
* configure options in example-options.json
15+
*/
16+
$sparky = new SparkPost($httpClient, $options);
17+
18+
$promise = $sparky->request('PUT', 'templates/TEMPLATE_ID', [
19+
'options' => [
20+
'open_tracking' => true
21+
]
22+
]);
23+
24+
try {
25+
$response = $promise->wait();
26+
echo $response->getStatusCode()."\n";
27+
print_r($response->getBody())."\n";
28+
} catch (\Exception $e) {
29+
echo $e->getCode()."\n";
30+
echo $e->getMessage()."\n";
31+
}

examples/transmissions/create_transmission.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
],
2424
'subject' => 'First Mailing From PHP',
2525
'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent your very first mailing!</p></body></html>',
26-
'text' => 'Congratulations, {{name}}!! You just sent your very first mailing!',
26+
'text' => 'Congratulations, {{name}}! You just sent your very first mailing!',
2727
],
2828
'substitution_data' => ['name' => 'YOUR_FIRST_NAME'],
2929
'recipients' => [

examples/transmissions/create_transmission_with_cc_and_bcc.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
'name' => 'SparkPost Team',
2222
'email' => '[email protected]',
2323
],
24-
'subject' => 'First Mailing From PHP',
25-
'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent your very first mailing!</p></body></html>',
26-
'text' => 'Congratulations, {{name}}!! You just sent your very first mailing!',
24+
'subject' => 'Mailing With CC and BCC From PHP',
25+
'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent your very first mailing with CC and BCC recipients!</p></body></html>',
26+
'text' => 'Congratulations, {{name}}! You just sent your very first mailing with CC and BCC recipients!',
2727
],
2828
'substitution_data' => ['name' => 'YOUR_FIRST_NAME'],
2929
'recipients' => [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Examples\Transmissions;
4+
5+
require dirname(__FILE__).'/../bootstrap.php';
6+
7+
use SparkPost\SparkPost;
8+
use GuzzleHttp\Client;
9+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
10+
11+
$httpClient = new GuzzleAdapter(new Client());
12+
13+
/*
14+
* configure options in example-options.json
15+
*/
16+
$sparky = new SparkPost($httpClient, $options);
17+
18+
$promise = $sparky->transmissions->post([
19+
'content' => [
20+
'from' => [
21+
'name' => 'SparkPost Team',
22+
'email' => '[email protected]',
23+
],
24+
'subject' => 'Mailing With Recipient List From PHP',
25+
'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent an email to everyone on your recipient list!</p></body></html>',
26+
'text' => 'Congratulations, {{name}}! You just sent an email to everyone on your recipient list!',
27+
],
28+
'substitution_data' => ['name' => 'YOUR_FIRST_NAME'],
29+
'recipients' => ['list_id' => 'RECIPIENT_LIST_ID'],
30+
]);
31+
32+
try {
33+
$response = $promise->wait();
34+
echo $response->getStatusCode()."\n";
35+
print_r($response->getBody())."\n";
36+
} catch (\Exception $e) {
37+
echo $e->getCode()."\n";
38+
echo $e->getMessage()."\n";
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Examples\Transmissions;
4+
5+
require dirname(__FILE__).'/../bootstrap.php';
6+
7+
use SparkPost\SparkPost;
8+
use GuzzleHttp\Client;
9+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
10+
11+
$httpClient = new GuzzleAdapter(new Client());
12+
13+
/*
14+
* configure options in example-options.json
15+
*/
16+
$sparky = new SparkPost($httpClient, $options);
17+
18+
$promise = $sparky->transmissions->post([
19+
'content' => ['template_id' => 'TEMPLATE_ID'],
20+
'substitution_data' => ['name' => 'YOUR_FIRST_NAME'],
21+
'recipients' => [
22+
[
23+
'address' => [
24+
'name' => 'YOUR_NAME',
25+
'email' => 'YOUR_EMAIL',
26+
],
27+
],
28+
],
29+
]);
30+
31+
try {
32+
$response = $promise->wait();
33+
echo $response->getStatusCode()."\n";
34+
print_r($response->getBody())."\n";
35+
} catch (\Exception $e) {
36+
echo $e->getCode()."\n";
37+
echo $e->getMessage()."\n";
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Examples\Transmissions;
4+
5+
require dirname(__FILE__).'/../bootstrap.php';
6+
7+
use SparkPost\SparkPost;
8+
use GuzzleHttp\Client;
9+
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
10+
11+
$httpClient = new GuzzleAdapter(new Client());
12+
13+
/*
14+
* configure options in example-options.json
15+
*/
16+
$sparky = new SparkPost($httpClient, $options);
17+
18+
$promise = $sparky->transmissions->get('TRANSMISSION_ID');
19+
20+
try {
21+
$response = $promise->wait();
22+
echo $response->getStatusCode()."\n";
23+
print_r($response->getBody())."\n";
24+
} catch (\Exception $e) {
25+
echo $e->getCode()."\n";
26+
echo $e->getMessage()."\n";
27+
}

0 commit comments

Comments
 (0)