SendGridHandler.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php declare(strict_types=1);
  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 = [];
  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['date'] = date('r');
  76. if ($this->isHtmlBody($content)) {
  77. $message['html'] = $content;
  78. } else {
  79. $message['text'] = $content;
  80. }
  81. $ch = curl_init();
  82. curl_setopt($ch, CURLOPT_URL, 'https://api.sendgrid.com/api/mail.send.json');
  83. curl_setopt($ch, CURLOPT_POST, 1);
  84. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  85. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($message));
  86. Curl\Util::execute($ch, 2);
  87. }
  88. }