Mail.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace App\Services;
  3. /***
  4. * Mail Service
  5. */
  6. use App\Services\Mail\Mailgun;
  7. use App\Services\Mail\Ses;
  8. use App\Services\Mail\Smtp;
  9. use App\Services\Mail\SendGrid;
  10. use App\Services\Mail\NullMail;
  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 "sendgrid":
  28. return new SendGrid();
  29. default:
  30. return new NullMail();
  31. }
  32. return null;
  33. }
  34. /**
  35. * @param $template
  36. * @param $ary
  37. * @return mixed
  38. */
  39. public static function genHtml($template, $ary)
  40. {
  41. $smarty = new smarty();
  42. $smarty->settemplatedir(BASE_PATH . '/resources/email/');
  43. $smarty->setcompiledir(BASE_PATH . '/storage/framework/smarty/compile/');
  44. $smarty->setcachedir(BASE_PATH . '/storage/framework/smarty/cache/');
  45. // add config
  46. $smarty->assign('config', Config::getPublicConfig());
  47. foreach ($ary as $key => $value) {
  48. $smarty->assign($key, $value);
  49. }
  50. return $smarty->fetch($template);
  51. }
  52. /**
  53. * @param $to
  54. * @param $subject
  55. * @param $template
  56. * @param $ary
  57. * @param $file
  58. * @return bool|void
  59. */
  60. public static function send($to, $subject, $template, $ary = [], $file = [])
  61. {
  62. $text = self::genHtml($template, $ary);
  63. return self::getClient()->send($to, $subject, $text, $file);
  64. }
  65. }