Email.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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'] && $params['content_type'] === 'text/html') {
  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'] && $body_part['content_type'] === 'text/html') {
  177. $parsedown = new Parsedown();
  178. $body = $parsedown->text($body);
  179. }
  180. if (isset($body_part['template'])) {
  181. $vars = array_merge($vars, ['content' => $body]);
  182. $body = $twig->processTemplate($body_part['template'], $vars);
  183. }
  184. $content_type = !empty($body_part['content_type']) ? $twig->processString($body_part['content_type'], $vars) : null;
  185. $charset = !empty($body_part['charset']) ? $twig->processString($body_part['charset'], $vars) : null;
  186. if (!$message->getBody()) {
  187. $message->setBody($body, $content_type, $charset);
  188. }
  189. else {
  190. $message->addPart($body, $content_type, $charset);
  191. }
  192. }
  193. }
  194. break;
  195. case 'subject':
  196. $message->setSubject($twig->processString($language->translate($value), $vars));
  197. break;
  198. case 'to':
  199. if (is_string($value) && !empty($params['to_name'])) {
  200. $value = [
  201. 'mail' => $twig->processString($value, $vars),
  202. 'name' => $twig->processString($params['to_name'], $vars),
  203. ];
  204. }
  205. foreach ($this->parseAddressValue($value, $vars) as $address) {
  206. $message->addTo($address->mail, $address->name);
  207. }
  208. break;
  209. case 'cc':
  210. if (is_string($value) && !empty($params['cc_name'])) {
  211. $value = [
  212. 'mail' => $twig->processString($value, $vars),
  213. 'name' => $twig->processString($params['cc_name'], $vars),
  214. ];
  215. }
  216. foreach ($this->parseAddressValue($value, $vars) as $address) {
  217. $message->addCc($address->mail, $address->name);
  218. }
  219. break;
  220. case 'bcc':
  221. foreach ($this->parseAddressValue($value, $vars) as $address) {
  222. $message->addBcc($address->mail, $address->name);
  223. }
  224. break;
  225. case 'from':
  226. if (is_string($value) && !empty($params['from_name'])) {
  227. $value = [
  228. 'mail' => $twig->processString($value, $vars),
  229. 'name' => $twig->processString($params['from_name'], $vars),
  230. ];
  231. }
  232. foreach ($this->parseAddressValue($value, $vars) as $address) {
  233. $message->addFrom($address->mail, $address->name);
  234. }
  235. break;
  236. case 'reply_to':
  237. if (is_string($value) && !empty($params['reply_to_name'])) {
  238. $value = [
  239. 'mail' => $twig->processString($value, $vars),
  240. 'name' => $twig->processString($params['reply_to_name'], $vars),
  241. ];
  242. }
  243. foreach ($this->parseAddressValue($value, $vars) as $address) {
  244. $message->addReplyTo($address->mail, $address->name);
  245. }
  246. break;
  247. }
  248. }
  249. return $message;
  250. }
  251. /**
  252. * Return parsed e-mail address value.
  253. *
  254. * @param string|string[] $value
  255. * @param array $vars
  256. * @return array
  257. */
  258. public function parseAddressValue($value, array $vars = [])
  259. {
  260. $parsed = [];
  261. /** @var Twig $twig */
  262. $twig = Grav::instance()['twig'];
  263. // Single e-mail address string
  264. if (is_string($value)) {
  265. $parsed[] = (object) [
  266. 'mail' => $twig->processString($value, $vars),
  267. 'name' => null,
  268. ];
  269. }
  270. else {
  271. // Cast value as array
  272. $value = (array) $value;
  273. // Single e-mail address array
  274. if (!empty($value['mail'])) {
  275. $parsed[] = (object) [
  276. 'mail' => $twig->processString($value['mail'], $vars),
  277. 'name' => !empty($value['name']) ? $twig->processString($value['name'], $vars) : NULL,
  278. ];
  279. }
  280. // Multiple addresses (either as strings or arrays)
  281. elseif (!(empty($value['mail']) && !empty($value['name']))) {
  282. foreach ($value as $y => $itemx) {
  283. $addresses = $this->parseAddressValue($itemx, $vars);
  284. if (($address = reset($addresses))) {
  285. $parsed[] = $address;
  286. }
  287. }
  288. }
  289. }
  290. return $parsed;
  291. }
  292. /**
  293. * Return debugging logs if enabled
  294. *
  295. * @return string
  296. */
  297. public function getLogs()
  298. {
  299. if ($this->debug()) {
  300. return $this->logger->dump();
  301. }
  302. return '';
  303. }
  304. /**
  305. * @internal
  306. * @return null|\Swift_Mailer
  307. */
  308. protected function getMailer()
  309. {
  310. if (!$this->enabled()) {
  311. return null;
  312. }
  313. if (!$this->mailer) {
  314. /** @var Config $config */
  315. $config = Grav::instance()['config'];
  316. $queue_enabled = $config->get('plugins.email.queue.enabled');
  317. $transport = $queue_enabled === true ? $this->getQueue() : $this->getTransport();
  318. // Create the Mailer using your created Transport
  319. $this->mailer = new \Swift_Mailer($transport);
  320. // Register the logger if we're debugging.
  321. if ($this->debug()) {
  322. $this->logger = new \Swift_Plugins_Loggers_ArrayLogger();
  323. $this->mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($this->logger));
  324. }
  325. }
  326. return $this->mailer;
  327. }
  328. protected static function getQueuePath()
  329. {
  330. $queue_path = Grav::instance()['locator']->findResource('user://data', true) . '/email-queue';
  331. if (!file_exists($queue_path)) {
  332. mkdir($queue_path);
  333. }
  334. return $queue_path;
  335. }
  336. protected static function getQueue()
  337. {
  338. $queue_path = static::getQueuePath();
  339. $spool = new \Swift_FileSpool($queue_path);
  340. $transport = new \Swift_SpoolTransport($spool);
  341. return $transport;
  342. }
  343. public static function flushQueue()
  344. {
  345. $grav = Grav::instance();
  346. $grav['debugger']->enabled(false);
  347. $config = $grav['config']->get('plugins.email.queue');
  348. $queue = static::getQueue();
  349. $spool = $queue->getSpool();
  350. $spool->setMessageLimit($config['flush_msg_limit']);
  351. $spool->setTimeLimit($config['flush_time_limit']);
  352. try {
  353. $failures = [];
  354. $result = $spool->flushQueue(static::getTransport(), $failures);
  355. return $result . ' messages flushed from queue...';
  356. } catch (\Exception $e) {
  357. $grav['log']->error($e->getMessage());
  358. return $e->getMessage();
  359. }
  360. }
  361. public static function clearQueueFailures()
  362. {
  363. $grav = Grav::instance();
  364. $grav['debugger']->enabled(false);
  365. $preferences = \Swift_Preferences::getInstance();
  366. $preferences->setTempDir(sys_get_temp_dir());
  367. /** @var \Swift_Transport $transport */
  368. $transport = static::getTransport();
  369. if (!$transport->isStarted()) {
  370. $transport->start();
  371. }
  372. $queue_path = static::getQueuePath();
  373. foreach (new \GlobIterator($queue_path . '/*.sending') as $file) {
  374. $final_message = $file->getPathname();
  375. /** @var \Swift_Message $message */
  376. $message = unserialize(file_get_contents($final_message));
  377. echo(sprintf(
  378. 'Retrying "%s" to "%s"',
  379. $message->getSubject(),
  380. implode(', ', array_keys($message->getTo()))
  381. ) . "\n");
  382. try {
  383. $clean = static::cloneMessage($message);
  384. $transport->send($clean);
  385. echo("sent!\n");
  386. // DOn't want to trip up any errors from sending too fast
  387. sleep(1);
  388. } catch (\Swift_TransportException $e) {
  389. echo("ERROR: Send failed - deleting spooled message\n");
  390. }
  391. // Remove the file
  392. unlink($final_message);
  393. }
  394. }
  395. /**
  396. * Clean copy a message
  397. *
  398. * @param \Swift_Message $message
  399. */
  400. public static function cloneMessage($message)
  401. {
  402. $clean = new \Swift_Message();
  403. $clean->setBoundary($message->getBoundary());
  404. $clean->setBcc($message->getBcc());
  405. $clean->setBody($message->getBody());
  406. $clean->setCharset($message->getCharset());
  407. $clean->setChildren($message->getChildren());
  408. $clean->setContentType($message->getContentType());
  409. $clean->setCc($message->getCc());
  410. $clean->setDate($message->getDate());
  411. $clean->setDescription($message->getDescription());
  412. $clean->setEncoder($message->getEncoder());
  413. $clean->setFormat($message->getFormat());
  414. $clean->setFrom($message->getFrom());
  415. $clean->setId($message->getId());
  416. $clean->setMaxLineLength($message->getMaxLineLength());
  417. $clean->setPriority($message->getPriority());
  418. $clean->setReplyTo($message->getReplyTo());
  419. $clean->setReturnPath($message->getReturnPath());
  420. $clean->setSender($message->getSender());
  421. $clean->setSubject($message->getSubject());
  422. $clean->setTo($message->getTo());
  423. $clean->setAuthMode($message->getAuthMode());
  424. return $clean;
  425. }
  426. protected static function getTransport()
  427. {
  428. /** @var Config $config */
  429. $config = Grav::instance()['config'];
  430. $engine = $config->get('plugins.email.mailer.engine');
  431. // Create the Transport and initialize it.
  432. switch ($engine) {
  433. case 'smtp':
  434. $transport = new \Swift_SmtpTransport();
  435. $options = $config->get('plugins.email.mailer.smtp');
  436. if (!empty($options['server'])) {
  437. $transport->setHost($options['server']);
  438. }
  439. if (!empty($options['port'])) {
  440. $transport->setPort($options['port']);
  441. }
  442. if (!empty($options['encryption']) && $options['encryption'] !== 'none') {
  443. $transport->setEncryption($options['encryption']);
  444. }
  445. if (!empty($options['user'])) {
  446. $transport->setUsername($options['user']);
  447. }
  448. if (!empty($options['password'])) {
  449. $transport->setPassword($options['password']);
  450. }
  451. if (!empty($options['auth_mode'])) {
  452. $transport->setAuthMode($options['auth_mode']);
  453. }
  454. break;
  455. case 'sendmail':
  456. default:
  457. $options = $config->get('plugins.email.mailer.sendmail');
  458. $bin = !empty($options['bin']) ? $options['bin'] : '/usr/sbin/sendmail';
  459. $transport = new \Swift_SendmailTransport($bin);
  460. break;
  461. }
  462. return $transport;
  463. }
  464. }