mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-08 14:16:42 +02:00
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 <j.boggiano@seld.be>
This commit is contained in:
@@ -12,9 +12,10 @@
|
|||||||
namespace Monolog\Handler;
|
namespace Monolog\Handler;
|
||||||
|
|
||||||
use Monolog\Level;
|
use Monolog\Level;
|
||||||
|
use Monolog\Utils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SendGridrHandler uses the SendGrid API v2 function to send Log emails, more information in https://sendgrid.com/docs/API_Reference/Web_API/mail.html
|
* 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>
|
* @author Ricardo Fontanelli <ricardo.fontanelli@hotmail.com>
|
||||||
*/
|
*/
|
||||||
@@ -22,19 +23,9 @@ class SendGridHandler extends MailHandler
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* The SendGrid API User
|
* The SendGrid API User
|
||||||
|
* @deprecated this is not used anymore as of SendGrid API v3
|
||||||
*/
|
*/
|
||||||
protected string $apiUser;
|
protected string $apiUser;
|
||||||
|
|
||||||
/**
|
|
||||||
* The SendGrid API Key
|
|
||||||
*/
|
|
||||||
protected string $apiKey;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The email addresses to which the message will be sent
|
|
||||||
*/
|
|
||||||
protected string $from;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The email addresses to which the message will be sent
|
* The email addresses to which the message will be sent
|
||||||
* @var string[]
|
* @var string[]
|
||||||
@@ -42,59 +33,63 @@ class SendGridHandler extends MailHandler
|
|||||||
protected array $to;
|
protected array $to;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The subject of the email
|
* @param string|null $apiUser Unused user as of SendGrid API v3, you can pass null or any string
|
||||||
*/
|
* @param list<string>|string $to
|
||||||
protected string $subject;
|
* @param non-empty-string $apiHost Allows you to use another endpoint (e.g. api.eu.sendgrid.com)
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $apiUser The SendGrid API User
|
|
||||||
* @param string $apiKey The SendGrid API Key
|
|
||||||
* @param string $from The sender of the email
|
|
||||||
* @param string|string[] $to The recipients of the email
|
|
||||||
* @param string $subject The subject of the mail
|
|
||||||
*
|
|
||||||
* @throws MissingExtensionException If the curl extension is missing
|
* @throws MissingExtensionException If the curl extension is missing
|
||||||
*/
|
*/
|
||||||
public function __construct(string $apiUser, string $apiKey, string $from, string|array $to, string $subject, int|string|Level $level = Level::Error, bool $bubble = true)
|
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')) {
|
if (!\extension_loaded('curl')) {
|
||||||
throw new MissingExtensionException('The curl extension is needed to use the SendGridHandler');
|
throw new MissingExtensionException('The curl extension is needed to use the SendGridHandler');
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::__construct($level, $bubble);
|
|
||||||
$this->apiUser = $apiUser;
|
|
||||||
$this->apiKey = $apiKey;
|
|
||||||
$this->from = $from;
|
|
||||||
$this->to = (array) $to;
|
$this->to = (array) $to;
|
||||||
$this->subject = $subject;
|
// @phpstan-ignore property.deprecated
|
||||||
|
$this->apiUser = $apiUser ?? '';
|
||||||
|
parent::__construct($level, $bubble);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritDoc
|
|
||||||
*/
|
|
||||||
protected function send(string $content, array $records): void
|
protected function send(string $content, array $records): void
|
||||||
{
|
{
|
||||||
$message = [];
|
$body = [];
|
||||||
$message['api_user'] = $this->apiUser;
|
$body['personalizations'] = [];
|
||||||
$message['api_key'] = $this->apiKey;
|
$body['from']['email'] = $this->from;
|
||||||
$message['from'] = $this->from;
|
|
||||||
foreach ($this->to as $recipient) {
|
foreach ($this->to as $recipient) {
|
||||||
$message['to[]'] = $recipient;
|
$body['personalizations'][]['to'][]['email'] = $recipient;
|
||||||
}
|
}
|
||||||
$message['subject'] = $this->subject;
|
$body['subject'] = $this->subject;
|
||||||
$message['date'] = date('r');
|
|
||||||
|
|
||||||
if ($this->isHtmlBody($content)) {
|
if ($this->isHtmlBody($content)) {
|
||||||
$message['html'] = $content;
|
$body['content'][] = [
|
||||||
|
'type' => 'text/html',
|
||||||
|
'value' => $content,
|
||||||
|
];
|
||||||
} else {
|
} else {
|
||||||
$message['text'] = $content;
|
$body['content'][] = [
|
||||||
|
'type' => 'text/plain',
|
||||||
|
'value' => $content,
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
curl_setopt($ch, CURLOPT_URL, 'https://api.sendgrid.com/api/mail.send.json');
|
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_POST, true);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($message));
|
curl_setopt($ch, CURLOPT_POSTFIELDS, Utils::jsonEncode($body));
|
||||||
|
|
||||||
Curl\Util::execute($ch, 2);
|
Curl\Util::execute($ch, 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user