email.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. 'page' => $this->grav['page']
  81. );
  82. // Copy files now, we need those.
  83. // TODO: needs an update
  84. $form->legacyUploads();
  85. $form->copyFiles();
  86. $this->grav->fireEvent('onEmailSend', new Event(['params' => &$params, 'vars' => &$vars]));
  87. if ($this->isAssocArray($params)) {
  88. $this->sendFormEmail($form, $params, $vars);
  89. } else {
  90. foreach ($params as $email) {
  91. $this->sendFormEmail($form, $email, $vars);
  92. }
  93. }
  94. break;
  95. }
  96. }
  97. /**
  98. * Add index job to Grav Scheduler
  99. * Requires Grav 1.6.0 - Scheduler
  100. */
  101. public function onSchedulerInitialized(Event $e)
  102. {
  103. if ($this->config->get('plugins.email.queue.enabled')) {
  104. /** @var Scheduler $scheduler */
  105. $scheduler = $e['scheduler'];
  106. $at = $this->config->get('plugins.email.queue.flush_frequency');
  107. $logs = 'logs/email-queue.out';
  108. $job = $scheduler->addFunction('Grav\Plugin\Email\Email::flushQueue', [], 'email-flushqueue');
  109. $job->at($at);
  110. $job->output($logs);
  111. $job->backlink('/plugins/email');
  112. }
  113. }
  114. protected function sendFormEmail($form, $params, $vars)
  115. {
  116. // Build message
  117. $message = $this->email->buildMessage($params, $vars);
  118. if (isset($params['attachments'])) {
  119. $filesToAttach = (array)$params['attachments'];
  120. if ($filesToAttach) foreach ($filesToAttach as $fileToAttach) {
  121. $filesValues = $form->value($fileToAttach);
  122. if ($filesValues) foreach($filesValues as $fileValues) {
  123. if (isset($fileValues['file'])) {
  124. $filename = $fileValues['file'];
  125. } else {
  126. $filename = ROOT_DIR . $fileValues['path'];
  127. }
  128. try {
  129. $message->attach(\Swift_Attachment::fromPath($filename));
  130. } catch (\Exception $e) {
  131. // Log any issues
  132. $this->grav['log']->error($e->getMessage());
  133. }
  134. }
  135. }
  136. }
  137. //fire event to apply optional signers
  138. $this->grav->fireEvent('onEmailMessage', new Event(['message' => $message, 'params' => $params, 'form' => $form]));
  139. // Send e-mail
  140. $this->email->send($message);
  141. //fire event after eMail was sent
  142. $this->grav->fireEvent('onEmailSent', new Event(['message' => $message, 'params' => $params, 'form' => $form]));
  143. }
  144. protected function isAssocArray(array $arr)
  145. {
  146. if (array() === $arr) return false;
  147. $keys = array_keys($arr);
  148. $index_keys = range(0, count($arr) - 1);
  149. return $keys !== $index_keys;
  150. }
  151. }