SendGridHandler.php 2.9 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\Level;
  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. */
  22. protected string $apiUser;
  23. /**
  24. * The SendGrid API Key
  25. */
  26. protected string $apiKey;
  27. /**
  28. * The email addresses to which the message will be sent
  29. */
  30. protected string $from;
  31. /**
  32. * The email addresses to which the message will be sent
  33. * @var string[]
  34. */
  35. protected array $to;
  36. /**
  37. * The subject of the email
  38. */
  39. protected string $subject;
  40. /**
  41. * @param string $apiUser The SendGrid API User
  42. * @param string $apiKey The SendGrid API Key
  43. * @param string $from The sender of the email
  44. * @param string|string[] $to The recipients of the email
  45. * @param string $subject The subject of the mail
  46. *
  47. * @throws MissingExtensionException If the curl extension is missing
  48. */
  49. public function __construct(string $apiUser, string $apiKey, string $from, string|array $to, string $subject, int|string|Level $level = Level::Error, bool $bubble = true)
  50. {
  51. if (!\extension_loaded('curl')) {
  52. throw new MissingExtensionException('The curl extension is needed to use the SendGridHandler');
  53. }
  54. parent::__construct($level, $bubble);
  55. $this->apiUser = $apiUser;
  56. $this->apiKey = $apiKey;
  57. $this->from = $from;
  58. $this->to = (array) $to;
  59. $this->subject = $subject;
  60. }
  61. /**
  62. * @inheritDoc
  63. */
  64. protected function send(string $content, array $records): void
  65. {
  66. $message = [];
  67. $message['api_user'] = $this->apiUser;
  68. $message['api_key'] = $this->apiKey;
  69. $message['from'] = $this->from;
  70. foreach ($this->to as $recipient) {
  71. $message['to[]'] = $recipient;
  72. }
  73. $message['subject'] = $this->subject;
  74. $message['date'] = date('r');
  75. if ($this->isHtmlBody($content)) {
  76. $message['html'] = $content;
  77. } else {
  78. $message['text'] = $content;
  79. }
  80. $ch = curl_init();
  81. curl_setopt($ch, CURLOPT_URL, 'https://api.sendgrid.com/api/mail.send.json');
  82. curl_setopt($ch, CURLOPT_POST, true);
  83. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  84. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($message));
  85. Curl\Util::execute($ch, 2);
  86. }
  87. }