|
12 | 12 | namespace Monolog\Handler; |
13 | 13 |
|
14 | 14 | use Monolog\Level; |
| 15 | +use Monolog\Utils; |
15 | 16 |
|
16 | 17 | /** |
17 | | - * SendGridrHandler uses the SendGrid API v2 function to send Log emails, more information in https://sendgrid.com/docs/API_Reference/Web_API/mail.html |
| 18 | + * 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 |
18 | 19 | * |
19 | 20 | * @author Ricardo Fontanelli <[email protected]> |
20 | 21 | */ |
21 | 22 | class SendGridHandler extends MailHandler |
22 | 23 | { |
23 | 24 | /** |
24 | 25 | * The SendGrid API User |
| 26 | + * @deprecated this is not used anymore as of SendGrid API v3 |
25 | 27 | */ |
26 | 28 | protected string $apiUser; |
27 | | - |
28 | | - /** |
29 | | - * The SendGrid API Key |
30 | | - */ |
31 | | - protected string $apiKey; |
32 | | - |
33 | | - /** |
34 | | - * The email addresses to which the message will be sent |
35 | | - */ |
36 | | - protected string $from; |
37 | | - |
38 | 29 | /** |
39 | 30 | * The email addresses to which the message will be sent |
40 | 31 | * @var string[] |
41 | 32 | */ |
42 | 33 | protected array $to; |
43 | 34 |
|
44 | 35 | /** |
45 | | - * The subject of the email |
46 | | - */ |
47 | | - protected string $subject; |
48 | | - |
49 | | - /** |
50 | | - * @param string $apiUser The SendGrid API User |
51 | | - * @param string $apiKey The SendGrid API Key |
52 | | - * @param string $from The sender of the email |
53 | | - * @param string|string[] $to The recipients of the email |
54 | | - * @param string $subject The subject of the mail |
55 | | - * |
| 36 | + * @param string|null $apiUser Unused user as of SendGrid API v3, you can pass null or any string |
| 37 | + * @param list<string>|string $to |
| 38 | + * @param non-empty-string $apiHost Allows you to use another endpoint (e.g. api.eu.sendgrid.com) |
56 | 39 | * @throws MissingExtensionException If the curl extension is missing |
57 | 40 | */ |
58 | | - public function __construct(string $apiUser, string $apiKey, string $from, string|array $to, string $subject, int|string|Level $level = Level::Error, bool $bubble = true) |
59 | | - { |
| 41 | + public function __construct( |
| 42 | + string|null $apiUser, |
| 43 | + protected string $apiKey, |
| 44 | + protected string $from, |
| 45 | + array|string $to, |
| 46 | + protected string $subject, |
| 47 | + int|string|Level $level = Level::Error, |
| 48 | + bool $bubble = true, |
| 49 | + /** @var non-empty-string */ |
| 50 | + private readonly string $apiHost = 'api.sendgrid.com', |
| 51 | + ) { |
60 | 52 | if (!\extension_loaded('curl')) { |
61 | 53 | throw new MissingExtensionException('The curl extension is needed to use the SendGridHandler'); |
62 | 54 | } |
63 | 55 |
|
64 | | - parent::__construct($level, $bubble); |
65 | | - $this->apiUser = $apiUser; |
66 | | - $this->apiKey = $apiKey; |
67 | | - $this->from = $from; |
68 | 56 | $this->to = (array) $to; |
69 | | - $this->subject = $subject; |
| 57 | + // @phpstan-ignore property.deprecated |
| 58 | + $this->apiUser = $apiUser ?? ''; |
| 59 | + parent::__construct($level, $bubble); |
70 | 60 | } |
71 | 61 |
|
72 | | - /** |
73 | | - * @inheritDoc |
74 | | - */ |
75 | 62 | protected function send(string $content, array $records): void |
76 | 63 | { |
77 | | - $message = []; |
78 | | - $message['api_user'] = $this->apiUser; |
79 | | - $message['api_key'] = $this->apiKey; |
80 | | - $message['from'] = $this->from; |
| 64 | + $body = []; |
| 65 | + $body['personalizations'] = []; |
| 66 | + $body['from']['email'] = $this->from; |
81 | 67 | foreach ($this->to as $recipient) { |
82 | | - $message['to[]'] = $recipient; |
| 68 | + $body['personalizations'][]['to'][]['email'] = $recipient; |
83 | 69 | } |
84 | | - $message['subject'] = $this->subject; |
85 | | - $message['date'] = date('r'); |
| 70 | + $body['subject'] = $this->subject; |
86 | 71 |
|
87 | 72 | if ($this->isHtmlBody($content)) { |
88 | | - $message['html'] = $content; |
| 73 | + $body['content'][] = [ |
| 74 | + 'type' => 'text/html', |
| 75 | + 'value' => $content, |
| 76 | + ]; |
89 | 77 | } else { |
90 | | - $message['text'] = $content; |
| 78 | + $body['content'][] = [ |
| 79 | + 'type' => 'text/plain', |
| 80 | + 'value' => $content, |
| 81 | + ]; |
91 | 82 | } |
92 | | - |
93 | 83 | $ch = curl_init(); |
94 | | - curl_setopt($ch, CURLOPT_URL, 'https://api.sendgrid.com/api/mail.send.json'); |
| 84 | + curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
| 85 | + 'Content-Type: application/json', |
| 86 | + 'Authorization: Bearer '.$this->apiKey, |
| 87 | + ]); |
| 88 | + curl_setopt($ch, CURLOPT_URL, 'https://'.$this->apiHost.'/v3/mail/send'); |
95 | 89 | curl_setopt($ch, CURLOPT_POST, true); |
96 | 90 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
97 | | - curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($message)); |
| 91 | + curl_setopt($ch, CURLOPT_POSTFIELDS, Utils::jsonEncode($body)); |
| 92 | + |
98 | 93 | Curl\Util::execute($ch, 2); |
99 | 94 | } |
100 | 95 | } |
0 commit comments