email.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Plugin;
  4. use Grav\Plugin\Email\Email;
  5. use RocketTheme\Toolbox\Event\Event;
  6. class EmailPlugin extends Plugin
  7. {
  8. /**
  9. * @var Email
  10. */
  11. protected $email;
  12. /**
  13. * @return array
  14. */
  15. public static function getSubscribedEvents()
  16. {
  17. return [
  18. 'onPluginsInitialized' => ['onPluginsInitialized', 0],
  19. 'onFormProcessed' => ['onFormProcessed', 0],
  20. 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
  21. 'onSchedulerInitialized' => ['onSchedulerInitialized', 0],
  22. ];
  23. }
  24. /**
  25. * Initialize emailing.
  26. */
  27. public function onPluginsInitialized()
  28. {
  29. require_once __DIR__ . '/vendor/autoload.php';
  30. $this->email = new Email();
  31. if ($this->email->enabled()) {
  32. $this->grav['Email'] = $this->email;
  33. }
  34. }
  35. /**
  36. * Add twig paths to plugin templates.
  37. */
  38. public function onTwigTemplatePaths()
  39. {
  40. $twig = $this->grav['twig'];
  41. $twig->twig_paths[] = __DIR__ . '/templates';
  42. }
  43. /**
  44. * Send email when processing the form data.
  45. *
  46. * @param Event $event
  47. */
  48. public function onFormProcessed(Event $event)
  49. {
  50. $form = $event['form'];
  51. $action = $event['action'];
  52. $params = $event['params'];
  53. if (!$this->email->enabled()) {
  54. return;
  55. }
  56. switch ($action) {
  57. case 'email':
  58. // Prepare Twig variables
  59. $vars = array(
  60. 'form' => $form
  61. );
  62. // Copy files now, we need those.
  63. // TODO: needs an update
  64. $form->legacyUploads();
  65. $form->copyFiles();
  66. $this->grav->fireEvent('onEmailSend', new Event(['params' => &$params, 'vars' => &$vars]));
  67. if ($this->isAssocArray($params)) {
  68. $this->sendFormEmail($form, $params, $vars);
  69. } else {
  70. foreach ($params as $email) {
  71. $this->sendFormEmail($form, $email, $vars);
  72. }
  73. }
  74. break;
  75. }
  76. }
  77. /**
  78. * Add index job to Grav Scheduler
  79. * Requires Grav 1.6.0 - Scheduler
  80. */
  81. public function onSchedulerInitialized(Event $e)
  82. {
  83. if ($this->config->get('plugins.email.queue.enabled')) {
  84. /** @var Scheduler $scheduler */
  85. $scheduler = $e['scheduler'];
  86. $at = $this->config->get('plugins.email.queue.flush_frequency');
  87. $logs = 'logs/email-queue.out';
  88. $job = $scheduler->addFunction('Grav\Plugin\Email\Email::flushQueue', [], 'email-flushqueue');
  89. $job->at($at);
  90. $job->output($logs);
  91. $job->backlink('/plugins/email');
  92. }
  93. }
  94. protected function sendFormEmail($form, $params, $vars)
  95. {
  96. // Build message
  97. $message = $this->email->buildMessage($params, $vars);
  98. if (isset($params['attachments'])) {
  99. $filesToAttach = (array)$params['attachments'];
  100. if ($filesToAttach) foreach ($filesToAttach as $fileToAttach) {
  101. $filesValues = $form->value($fileToAttach);
  102. if ($filesValues) foreach($filesValues as $fileValues) {
  103. if (isset($fileValues['file'])) {
  104. $filename = $fileValues['file'];
  105. } else {
  106. $filename = ROOT_DIR . $fileValues['path'];
  107. }
  108. try {
  109. $message->attach(\Swift_Attachment::fromPath($filename));
  110. } catch (\Exception $e) {
  111. // Log any issues
  112. $this->grav['log']->error($e->getMessage());
  113. }
  114. }
  115. }
  116. }
  117. // Send e-mail
  118. $this->email->send($message);
  119. }
  120. protected function isAssocArray(array $arr)
  121. {
  122. if (array() === $arr) return false;
  123. $keys = array_keys($arr);
  124. $index_keys = range(0, count($arr) - 1);
  125. return $keys !== $index_keys;
  126. }
  127. }