Skip to content

Commit 87ee8c0

Browse files
committed
added attachment example
1 parent 1abdc8b commit 87ee8c0

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
$filePath = dirname(__FILE__).'/';
19+
$fileName = 'sparkpost.png';
20+
$fileType = mime_content_type($filePath.$fileName);
21+
$fileData = base64_encode(file_get_contents($filePath.$fileName));
22+
23+
$promise = $sparky->transmissions->post([
24+
'content' => [
25+
'from' => [
26+
'name' => 'SparkPost Team',
27+
'email' => '[email protected]',
28+
],
29+
'subject' => 'Mailing With Attachment From PHP',
30+
'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent an email with an attachment!</p></body></html>',
31+
'text' => 'Congratulations, {{name}}! You just sent an email with an attachment',
32+
'attachments' => [
33+
[
34+
'name' => $fileName,
35+
'type' => $fileType,
36+
'data' => $fileData,
37+
],
38+
],
39+
],
40+
'substitution_data' => ['name' => 'YOUR_FIRST_NAME'],
41+
'recipients' => [
42+
[
43+
'address' => [
44+
'name' => 'YOUR_NAME',
45+
'email' => 'YOUR_EMAIL',
46+
],
47+
],
48+
],
49+
]);
50+
51+
try {
52+
$response = $promise->wait();
53+
echo $response->getStatusCode()."\n";
54+
print_r($response->getBody())."\n";
55+
} catch (\Exception $e) {
56+
echo $e->getCode()."\n";
57+
echo $e->getMessage()."\n";
58+
}

examples/transmissions/sparkpost.png

6.79 KB
Loading

0 commit comments

Comments
 (0)