From 1ea7610552023df657bea2f7d6813523f97273f5 Mon Sep 17 00:00:00 2001 From: Adam Nicholson Date: Thu, 25 Sep 2014 23:49:01 +0100 Subject: [PATCH 1/3] Added MandrillHandler for sending log messages to the Mandrill API --- composer.json | 3 +- src/Monolog/Handler/MandrillHandler.php | 64 +++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/Monolog/Handler/MandrillHandler.php diff --git a/composer.json b/composer.json index 62297d3c..e67bb0ba 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,8 @@ "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", "ext-mongo": "Allow sending log messages to a MongoDB server", "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB", - "rollbar/rollbar": "Allow sending log messages to Rollbar" + "rollbar/rollbar": "Allow sending log messages to Rollbar", + "guzzlehttp/guzzle": "Allow sending log messages to Mandrill" }, "autoload": { "psr-4": {"Monolog\\": "src/Monolog"} diff --git a/src/Monolog/Handler/MandrillHandler.php b/src/Monolog/Handler/MandrillHandler.php new file mode 100644 index 00000000..1e994b05 --- /dev/null +++ b/src/Monolog/Handler/MandrillHandler.php @@ -0,0 +1,64 @@ + + * + * 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; +use GuzzleHttp\Client; + +/** + * MandrillHandler uses GuzzleHttp to send the emails to the Mandrill API + * + * @author Adam Nicholson + */ +class MandrillHandler extends MailHandler +{ + protected $client; + protected $message; + + /** + * @param \GuzzleHttp\Client $client The Guzzle client + * @oaram string $apiKey A valid Mandrill API key + * @param callable|\Swift_Message $message An example message for real messages, only the body will be replaced + * @param integer $level The minimum logging level at which this handler will be triggered + * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not + */ + public function __construct(Client $client, $apiKey, $message, $level = Logger::ERROR, $bubble = true) + { + parent::__construct($level, $bubble); + $this->client = $client; + if (!$message instanceof \Swift_Message && is_callable($message)) { + $message = call_user_func($message); + } + if (!$message instanceof \Swift_Message) { + throw new \InvalidArgumentException('You must provide either a Swift_Message instance or a callable returning it'); + } + $this->message = $message; + $this->apiKey = $apiKey; + } + + /** + * {@inheritdoc} + */ + protected function send($content, array $records) + { + $message = clone $this->message; + $message->setBody($content); + + $this->client->post('https://mandrillapp.com/api/1.0/messages/send-raw.json', [ + 'body' => [ + 'key' => $this->apiKey, + 'raw_message' => (string) $message, + 'async' => false, + ], + ]); + } +} \ No newline at end of file From 05936033f7c03532ccaff7ec89f3faa52e7d5742 Mon Sep 17 00:00:00 2001 From: Adam Nicholson Date: Mon, 29 Sep 2014 20:51:07 +0100 Subject: [PATCH 2/3] Removed Guzzle dependency from Mandrill handler. API calls are now done via cURL --- composer.json | 3 +-- src/Monolog/Handler/MandrillHandler.php | 27 ++++++++++++++----------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index e67bb0ba..62297d3c 100644 --- a/composer.json +++ b/composer.json @@ -34,8 +34,7 @@ "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", "ext-mongo": "Allow sending log messages to a MongoDB server", "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB", - "rollbar/rollbar": "Allow sending log messages to Rollbar", - "guzzlehttp/guzzle": "Allow sending log messages to Mandrill" + "rollbar/rollbar": "Allow sending log messages to Rollbar" }, "autoload": { "psr-4": {"Monolog\\": "src/Monolog"} diff --git a/src/Monolog/Handler/MandrillHandler.php b/src/Monolog/Handler/MandrillHandler.php index 1e994b05..cb20b2f5 100644 --- a/src/Monolog/Handler/MandrillHandler.php +++ b/src/Monolog/Handler/MandrillHandler.php @@ -12,10 +12,9 @@ namespace Monolog\Handler; use Monolog\Logger; -use GuzzleHttp\Client; /** - * MandrillHandler uses GuzzleHttp to send the emails to the Mandrill API + * MandrillHandler uses cURL to send the emails to the Mandrill API * * @author Adam Nicholson */ @@ -25,16 +24,15 @@ class MandrillHandler extends MailHandler protected $message; /** - * @param \GuzzleHttp\Client $client The Guzzle client * @oaram string $apiKey A valid Mandrill API key * @param callable|\Swift_Message $message An example message for real messages, only the body will be replaced * @param integer $level The minimum logging level at which this handler will be triggered * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not */ - public function __construct(Client $client, $apiKey, $message, $level = Logger::ERROR, $bubble = true) + public function __construct($apiKey, $message, $level = Logger::ERROR, $bubble = true) { parent::__construct($level, $bubble); - $this->client = $client; + if (!$message instanceof \Swift_Message && is_callable($message)) { $message = call_user_func($message); } @@ -53,12 +51,17 @@ class MandrillHandler extends MailHandler $message = clone $this->message; $message->setBody($content); - $this->client->post('https://mandrillapp.com/api/1.0/messages/send-raw.json', [ - 'body' => [ - 'key' => $this->apiKey, - 'raw_message' => (string) $message, - 'async' => false, - ], - ]); + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, 'https://mandrillapp.com/api/1.0/messages/send-raw.json'); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array( + 'key' => $this->apiKey, + 'raw_message' => (string) $message, + 'async' => false, + ))); + + curl_exec($ch); + curl_close ($ch); } } \ No newline at end of file From e99d6b1bce1e8676ee3b4b0e267d029ce24064f5 Mon Sep 17 00:00:00 2001 From: Adam Nicholson Date: Mon, 29 Sep 2014 21:02:43 +0100 Subject: [PATCH 3/3] Fixed a docblock typo in Mandrill handler --- src/Monolog/Handler/MandrillHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Handler/MandrillHandler.php b/src/Monolog/Handler/MandrillHandler.php index cb20b2f5..0406e022 100644 --- a/src/Monolog/Handler/MandrillHandler.php +++ b/src/Monolog/Handler/MandrillHandler.php @@ -24,7 +24,7 @@ class MandrillHandler extends MailHandler protected $message; /** - * @oaram string $apiKey A valid Mandrill API key + * @param string $apiKey A valid Mandrill API key * @param callable|\Swift_Message $message An example message for real messages, only the body will be replaced * @param integer $level The minimum logging level at which this handler will be triggered * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not