forked from utopia-php/messaging
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendgrid.php
More file actions
56 lines (50 loc) · 1.46 KB
/
Sendgrid.php
File metadata and controls
56 lines (50 loc) · 1.46 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
<?php
namespace Utopia\Messaging\Adapters\Email;
use Utopia\Messaging\Adapters\Email as EmailAdapter;
use Utopia\Messaging\Messages\Email;
class Sendgrid extends EmailAdapter
{
public function __construct(
private string $apiKey,
) {
}
public function getName(): string
{
return 'Sendgrid';
}
public function getMaxMessagesPerRequest(): int
{
return 1000;
}
protected function process(Email $message): string
{
return $this->request(
method: 'POST',
url: 'https://api.sendgrid.com/v3/mail/send',
headers: [
'Authorization: Bearer '.$this->apiKey,
'Content-Type: application/json',
],
body: \json_encode([
'personalizations' => [
[
'to' => \array_map(
fn ($to) => ['email' => $to],
$message->getTo()
),
'subject' => $message->getSubject(),
],
],
'from' => [
'email' => $message->getFrom(),
],
'content' => [
[
'type' => $message->isHtml() ? 'text/html' : 'text/plain',
'value' => $message->getContent(),
],
],
]),
);
}
}