Utils.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Grav\Plugin\Email;
  3. use Grav\Common\Grav;
  4. use Grav\Common\Twig\Twig;
  5. use Grav\Common\Utils as GravUtils;
  6. /**
  7. * Class Utils
  8. * @package Grav\Plugin\Email
  9. */
  10. class Utils
  11. {
  12. /**
  13. * Quick utility method to send an HTML email.
  14. *
  15. * @param array<int,mixed> $params
  16. *
  17. * @return bool True if the action was performed.
  18. */
  19. public static function sendEmail(...$params)
  20. {
  21. if (is_array($params[0])) {
  22. $params = array_shift($params);
  23. } else {
  24. $keys = ['subject', 'body', 'to', 'from', 'content_type'];
  25. $params = GravUtils::arrayCombine($keys, $params);
  26. }
  27. //Initialize twig if not yet initialized
  28. /** @var Twig $twig */
  29. $twig = Grav::instance()['twig']->init();
  30. /** @var Email $email */
  31. $email = Grav::instance()['Email'];
  32. if (empty($params['to']) || empty($params['subject']) || empty($params['body'])) {
  33. return false;
  34. }
  35. $params['body'] = $twig->processTemplate('email/base.html.twig', ['content' => $params['body']]);
  36. $message = $email->buildMessage($params);
  37. return $email->send($message);
  38. }
  39. }