Email.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace Grav\Plugin\Email;
  3. use Grav\Common\Config\Config;
  4. use Grav\Common\Grav;
  5. use \Monolog\Logger;
  6. use \Monolog\Handler\StreamHandler;
  7. class Email
  8. {
  9. /**
  10. * @var \Swift_Transport
  11. */
  12. protected $mailer;
  13. /**
  14. * @var \Swift_Plugins_LoggerPlugin
  15. */
  16. protected $logger;
  17. /**
  18. * Returns true if emails have been enabled in the system.
  19. *
  20. * @return bool
  21. */
  22. public function enabled()
  23. {
  24. return Grav::instance()['config']->get('plugins.email.mailer.engine') != 'none';
  25. }
  26. /**
  27. * Returns true if debugging on emails has been enabled.
  28. *
  29. * @return bool
  30. */
  31. public function debug()
  32. {
  33. return Grav::instance()['config']->get('plugins.email.debug') == 'true';
  34. }
  35. /**
  36. * Creates an email message.
  37. *
  38. * @param string $subject
  39. * @param string $body
  40. * @param string $contentType
  41. * @param string $charset
  42. * @return \Swift_Message
  43. */
  44. public function message($subject = null, $body = null, $contentType = null, $charset = null)
  45. {
  46. return new \Swift_Message($subject, $body, $contentType, $charset);
  47. }
  48. /**
  49. * Creates an attachment.
  50. *
  51. * @param string $data
  52. * @param string $filename
  53. * @param string $contentType
  54. * @return \Swift_Attachment
  55. */
  56. public function attachment($data = null, $filename = null, $contentType = null)
  57. {
  58. return new \Swift_Attachment($data, $filename, $contentType);
  59. }
  60. /**
  61. * Creates an embedded attachment.
  62. *
  63. * @param string $data
  64. * @param string $filename
  65. * @param string $contentType
  66. * @return \Swift_EmbeddedFile
  67. */
  68. public function embedded($data = null, $filename = null, $contentType = null)
  69. {
  70. return new \Swift_EmbeddedFile($data, $filename, $contentType);
  71. }
  72. /**
  73. * Creates an image attachment.
  74. *
  75. * @param string $data
  76. * @param string $filename
  77. * @param string $contentType
  78. * @return \Swift_Image
  79. */
  80. public function image($data = null, $filename = null, $contentType = null)
  81. {
  82. return new \Swift_Image($data, $filename, $contentType);
  83. }
  84. /**
  85. * Send email.
  86. *
  87. * @param \Swift_Message $message
  88. * @return int
  89. */
  90. public function send($message)
  91. {
  92. $mailer = $this->getMailer();
  93. $result = $mailer ? $mailer->send($message) : 0;
  94. // Check if emails and debugging are both enabled.
  95. if ($mailer && $this->debug()) {
  96. $log = new Logger('email');
  97. $locator = Grav::instance()['locator'];
  98. $log_file = $locator->findResource('log://email.log', true, true);
  99. $log->pushHandler(new StreamHandler($log_file, Logger::DEBUG));
  100. // Append the SwiftMailer log to the log.
  101. $log->addDebug($this->getLogs());
  102. }
  103. return $result;
  104. }
  105. /**
  106. * Return debugging logs if enabled
  107. *
  108. * @return string
  109. */
  110. public function getLogs()
  111. {
  112. if ($this->debug()) {
  113. return $this->logger->dump();
  114. }
  115. return '';
  116. }
  117. /**
  118. * @internal
  119. * @return null|\Swift_Mailer
  120. */
  121. protected function getMailer()
  122. {
  123. if (!$this->enabled()) {
  124. return null;
  125. }
  126. if (!$this->mailer) {
  127. /** @var Config $config */
  128. $config = Grav::instance()['config'];
  129. $mailer = $config->get('plugins.email.mailer.engine');
  130. // Create the Transport and initialize it.
  131. switch ($mailer) {
  132. case 'smtp':
  133. $transport = \Swift_SmtpTransport::newInstance();
  134. $options = $config->get('plugins.email.mailer.smtp');
  135. if (!empty($options['server'])) {
  136. $transport->setHost($options['server']);
  137. }
  138. if (!empty($options['port'])) {
  139. $transport->setPort($options['port']);
  140. }
  141. if (!empty($options['encryption']) && $options['encryption'] != 'none') {
  142. $transport->setEncryption($options['encryption']);
  143. }
  144. if (!empty($options['user'])) {
  145. $transport->setUsername($options['user']);
  146. }
  147. if (!empty($options['password'])) {
  148. $transport->setPassword($options['password']);
  149. }
  150. break;
  151. case 'sendmail':
  152. default:
  153. $options = $config->get('plugins.email.mailer.sendmail');
  154. $bin = !empty($options['bin']) ? $options['bin'] : '/usr/sbin/sendmail';
  155. $transport = \Swift_SendmailTransport::newInstance($bin);
  156. break;
  157. }
  158. // Create the Mailer using your created Transport
  159. $this->mailer = \Swift_Mailer::newInstance($transport);
  160. // Register the logger if we're debugging.
  161. if ($this->debug()) {
  162. $this->logger = new \Swift_Plugins_Loggers_ArrayLogger();
  163. $this->mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($this->logger));
  164. }
  165. }
  166. return $this->mailer;
  167. }
  168. }