Utils.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace Grav\Plugin\Email;
  3. use Grav\Common\Grav;
  4. /**
  5. * Class Utils
  6. * @package Grav\Plugin\Email
  7. */
  8. class Utils
  9. {
  10. /**
  11. * Quick utility method to send an HTML email.
  12. *
  13. * @param $subject
  14. * @param string $content
  15. * @param string $to
  16. * @param null $from
  17. * @param string $mimetype
  18. *
  19. * @return bool True if the action was performed.
  20. */
  21. public static function sendEmail($subject, $content, $to, $from = null, $mimetype = 'text/html')
  22. {
  23. $grav = Grav::instance();
  24. if (!$from) {
  25. $from = $grav['config']->get('plugins.email.from');
  26. }
  27. if (!isset($grav['Email']) || empty($from)) {
  28. throw new \RuntimeException($grav['language']->translate('PLUGIN_EMAIL.PLEASE_CONFIGURE_A_FROM_ADDRESS'));
  29. }
  30. if (empty($to) || empty($subject) || empty($content)) {
  31. return false;
  32. }
  33. //Initialize twig if not yet initialized
  34. $grav['twig']->init();
  35. $body = $grav['twig']->processTemplate('email/base.html.twig', ['content' => $content]);
  36. $message = $grav['Email']->message($subject, $body, $mimetype)
  37. ->setFrom($from)
  38. ->setTo($to);
  39. $sent = $grav['Email']->send($message);
  40. if ($sent < 1) {
  41. return false;
  42. } else {
  43. return true;
  44. }
  45. }
  46. }