SendGridHandler.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Logger;
  12. /**
  13. * SendGridrHandler uses the SendGrid API v2 function to send Log emails, more information in https://sendgrid.com/docs/API_Reference/Web_API/mail.html
  14. *
  15. * @author Ricardo Fontanelli <ricardo.fontanelli@hotmail.com>
  16. */
  17. class SendGridHandler extends MailHandler
  18. {
  19. /**
  20. * The SendGrid API User
  21. * @var string
  22. */
  23. protected $apiUser;
  24. /**
  25. * The SendGrid API Key
  26. * @var string
  27. */
  28. protected $apiKey;
  29. /**
  30. * The email addresses to which the message will be sent
  31. * @var string
  32. */
  33. protected $from;
  34. /**
  35. * The email addresses to which the message will be sent
  36. * @var array
  37. */
  38. protected $to;
  39. /**
  40. * The subject of the email
  41. * @var string
  42. */
  43. protected $subject;
  44. /**
  45. * @param string $apiUser The SendGrid API User
  46. * @param string $apiKey The SendGrid API Key
  47. * @param string $from The sender of the email
  48. * @param string|array $to The recipients of the email
  49. * @param string $subject The subject of the mail
  50. * @param int $level The minimum logging level at which this handler will be triggered
  51. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  52. */
  53. public function __construct(string $apiUser, string $apiKey, string $from, $to, string $subject, int $level = Logger::ERROR, bool $bubble = true)
  54. {
  55. parent::__construct($level, $bubble);
  56. $this->apiUser = $apiUser;
  57. $this->apiKey = $apiKey;
  58. $this->from = $from;
  59. $this->to = (array) $to;
  60. $this->subject = $subject;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function send($content, array $records)
  66. {
  67. $message = array();
  68. $message['api_user'] = $this->apiUser;
  69. $message['api_key'] = $this->apiKey;
  70. $message['from'] = $this->from;
  71. foreach ($this->to as $recipient) {
  72. $message['to[]'] = $recipient;
  73. }
  74. $message['subject'] = $this->subject;
  75. $message['text'] = $content;
  76. $message['date'] = date('r');
  77. $ch = curl_init();
  78. curl_setopt($ch, CURLOPT_URL, 'https://api.sendgrid.com/api/mail.send.json');
  79. curl_setopt($ch, CURLOPT_POST, 1);
  80. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  81. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($message));
  82. Curl\Util::execute($ch, 2);
  83. }
  84. }