Mail.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Services;
  3. /***
  4. * Mail Service
  5. */
  6. use App\Services\Mail\File;
  7. use App\Services\Mail\Mailgun;
  8. use App\Services\Mail\Ses;
  9. use App\Services\Mail\Smtp;
  10. use App\Services\Mail\SendCloud;
  11. use Smarty;
  12. class Mail
  13. {
  14. /**
  15. * @return Mailgun|Ses|Smtp|null
  16. */
  17. public static function getClient()
  18. {
  19. $driver = Config::get("mailDriver");
  20. switch ($driver) {
  21. case "mailgun":
  22. return new Mailgun();
  23. case "ses":
  24. return new Ses();
  25. case "smtp":
  26. return new Smtp();
  27. case "sendcloud":
  28. return new SendCloud();
  29. case "file":
  30. return new File();
  31. default:
  32. // @TODO default action
  33. }
  34. return null;
  35. }
  36. /**
  37. * @param $template
  38. * @param $ary
  39. * @return mixed
  40. */
  41. public static function genHtml($template, $ary)
  42. {
  43. $smarty = new smarty();
  44. $smarty->settemplatedir(BASE_PATH . '/resources/email/');
  45. $smarty->setcompiledir(BASE_PATH . '/storage/framework/smarty/compile/');
  46. $smarty->setcachedir(BASE_PATH . '/storage/framework/smarty/cache/');
  47. // add config
  48. $smarty->assign('config', Config::getPublicConfig());
  49. $smarty->assign('analyticsCode', DbConfig::get('analytics-code'));
  50. foreach ($ary as $key => $value) {
  51. $smarty->assign($key, $value);
  52. }
  53. return $smarty->fetch($template);
  54. }
  55. /**
  56. * @param $to
  57. * @param $subject
  58. * @param $template
  59. * @param $ary
  60. * @param $file
  61. * @return bool|void
  62. */
  63. public static function send($to, $subject, $template, $ary = [], $file = [])
  64. {
  65. $text = self::genHtml($template, $ary);
  66. Logger::debug($text);
  67. return self::getClient()->send($to, $subject, $text, $file);
  68. }
  69. }