Skouter mortgage estimates. Web application with view written in PHP and Vue, but controller and models in Go.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

191 líneas
5.3 KiB

  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. $locator = $this->grav['locator'];
  111. if (isset($params['attachments'])) {
  112. $filesToAttach = (array)$params['attachments'];
  113. if ($filesToAttach) foreach ($filesToAttach as $fileToAttach) {
  114. $filesValues = $form->value($fileToAttach);
  115. if ($filesValues) foreach($filesValues as $fileValues) {
  116. if (isset($fileValues['file'])) {
  117. $filename = $fileValues['file'];
  118. } else {
  119. $filename = $fileValues['path'];
  120. }
  121. $filename = $locator->findResource($filename, true, true);
  122. try {
  123. $message->attachFromPath($filename);
  124. } catch (\Exception $e) {
  125. // Log any issues
  126. $this->grav['log']->error($e->getMessage());
  127. }
  128. }
  129. }
  130. }
  131. //fire event to apply optional signers
  132. $this->grav->fireEvent('onEmailMessage', new Event(['message' => $message, 'params' => $params, 'form' => $form]));
  133. // Send e-mail
  134. $this->email->send($message);
  135. //fire event after eMail was sent
  136. $this->grav->fireEvent('onEmailSent', new Event(['message' => $message, 'params' => $params, 'form' => $form]));
  137. }
  138. /**
  139. * Used for dynamic blueprint field
  140. *
  141. * @return array
  142. */
  143. public static function getEngines(): array
  144. {
  145. $engines = (object) [
  146. 'sendmail' => 'Sendmail',
  147. 'smtp' => 'SMTP',
  148. 'smtps' => 'SMTPS',
  149. 'native' => 'Native',
  150. 'none' => 'PLUGIN_ADMIN.DISABLED',
  151. ];
  152. Grav::instance()->fireEvent('onEmailEngines', new Event(['engines' => $engines]));
  153. return (array) $engines;
  154. }
  155. /**
  156. * @deprecated 4.0 Switched from Swiftmailer to Symfony/Mailer - No longer supported
  157. */
  158. public function onSchedulerInitialized(Event $e)
  159. {
  160. }
  161. }