| 148 |
lars |
1 |
<?php declare(strict_types=1);
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the Monolog package.
|
|
|
5 |
*
|
|
|
6 |
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
namespace Monolog\Handler;
|
|
|
13 |
|
|
|
14 |
use Monolog\Logger;
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
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 |
*
|
|
|
19 |
* @author Ricardo Fontanelli <ricardo.fontanelli@hotmail.com>
|
|
|
20 |
*/
|
|
|
21 |
class SendGridHandler extends MailHandler
|
|
|
22 |
{
|
|
|
23 |
/**
|
|
|
24 |
* The SendGrid API User
|
|
|
25 |
* @var string
|
|
|
26 |
*/
|
|
|
27 |
protected $apiUser;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* The SendGrid API Key
|
|
|
31 |
* @var string
|
|
|
32 |
*/
|
|
|
33 |
protected $apiKey;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* The email addresses to which the message will be sent
|
|
|
37 |
* @var string
|
|
|
38 |
*/
|
|
|
39 |
protected $from;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* The email addresses to which the message will be sent
|
|
|
43 |
* @var string[]
|
|
|
44 |
*/
|
|
|
45 |
protected $to;
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* The subject of the email
|
|
|
49 |
* @var string
|
|
|
50 |
*/
|
|
|
51 |
protected $subject;
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* @param string $apiUser The SendGrid API User
|
|
|
55 |
* @param string $apiKey The SendGrid API Key
|
|
|
56 |
* @param string $from The sender of the email
|
|
|
57 |
* @param string|string[] $to The recipients of the email
|
|
|
58 |
* @param string $subject The subject of the mail
|
|
|
59 |
*/
|
|
|
60 |
public function __construct(string $apiUser, string $apiKey, string $from, $to, string $subject, $level = Logger::ERROR, bool $bubble = true)
|
|
|
61 |
{
|
|
|
62 |
if (!extension_loaded('curl')) {
|
|
|
63 |
throw new MissingExtensionException('The curl extension is needed to use the SendGridHandler');
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
parent::__construct($level, $bubble);
|
|
|
67 |
$this->apiUser = $apiUser;
|
|
|
68 |
$this->apiKey = $apiKey;
|
|
|
69 |
$this->from = $from;
|
|
|
70 |
$this->to = (array) $to;
|
|
|
71 |
$this->subject = $subject;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
/**
|
|
|
75 |
* {@inheritDoc}
|
|
|
76 |
*/
|
|
|
77 |
protected function send(string $content, array $records): void
|
|
|
78 |
{
|
|
|
79 |
$message = [];
|
|
|
80 |
$message['api_user'] = $this->apiUser;
|
|
|
81 |
$message['api_key'] = $this->apiKey;
|
|
|
82 |
$message['from'] = $this->from;
|
|
|
83 |
foreach ($this->to as $recipient) {
|
|
|
84 |
$message['to[]'] = $recipient;
|
|
|
85 |
}
|
|
|
86 |
$message['subject'] = $this->subject;
|
|
|
87 |
$message['date'] = date('r');
|
|
|
88 |
|
|
|
89 |
if ($this->isHtmlBody($content)) {
|
|
|
90 |
$message['html'] = $content;
|
|
|
91 |
} else {
|
|
|
92 |
$message['text'] = $content;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
$ch = curl_init();
|
|
|
96 |
curl_setopt($ch, CURLOPT_URL, 'https://api.sendgrid.com/api/mail.send.json');
|
|
|
97 |
curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
98 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
99 |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($message));
|
|
|
100 |
Curl\Util::execute($ch, 2);
|
|
|
101 |
}
|
|
|
102 |
}
|