From a9e990ac5e307fe93536080192812846c4781a55 Mon Sep 17 00:00:00 2001 From: Ricardo Fontanelli Date: Mon, 23 May 2016 22:50:26 -0300 Subject: [PATCH 1/4] ADD support to SendGrid Handler ADD support to SendGrid Handler --- src/Monolog/Handler/SendGridHandler.php | 95 +++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/Monolog/Handler/SendGridHandler.php diff --git a/src/Monolog/Handler/SendGridHandler.php b/src/Monolog/Handler/SendGridHandler.php new file mode 100644 index 00000000..5e6bdd75 --- /dev/null +++ b/src/Monolog/Handler/SendGridHandler.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Handler; + +use Monolog\Logger; + +/** + * SendGridrHandler uses the SendGrid API v2 function to send Log emails, more information in https://sendgrid.com/docs/API_Reference/Web_API/mail.html + * + * @author Ricardo Fontanelli + */ +class SendGridrHandler extends MailHandler +{ + /** + * The SendGrid API User + * @var string + */ + protected $apiUser; + + /** + * The SendGrid API Key + * @var string + */ + protected $apiKey; + + /** + * The email addresses to which the message will be sent + * @var string + */ + protected $from; + + /** + * The email addresses to which the message will be sent + * @var array + */ + protected $to; + + /** + * The subject of the email + * @var string + */ + protected $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|array $to The recipients of the email + * @param string $subject The subject of the mail + * @param int $level The minimum logging level at which this handler will be triggered + * @param bool $bubble Whether the messages that are handled can bubble up the stack or not + */ + public function __construct($apiUser, $apiKey, $from, $to, $subject, $level = Logger::ERROR, $bubble = true) + { + parent::__construct($level, $bubble); + $this->apiUser = $apiUser; + $this->apiKey = $apiKey; + $this->from = $from; + $this->to = is_array($to) ? $to : array($to); + $this->subject = $subject; + } + + /** + * {@inheritdoc} + */ + protected function send($content, array $records) + { + $message = array(); + $message['api_user'] = $this->apiUser, + $message['api_key'] = $this->apiKey; + $message['from'] = $this->from; + foreach ($this->to AS $v) { + $message['to[]'] = $v; + } + $message['subject'] = $this->subject; + $message['text'] = $content; + $message['date'] = date('r'); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, 'https://api.sendgrid.com/api/mail.send.json'); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($message)); + Curl\Util::execute($ch, 2); + } +} From 446613af477c0891473e697f0239b70377d70bc9 Mon Sep 17 00:00:00 2001 From: Ricardo Fontanelli Date: Mon, 23 May 2016 22:52:24 -0300 Subject: [PATCH 2/4] CS fixes CS fixes --- src/Monolog/Handler/SendGridHandler.php | 26 ++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Monolog/Handler/SendGridHandler.php b/src/Monolog/Handler/SendGridHandler.php index 5e6bdd75..bc3097c0 100644 --- a/src/Monolog/Handler/SendGridHandler.php +++ b/src/Monolog/Handler/SendGridHandler.php @@ -26,13 +26,13 @@ class SendGridrHandler extends MailHandler */ protected $apiUser; - /** + /** * The SendGrid API Key * @var string */ protected $apiKey; - /** + /** * The email addresses to which the message will be sent * @var string */ @@ -53,7 +53,7 @@ class SendGridrHandler extends MailHandler /** * @param string $apiUser The SendGrid API User * @param string $apiKey The SendGrid API Key - * @param string $from The sender of the email + * @param string $from The sender of the email * @param string|array $to The recipients of the email * @param string $subject The subject of the mail * @param int $level The minimum logging level at which this handler will be triggered @@ -74,16 +74,16 @@ class SendGridrHandler extends MailHandler */ protected function send($content, array $records) { - $message = array(); - $message['api_user'] = $this->apiUser, - $message['api_key'] = $this->apiKey; - $message['from'] = $this->from; - foreach ($this->to AS $v) { - $message['to[]'] = $v; - } - $message['subject'] = $this->subject; - $message['text'] = $content; - $message['date'] = date('r'); + $message = array(); + $message['api_user'] = $this->apiUser, + $message['api_key'] = $this->apiKey; + $message['from'] = $this->from; + foreach ($this->to AS $v) { + $message['to[]'] = $v; + } + $message['subject'] = $this->subject; + $message['text'] = $content; + $message['date'] = date('r'); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://api.sendgrid.com/api/mail.send.json'); From f1e3bdce76e4a2deabd7b5d23f0ecc348d525000 Mon Sep 17 00:00:00 2001 From: Ricardo Fontanelli Date: Mon, 23 May 2016 22:57:51 -0300 Subject: [PATCH 3/4] FIX typing errors FIX typing errors --- src/Monolog/Handler/SendGridHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Handler/SendGridHandler.php b/src/Monolog/Handler/SendGridHandler.php index bc3097c0..6cdaf852 100644 --- a/src/Monolog/Handler/SendGridHandler.php +++ b/src/Monolog/Handler/SendGridHandler.php @@ -75,7 +75,7 @@ class SendGridrHandler extends MailHandler protected function send($content, array $records) { $message = array(); - $message['api_user'] = $this->apiUser, + $message['api_user'] = $this->apiUser; $message['api_key'] = $this->apiKey; $message['from'] = $this->from; foreach ($this->to AS $v) { From 3c7920b446cc0ffb353917b61d8202ece163355d Mon Sep 17 00:00:00 2001 From: Ricardo Fontanelli Date: Tue, 24 May 2016 00:57:48 -0300 Subject: [PATCH 4/4] FIX the class name FIX the class name (sorry, I'm coding direct in GitHub) --- src/Monolog/Handler/SendGridHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Handler/SendGridHandler.php b/src/Monolog/Handler/SendGridHandler.php index 6cdaf852..01e70a36 100644 --- a/src/Monolog/Handler/SendGridHandler.php +++ b/src/Monolog/Handler/SendGridHandler.php @@ -18,7 +18,7 @@ use Monolog\Logger; * * @author Ricardo Fontanelli */ -class SendGridrHandler extends MailHandler +class SendGridHandler extends MailHandler { /** * The SendGrid API User