Email.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?php
  2. namespace Grav\Plugin\Email;
  3. use Grav\Common\Config\Config;
  4. use Grav\Common\Grav;
  5. use Grav\Common\Language\Language;
  6. use Grav\Common\Markdown\Parsedown;
  7. use Grav\Common\Twig\Twig;
  8. use Grav\Framework\Form\Interfaces\FormInterface;
  9. use \Monolog\Logger;
  10. use \Monolog\Handler\StreamHandler;
  11. class Email
  12. {
  13. /**
  14. * @var \Swift_Transport
  15. */
  16. protected $mailer;
  17. /**
  18. * @var \Swift_Plugins_LoggerPlugin
  19. */
  20. protected $logger;
  21. protected $queue_path;
  22. /**
  23. * Returns true if emails have been enabled in the system.
  24. *
  25. * @return bool
  26. */
  27. public static function enabled()
  28. {
  29. return Grav::instance()['config']->get('plugins.email.mailer.engine') !== 'none';
  30. }
  31. /**
  32. * Returns true if debugging on emails has been enabled.
  33. *
  34. * @return bool
  35. */
  36. public static function debug()
  37. {
  38. return Grav::instance()['config']->get('plugins.email.debug') == 'true';
  39. }
  40. /**
  41. * Creates an email message.
  42. *
  43. * @param string $subject
  44. * @param string $body
  45. * @param string $contentType
  46. * @param string $charset
  47. * @return \Swift_Message
  48. */
  49. public function message($subject = null, $body = null, $contentType = null, $charset = null)
  50. {
  51. return new \Swift_Message($subject, $body, $contentType, $charset);
  52. }
  53. /**
  54. * Creates an attachment.
  55. *
  56. * @param string $data
  57. * @param string $filename
  58. * @param string $contentType
  59. * @return \Swift_Attachment
  60. */
  61. public function attachment($data = null, $filename = null, $contentType = null)
  62. {
  63. return new \Swift_Attachment($data, $filename, $contentType);
  64. }
  65. /**
  66. * Creates an embedded attachment.
  67. *
  68. * @param string $data
  69. * @param string $filename
  70. * @param string $contentType
  71. * @return \Swift_EmbeddedFile
  72. */
  73. public function embedded($data = null, $filename = null, $contentType = null)
  74. {
  75. return new \Swift_EmbeddedFile($data, $filename, $contentType);
  76. }
  77. /**
  78. * Creates an image attachment.
  79. *
  80. * @param string $data
  81. * @param string $filename
  82. * @param string $contentType
  83. * @return \Swift_Image
  84. */
  85. public function image($data = null, $filename = null, $contentType = null)
  86. {
  87. return new \Swift_Image($data, $filename, $contentType);
  88. }
  89. /**
  90. * Send email.
  91. *
  92. * @param \Swift_Message $message
  93. * @return int
  94. */
  95. public function send($message)
  96. {
  97. $mailer = $this->getMailer();
  98. $result = $mailer ? $mailer->send($message) : 0;
  99. // Check if emails and debugging are both enabled.
  100. if ($mailer && $this->debug()) {
  101. $log = new Logger('email');
  102. $locator = Grav::instance()['locator'];
  103. $log_file = $locator->findResource('log://email.log', true, true);
  104. $log->pushHandler(new StreamHandler($log_file, Logger::DEBUG));
  105. // Append the SwiftMailer log to the log.
  106. $log->addDebug($this->getLogs());
  107. }
  108. return $result;
  109. }
  110. /**
  111. * Build e-mail message.
  112. *
  113. * @param array $params
  114. * @param array $vars
  115. * @return \Swift_Message
  116. */
  117. public function buildMessage(array $params, array $vars = [])
  118. {
  119. /** @var Twig $twig */
  120. $twig = Grav::instance()['twig'];
  121. /** @var Config $config */
  122. $config = Grav::instance()['config'];
  123. /** @var Language $language */
  124. $language = Grav::instance()['language'];
  125. // Extend parameters with defaults.
  126. $params += [
  127. 'bcc' => $config->get('plugins.email.bcc', []),
  128. 'body' => $config->get('plugins.email.body', '{% include "forms/data.html.twig" %}'),
  129. 'cc' => $config->get('plugins.email.cc', []),
  130. 'cc_name' => $config->get('plugins.email.cc_name'),
  131. 'charset' => $config->get('plugins.email.charset', 'utf-8'),
  132. 'from' => $config->get('plugins.email.from'),
  133. 'from_name' => $config->get('plugins.email.from_name'),
  134. 'content_type' => $config->get('plugins.email.content_type', 'text/html'),
  135. 'reply_to' => $config->get('plugins.email.reply_to', []),
  136. 'reply_to_name' => $config->get('plugins.email.reply_to_name'),
  137. 'subject' => !empty($vars['form']) && $vars['form'] instanceof FormInterface ? $vars['form']->page()->title() : null,
  138. 'to' => $config->get('plugins.email.to'),
  139. 'to_name' => $config->get('plugins.email.to_name'),
  140. 'process_markdown' => false,
  141. 'template' => false
  142. ];
  143. // Create message object.
  144. $message = $this->message();
  145. if (!$params['to']) {
  146. throw new \RuntimeException($language->translate('PLUGIN_EMAIL.PLEASE_CONFIGURE_A_TO_ADDRESS'));
  147. }
  148. if (!$params['from']) {
  149. throw new \RuntimeException($language->translate('PLUGIN_EMAIL.PLEASE_CONFIGURE_A_FROM_ADDRESS'));
  150. }
  151. // Process parameters.
  152. foreach ($params as $key => $value) {
  153. switch ($key) {
  154. case 'body':
  155. if (is_string($value)) {
  156. $body = $twig->processString($value, $vars);
  157. if ($params['process_markdown']) {
  158. $parsedown = new Parsedown();
  159. $body = $parsedown->text($body);
  160. }
  161. if ($params['template']) {
  162. $vars = array_merge($vars, ['content' => $body]);
  163. $body = $twig->processTemplate($params['template'], $vars);
  164. }
  165. $content_type = !empty($params['content_type']) ? $twig->processString($params['content_type'], $vars) : null;
  166. $charset = !empty($params['charset']) ? $twig->processString($params['charset'], $vars) : null;
  167. $message->setBody($body, $content_type, $charset);
  168. }
  169. elseif (is_array($value)) {
  170. foreach ($value as $body_part) {
  171. $body_part += [
  172. 'charset' => $params['charset'],
  173. 'content_type' => $params['content_type'],
  174. ];
  175. $body = !empty($body_part['body']) ? $twig->processString($body_part['body'], $vars) : null;
  176. if ($params['process_markdown']) {
  177. $parsedown = new Parsedown();
  178. $body = $parsedown->text($body);
  179. }
  180. $content_type = !empty($body_part['content_type']) ? $twig->processString($body_part['content_type'], $vars) : null;
  181. $charset = !empty($body_part['charset']) ? $twig->processString($body_part['charset'], $vars) : null;
  182. if (!$message->getBody()) {
  183. $message->setBody($body, $content_type, $charset);
  184. }
  185. else {
  186. $message->addPart($body, $content_type, $charset);
  187. }
  188. }
  189. }
  190. break;
  191. case 'subject':
  192. $message->setSubject($twig->processString($language->translate($value), $vars));
  193. break;
  194. case 'to':
  195. if (is_string($value) && !empty($params['to_name'])) {
  196. $value = [
  197. 'mail' => $twig->processString($value, $vars),
  198. 'name' => $twig->processString($params['to_name'], $vars),
  199. ];
  200. }
  201. foreach ($this->parseAddressValue($value, $vars) as $address) {
  202. $message->addTo($address->mail, $address->name);
  203. }
  204. break;
  205. case 'cc':
  206. if (is_string($value) && !empty($params['cc_name'])) {
  207. $value = [
  208. 'mail' => $twig->processString($value, $vars),
  209. 'name' => $twig->processString($params['cc_name'], $vars),
  210. ];
  211. }
  212. foreach ($this->parseAddressValue($value, $vars) as $address) {
  213. $message->addCc($address->mail, $address->name);
  214. }
  215. break;
  216. case 'bcc':
  217. foreach ($this->parseAddressValue($value, $vars) as $address) {
  218. $message->addBcc($address->mail, $address->name);
  219. }
  220. break;
  221. case 'from':
  222. if (is_string($value) && !empty($params['from_name'])) {
  223. $value = [
  224. 'mail' => $twig->processString($value, $vars),
  225. 'name' => $twig->processString($params['from_name'], $vars),
  226. ];
  227. }
  228. foreach ($this->parseAddressValue($value, $vars) as $address) {
  229. $message->addFrom($address->mail, $address->name);
  230. }
  231. break;
  232. case 'reply_to':
  233. if (is_string($value) && !empty($params['reply_to_name'])) {
  234. $value = [
  235. 'mail' => $twig->processString($value, $vars),
  236. 'name' => $twig->processString($params['reply_to_name'], $vars),
  237. ];
  238. }
  239. foreach ($this->parseAddressValue($value, $vars) as $address) {
  240. $message->addReplyTo($address->mail, $address->name);
  241. }
  242. break;
  243. }
  244. }
  245. return $message;
  246. }
  247. /**
  248. * Return parsed e-mail address value.
  249. *
  250. * @param string|string[] $value
  251. * @param array $vars
  252. * @return array
  253. */
  254. public function parseAddressValue($value, array $vars = [])
  255. {
  256. $parsed = [];
  257. /** @var Twig $twig */
  258. $twig = Grav::instance()['twig'];
  259. // Single e-mail address string
  260. if (is_string($value)) {
  261. $parsed[] = (object) [
  262. 'mail' => $twig->processString($value, $vars),
  263. 'name' => null,
  264. ];
  265. }
  266. else {
  267. // Cast value as array
  268. $value = (array) $value;
  269. // Single e-mail address array
  270. if (!empty($value['mail'])) {
  271. $parsed[] = (object) [
  272. 'mail' => $twig->processString($value['mail'], $vars),
  273. 'name' => !empty($value['name']) ? $twig->processString($value['name'], $vars) : NULL,
  274. ];
  275. }
  276. // Multiple addresses (either as strings or arrays)
  277. elseif (!(empty($value['mail']) && !empty($value['name']))) {
  278. foreach ($value as $y => $itemx) {
  279. $addresses = $this->parseAddressValue($itemx, $vars);
  280. if (($address = reset($addresses))) {
  281. $parsed[] = $address;
  282. }
  283. }
  284. }
  285. }
  286. return $parsed;
  287. }
  288. /**
  289. * Return debugging logs if enabled
  290. *
  291. * @return string
  292. */
  293. public function getLogs()
  294. {
  295. if ($this->debug()) {
  296. return $this->logger->dump();
  297. }
  298. return '';
  299. }
  300. /**
  301. * @internal
  302. * @return null|\Swift_Mailer
  303. */
  304. protected function getMailer()
  305. {
  306. if (!$this->enabled()) {
  307. return null;
  308. }
  309. if (!$this->mailer) {
  310. /** @var Config $config */
  311. $config = Grav::instance()['config'];
  312. $queue_enabled = $config->get('plugins.email.queue.enabled');
  313. $transport = $queue_enabled === true ? $this->getQueue() : $this->getTransport();
  314. // Create the Mailer using your created Transport
  315. $this->mailer = new \Swift_Mailer($transport);
  316. // Register the logger if we're debugging.
  317. if ($this->debug()) {
  318. $this->logger = new \Swift_Plugins_Loggers_ArrayLogger();
  319. $this->mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($this->logger));
  320. }
  321. }
  322. return $this->mailer;
  323. }
  324. protected static function getQueuePath()
  325. {
  326. $queue_path = Grav::instance()['locator']->findResource('user://data', true) . '/email-queue';
  327. if (!file_exists($queue_path)) {
  328. mkdir($queue_path);
  329. }
  330. return $queue_path;
  331. }
  332. protected static function getQueue()
  333. {
  334. $queue_path = static::getQueuePath();
  335. $spool = new \Swift_FileSpool($queue_path);
  336. $transport = new \Swift_SpoolTransport($spool);
  337. return $transport;
  338. }
  339. public static function flushQueue()
  340. {
  341. $grav = Grav::instance();
  342. $grav['debugger']->enabled(false);
  343. $config = $grav['config']->get('plugins.email.queue');
  344. $queue = static::getQueue();
  345. $spool = $queue->getSpool();
  346. $spool->setMessageLimit($config['flush_msg_limit']);
  347. $spool->setTimeLimit($config['flush_time_limit']);
  348. try {
  349. $failures = [];
  350. $result = $spool->flushQueue(static::getTransport(), $failures);
  351. return $result . ' messages flushed from queue...';
  352. } catch (\Exception $e) {
  353. $grav['log']->error($e->getMessage());
  354. return $e->getMessage();
  355. }
  356. }
  357. public static function clearQueueFailures()
  358. {
  359. $grav = Grav::instance();
  360. $grav['debugger']->enabled(false);
  361. $preferences = \Swift_Preferences::getInstance();
  362. $preferences->setTempDir(sys_get_temp_dir());
  363. /** @var \Swift_Transport $transport */
  364. $transport = static::getTransport();
  365. if (!$transport->isStarted()) {
  366. $transport->start();
  367. }
  368. $queue_path = static::getQueuePath();
  369. foreach (new \GlobIterator($queue_path . '/*.sending') as $file) {
  370. $final_message = $file->getPathname();
  371. /** @var \Swift_Message $message */
  372. $message = unserialize(file_get_contents($final_message));
  373. echo(sprintf(
  374. 'Retrying "%s" to "%s"',
  375. $message->getSubject(),
  376. implode(', ', array_keys($message->getTo()))
  377. ) . "\n");
  378. try {
  379. $clean = static::cloneMessage($message);
  380. $transport->send($clean);
  381. echo("sent!\n");
  382. // DOn't want to trip up any errors from sending too fast
  383. sleep(1);
  384. } catch (\Swift_TransportException $e) {
  385. echo("ERROR: Send failed - deleting spooled message\n");
  386. }
  387. // Remove the file
  388. unlink($final_message);
  389. }
  390. }
  391. /**
  392. * Clean copy a message
  393. *
  394. * @param \Swift_Message $message
  395. */
  396. public static function cloneMessage($message)
  397. {
  398. $clean = new \Swift_Message();
  399. $clean->setBoundary($message->getBoundary());
  400. $clean->setBcc($message->getBcc());
  401. $clean->setBody($message->getBody());
  402. $clean->setCharset($message->getCharset());
  403. $clean->setChildren($message->getChildren());
  404. $clean->setContentType($message->getContentType());
  405. $clean->setCc($message->getCc());
  406. $clean->setDate($message->getDate());
  407. $clean->setDescription($message->getDescription());
  408. $clean->setEncoder($message->getEncoder());
  409. $clean->setFormat($message->getFormat());
  410. $clean->setFrom($message->getFrom());
  411. $clean->setId($message->getId());
  412. $clean->setMaxLineLength($message->getMaxLineLength());
  413. $clean->setPriority($message->getPriority());
  414. $clean->setReplyTo($message->getReplyTo());
  415. $clean->setReturnPath($message->getReturnPath());
  416. $clean->setSender($message->getSender());
  417. $clean->setSubject($message->getSubject());
  418. $clean->setTo($message->getTo());
  419. return $clean;
  420. }
  421. protected static function getTransport()
  422. {
  423. /** @var Config $config */
  424. $config = Grav::instance()['config'];
  425. $engine = $config->get('plugins.email.mailer.engine');
  426. // Create the Transport and initialize it.
  427. switch ($engine) {
  428. case 'smtp':
  429. $transport = new \Swift_SmtpTransport();
  430. $options = $config->get('plugins.email.mailer.smtp');
  431. if (!empty($options['server'])) {
  432. $transport->setHost($options['server']);
  433. }
  434. if (!empty($options['port'])) {
  435. $transport->setPort($options['port']);
  436. }
  437. if (!empty($options['encryption']) && $options['encryption'] !== 'none') {
  438. $transport->setEncryption($options['encryption']);
  439. }
  440. if (!empty($options['user'])) {
  441. $transport->setUsername($options['user']);
  442. }
  443. if (!empty($options['password'])) {
  444. $transport->setPassword($options['password']);
  445. }
  446. break;
  447. case 'sendmail':
  448. default:
  449. $options = $config->get('plugins.email.mailer.sendmail');
  450. $bin = !empty($options['bin']) ? $options['bin'] : '/usr/sbin/sendmail';
  451. $transport = new \Swift_SendmailTransport($bin);
  452. break;
  453. }
  454. return $transport;
  455. }
  456. }