Skip to content

Commit 2b8777d

Browse files
SendGrid V3 API (#1952)
Fixed SendGridHandler to use the new V3 API as the V2 one has been sunset. This requires configuring a new API key. Co-authored-by: Jordi Boggiano <[email protected]>
1 parent 5d0da0a commit 2b8777d

File tree

1 file changed

+40
-45
lines changed

1 file changed

+40
-45
lines changed

src/Monolog/Handler/SendGridHandler.php

+40-45
Original file line numberDiff line numberDiff line change
@@ -12,89 +12,84 @@
1212
namespace Monolog\Handler;
1313

1414
use Monolog\Level;
15+
use Monolog\Utils;
1516

1617
/**
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
1819
*
1920
* @author Ricardo Fontanelli <[email protected]>
2021
*/
2122
class SendGridHandler extends MailHandler
2223
{
2324
/**
2425
* The SendGrid API User
26+
* @deprecated this is not used anymore as of SendGrid API v3
2527
*/
2628
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-
3829
/**
3930
* The email addresses to which the message will be sent
4031
* @var string[]
4132
*/
4233
protected array $to;
4334

4435
/**
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)
5639
* @throws MissingExtensionException If the curl extension is missing
5740
*/
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+
) {
6052
if (!\extension_loaded('curl')) {
6153
throw new MissingExtensionException('The curl extension is needed to use the SendGridHandler');
6254
}
6355

64-
parent::__construct($level, $bubble);
65-
$this->apiUser = $apiUser;
66-
$this->apiKey = $apiKey;
67-
$this->from = $from;
6856
$this->to = (array) $to;
69-
$this->subject = $subject;
57+
// @phpstan-ignore property.deprecated
58+
$this->apiUser = $apiUser ?? '';
59+
parent::__construct($level, $bubble);
7060
}
7161

72-
/**
73-
* @inheritDoc
74-
*/
7562
protected function send(string $content, array $records): void
7663
{
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;
8167
foreach ($this->to as $recipient) {
82-
$message['to[]'] = $recipient;
68+
$body['personalizations'][]['to'][]['email'] = $recipient;
8369
}
84-
$message['subject'] = $this->subject;
85-
$message['date'] = date('r');
70+
$body['subject'] = $this->subject;
8671

8772
if ($this->isHtmlBody($content)) {
88-
$message['html'] = $content;
73+
$body['content'][] = [
74+
'type' => 'text/html',
75+
'value' => $content,
76+
];
8977
} else {
90-
$message['text'] = $content;
78+
$body['content'][] = [
79+
'type' => 'text/plain',
80+
'value' => $content,
81+
];
9182
}
92-
9383
$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');
9589
curl_setopt($ch, CURLOPT_POST, true);
9690
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+
9893
Curl\Util::execute($ch, 2);
9994
}
10095
}

0 commit comments

Comments
 (0)