Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
90388b3
SendGrid V3 API
rajmundtoth0 Mar 10, 2025
f267219
setType method was removed sinve v7 in ELastice.
rajmundtoth0 Mar 15, 2025
e264db6
Merge branch 'main' into sendgrid-v3
rajmundtoth0 Mar 15, 2025
3e5f8fe
Fix PHPStan issues.
rajmundtoth0 Mar 15, 2025
0aca028
Merge branch 'sendgrid-v3' of https://github.com/rajmundtoth0/monolog…
rajmundtoth0 Mar 15, 2025
33f11b0
No native types for constants.
rajmundtoth0 Mar 15, 2025
3f67195
Add return type declaration to setHandler method in BufferHandler
rajmundtoth0 Mar 15, 2025
5245a0e
Fix datetime format in ElasticsearchFormatterTest
rajmundtoth0 Mar 15, 2025
77be5bd
SendGrid V3 API
rajmundtoth0 Mar 10, 2025
06b3eeb
Fix PHPStan issues.
rajmundtoth0 Mar 15, 2025
051050d
No native types for constants.
rajmundtoth0 Mar 15, 2025
b4f6b69
Merge branch 'sendgrid-v3' of https://github.com/rajmundtoth0/monolog…
rajmundtoth0 Mar 17, 2025
17cece4
Refactor SendGridHandler constructor to include apiUser and improve t…
rajmundtoth0 Mar 17, 2025
5c55f06
Use Utils for json encode.
rajmundtoth0 Mar 17, 2025
f5da20f
Update SendGridHandler constructor to allow array|string type for rec…
rajmundtoth0 Mar 17, 2025
f01543f
Update src/Monolog/Handler/SendGridHandler.php
rajmundtoth0 Mar 17, 2025
f7d05b1
Update src/Monolog/Handler/SendGridHandler.php
rajmundtoth0 Mar 17, 2025
c253d55
Revert "setType method was removed sinve v7 in ELastice."
rajmundtoth0 Mar 18, 2025
1222ae9
Add phpstan-ignore comment for setType method in ElasticaFormatter
rajmundtoth0 Mar 18, 2025
1318513
Update some docs
Seldaek Mar 20, 2025
f3de95d
Some more tweaks
Seldaek Mar 20, 2025
fdb4372
Fix deprecation warning
Seldaek Mar 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/Monolog/Formatter/ElasticaFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ protected function getDocument(array $record): Document
{
$document = new Document();
$document->setData($record);
/** @phpstan-ignore function.impossibleType */
if (method_exists($document, 'setType')) {
$document->setType($this->type);
}
$document->setIndex($this->index);

return $document;
Expand Down
78 changes: 35 additions & 43 deletions src/Monolog/Handler/SendGridHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Monolog\Handler;

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
Expand All @@ -20,81 +21,72 @@
*/
class SendGridHandler extends MailHandler
{
private const CONTENT_TYPE = 'Content-Type: application/json';

/**
* The SendGrid API User
*/
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
* @var string[]
*/
protected array $to;

/**
* The subject of the email
*/
protected string $subject;

/**
* @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
*
* @param list<string>|string $to
* @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 $apiUser,
protected readonly string $apiKey,
private readonly string $from,
array|string $to,
protected readonly string $subject,
int|string|Level $level = Level::Error,
bool $bubble = true,
/** @var non-empty-string */
private readonly string $sendGridApiUrl = 'https://api.sendgrid.com/v3/mail/send',
)
{
if (!\extension_loaded('curl')) {
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->subject = $subject;
$this->apiUser = $apiUser;
parent::__construct($level, $bubble);
}

/**
* @inheritDoc
*/
protected function send(string $content, array $records): void
{
$message = [];
$message['api_user'] = $this->apiUser;
$message['api_key'] = $this->apiKey;
$message['from'] = $this->from;
$body = [];
$body['personalizations'] = [];
$body['from']['email'] = $this->from;
foreach ($this->to as $recipient) {
$message['to[]'] = $recipient;
$body['personalizations'][]['to'][]['email'] = $recipient;
}
$message['subject'] = $this->subject;
$message['date'] = date('r');
$body['subject'] = $this->subject;

if ($this->isHtmlBody($content)) {
$message['html'] = $content;
$body['content'][] = [
'type' => 'text/html',
'value' => $content,
];
} else {
$message['text'] = $content;
$body['content'][] = [
'type' => 'text/plain',
'value' => $content,
];
}

$authorization = "Authorization: Bearer {$this->apiKey}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sendgrid.com/api/mail.send.json');
curl_setopt($ch, CURLOPT_HTTPHEADER, [self::CONTENT_TYPE , $authorization]);
curl_setopt($ch, CURLOPT_URL, $this->sendGridApiUrl);
curl_setopt($ch, CURLOPT_POST, 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);
}
}