Email.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace app\common\library;
  3. use think\facade\Lang;
  4. use PHPMailer\PHPMailer\Exception;
  5. use PHPMailer\PHPMailer\PHPMailer;
  6. /**
  7. * 邮件类
  8. * 继承PHPMailer并初始化好了站点系统配置中的邮件配置信息
  9. */
  10. class Email extends PHPMailer
  11. {
  12. /**
  13. * 是否已在管理后台配置好邮件服务
  14. */
  15. public $configured = false;
  16. /**
  17. * 默认配置
  18. */
  19. public $options = [
  20. 'charset' => 'utf-8', //编码格式
  21. 'debug' => true, //调式模式
  22. 'lang' => 'zh_cn',
  23. ];
  24. /**
  25. * 构造函数
  26. * @param array $options
  27. * @throws Exception
  28. */
  29. public function __construct(array $options = [])
  30. {
  31. $this->options = array_merge($this->options, $options);
  32. parent::__construct($this->options['debug']);
  33. $langset = Lang::getLangSet();
  34. if ($langset == 'zh-cn' || !$langset) $langset = 'zh_cn';
  35. $this->options['lang'] = $this->options['lang'] ?: $langset;
  36. $this->setLanguage($this->options['lang'], root_path() . 'vendor' . DIRECTORY_SEPARATOR . 'phpmailer' . DIRECTORY_SEPARATOR . 'phpmailer' . DIRECTORY_SEPARATOR . 'language' . DIRECTORY_SEPARATOR);
  37. $this->CharSet = $this->options['charset'];
  38. $sysMailConfig = get_sys_config('', 'mail');
  39. $this->configured = true;
  40. foreach ($sysMailConfig as $item) {
  41. if (!$item) {
  42. $this->configured = false;
  43. }
  44. }
  45. if ($this->configured) {
  46. $this->Host = $sysMailConfig['smtp_server'];
  47. $this->SMTPAuth = true;
  48. $this->Username = $sysMailConfig['smtp_user'];
  49. $this->Password = $sysMailConfig['smtp_pass'];
  50. $this->SMTPSecure = $sysMailConfig['smtp_verification'] == 'SSL' ? self::ENCRYPTION_SMTPS : self::ENCRYPTION_STARTTLS;
  51. $this->Port = $sysMailConfig['smtp_port'];
  52. $this->setFrom($sysMailConfig['smtp_sender_mail'], $sysMailConfig['smtp_user']);
  53. }
  54. }
  55. public function setSubject($subject)
  56. {
  57. $this->Subject = "=?utf-8?B?" . base64_encode($subject) . "?=";
  58. }
  59. }