-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathSparkPost.php
More file actions
71 lines (58 loc) · 2.16 KB
/
SparkPost.php
File metadata and controls
71 lines (58 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace Utopia\Messaging\Adapters\Email;
use Utopia\Messaging\Adapters\Email as EmailAdapter;
use Utopia\Messaging\Messages\Email;
class SparkPost extends EmailAdapter{
public function __construct(private string $apiKey,
private bool $isEu = false){
}
public function getName() :string{
return 'SparkPost';
}
public function getMaxMessagesPerRequest(): int{
//TODO: real value for this
return 1000;
}
protected function process(Email $message) : string{
$usDomain = 'api.sparkpost.com';
$euDomain = 'api.eu.sparkpost.com';
$domain = $this->isEu ? $euDomain : $usDomain;
$requestBody = [
'options' =>[
//for testing without domain set sandbox option to true(for more information, visit sparkpost.com)
'sandbox' => false,
],
'recipients' => [
[
'address' => [
'email' => $message->getTo()[0], // Assuming you have only one recipient
],
],
],
'content' => [
// for testing with sandbox template id should be 'my-first-email' , so uncomment below line
// 'template_id' => 'my-first-email',
'from' => [
//sender domain should be registered and verified at sparkpost
'email' => $message->getFrom(),
],
//when testing with sandbox comment out below three lines
'subject' => $message->getSubject(),
'html' => $message->isHtml() ? $message->getContent() : null,
'text' => $message->isHtml() ? null : $message->getContent(),
],
];
$json_body = json_encode($requestBody);
$response = $this->request(
method: 'POST',
url: "https://$domain/api/v1/transmissions",
headers :[
'Authorization: '.$this->apiKey,
'Content-Type: application/json',
],
body: $json_body,
);
return $response;
}
}
?>