email.php 4.8 KB

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