-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathSendGridHandler.php
More file actions
95 lines (86 loc) · 3.02 KB
/
SendGridHandler.php
File metadata and controls
95 lines (86 loc) · 3.02 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php declare(strict_types=1);
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Handler;
use Monolog\Level;
use Monolog\Utils;
/**
* SendGridHandler uses the SendGrid API v3 function to send Log emails, more information in https://www.twilio.com/docs/sendgrid/for-developers/sending-email/api-getting-started
*
* @author Ricardo Fontanelli <ricardo.fontanelli@hotmail.com>
*/
class SendGridHandler extends MailHandler
{
/**
* The SendGrid API User
* @deprecated this is not used anymore as of SendGrid API v3
*/
protected string $apiUser;
/**
* The email addresses to which the message will be sent
* @var string[]
*/
protected array $to;
/**
* @param string|null $apiUser Unused user as of SendGrid API v3, you can pass null or any string
* @param list<string>|string $to
* @param non-empty-string $apiHost Allows you to use another endpoint (e.g. api.eu.sendgrid.com)
* @throws MissingExtensionException If the curl extension is missing
*/
public function __construct(
string|null $apiUser,
protected string $apiKey,
protected string $from,
array|string $to,
protected string $subject,
int|string|Level $level = Level::Error,
bool $bubble = true,
/** @var non-empty-string */
private readonly string $apiHost = 'api.sendgrid.com',
) {
if (!\extension_loaded('curl')) {
throw new MissingExtensionException('The curl extension is needed to use the SendGridHandler');
}
$this->to = (array) $to;
// @phpstan-ignore property.deprecated
$this->apiUser = $apiUser ?? '';
parent::__construct($level, $bubble);
}
protected function send(string $content, array $records): void
{
$body = [];
$body['personalizations'] = [];
$body['from']['email'] = $this->from;
foreach ($this->to as $recipient) {
$body['personalizations'][]['to'][]['email'] = $recipient;
}
$body['subject'] = $this->subject;
if ($this->isHtmlBody($content)) {
$body['content'][] = [
'type' => 'text/html',
'value' => $content,
];
} else {
$body['content'][] = [
'type' => 'text/plain',
'value' => $content,
];
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer '.$this->apiKey,
]);
curl_setopt($ch, CURLOPT_URL, 'https://'.$this->apiHost.'/v3/mail/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, Utils::jsonEncode($body));
Curl\Util::execute($ch, 2);
}
}