email.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace Grav\Plugin;
  3. use Composer\Autoload\ClassLoader;
  4. use Grav\Common\Data\Data;
  5. use Grav\Common\Grav;
  6. use Grav\Common\Plugin;
  7. use Grav\Common\Utils;
  8. use Grav\Plugin\Email\Email;
  9. use RocketTheme\Toolbox\Event\Event;
  10. class EmailPlugin extends Plugin
  11. {
  12. /**
  13. * @var Email
  14. */
  15. protected $email;
  16. /**
  17. * @return array
  18. */
  19. public static function getSubscribedEvents()
  20. {
  21. return [
  22. 'onPluginsInitialized' => ['onPluginsInitialized', 0],
  23. 'onFormProcessed' => ['onFormProcessed', 0],
  24. 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
  25. 'onSchedulerInitialized' => ['onSchedulerInitialized', 0],
  26. 'onAdminSave' => ['onAdminSave', 0],
  27. ];
  28. }
  29. /**
  30. * @return ClassLoader
  31. */
  32. public function autoload(): ClassLoader
  33. {
  34. return require __DIR__ . '/vendor/autoload.php';
  35. }
  36. /**
  37. * Initialize emailing.
  38. */
  39. public function onPluginsInitialized()
  40. {
  41. $this->email = new Email();
  42. if ($this->email::enabled()) {
  43. $this->grav['Email'] = $this->email;
  44. }
  45. }
  46. /**
  47. * Add twig paths to plugin templates.
  48. */
  49. public function onTwigTemplatePaths()
  50. {
  51. $twig = $this->grav['twig'];
  52. $twig->twig_paths[] = __DIR__ . '/templates';
  53. }
  54. /**
  55. * Force compile during save if admin plugin save
  56. *
  57. * @param Event $event
  58. */
  59. public function onAdminSave(Event $event)
  60. {
  61. /** @var Data $obj */
  62. $obj = $event['object'];
  63. if ($obj instanceof Data && $obj->blueprints()->getFilename() === 'email/blueprints') {
  64. $current_pw = $this->grav['config']->get('plugins.email.mailer.smtp.password');
  65. $new_pw = $obj->get('mailer.smtp.password');
  66. if (!empty($current_pw) && empty($new_pw)) {
  67. $obj->set('mailer.smtp.password', $current_pw);
  68. }
  69. }
  70. }
  71. /**
  72. * Send email when processing the form data.
  73. *
  74. * @param Event $event
  75. */
  76. public function onFormProcessed(Event $event)
  77. {
  78. $form = $event['form'];
  79. $action = $event['action'];
  80. $params = $event['params'];
  81. if (!$this->email->enabled()) {
  82. return;
  83. }
  84. switch ($action) {
  85. case 'email':
  86. // Prepare Twig variables
  87. $vars = array(
  88. 'form' => $form,
  89. 'page' => $this->grav['page']
  90. );
  91. // Copy files now, we need those.
  92. // TODO: needs an update
  93. $form->legacyUploads();
  94. $form->copyFiles();
  95. $this->grav->fireEvent('onEmailSend', new Event(['params' => &$params, 'vars' => &$vars]));
  96. if (Utils::isAssoc($params)) {
  97. $this->sendFormEmail($form, $params, $vars);
  98. } else {
  99. foreach ($params as $email) {
  100. $this->sendFormEmail($form, $email, $vars);
  101. }
  102. }
  103. break;
  104. }
  105. }
  106. protected function sendFormEmail($form, $params, $vars)
  107. {
  108. // Build message
  109. $message = $this->email->buildMessage($params, $vars);
  110. if (isset($params['attachments'])) {
  111. $filesToAttach = (array)$params['attachments'];
  112. if ($filesToAttach) foreach ($filesToAttach as $fileToAttach) {
  113. $filesValues = $form->value($fileToAttach);
  114. if ($filesValues) foreach($filesValues as $fileValues) {
  115. if (isset($fileValues['file'])) {
  116. $filename = $fileValues['file'];
  117. } else {
  118. $filename = ROOT_DIR . $fileValues['path'];
  119. }
  120. try {
  121. $message->attachFromPath($filename);
  122. } catch (\Exception $e) {
  123. // Log any issues
  124. $this->grav['log']->error($e->getMessage());
  125. }
  126. }
  127. }
  128. }
  129. //fire event to apply optional signers
  130. $this->grav->fireEvent('onEmailMessage', new Event(['message' => $message, 'params' => $params, 'form' => $form]));
  131. // Send e-mail
  132. $this->email->send($message);
  133. //fire event after eMail was sent
  134. $this->grav->fireEvent('onEmailSent', new Event(['message' => $message, 'params' => $params, 'form' => $form]));
  135. }
  136. /**
  137. * Used for dynamic blueprint field
  138. *
  139. * @return array
  140. */
  141. public static function getEngines(): array
  142. {
  143. $engines = (object) [
  144. 'sendmail' => 'Sendmail',
  145. 'smtp' => 'SMTP',
  146. 'smtps' => 'SMTPS',
  147. 'native' => 'Native',
  148. 'none' => 'PLUGIN_ADMIN.DISABLED',
  149. ];
  150. Grav::instance()->fireEvent('onEmailEngines', new Event(['engines' => $engines]));
  151. return (array) $engines;
  152. }
  153. /**
  154. * @deprecated 4.0 Switched from Swiftmailer to Symfony/Mailer - No longer supported
  155. */
  156. public function onSchedulerInitialized(Event $e)
  157. {
  158. }
  159. }