You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

233 lines
5.6 KiB

  1. <?php
  2. namespace app\common\library;
  3. use think\Config;
  4. class Email
  5. {
  6. /**
  7. * 单例对象
  8. */
  9. protected static $instance;
  10. /**
  11. * phpmailer对象
  12. */
  13. protected $mail = [];
  14. /**
  15. * 错误内容
  16. */
  17. protected $_error = '';
  18. /**
  19. * 默认配置
  20. */
  21. public $options = [
  22. 'charset' => 'utf-8', //编码格式
  23. 'debug' => false, //调式模式
  24. ];
  25. /**
  26. * 初始化
  27. * @access public
  28. * @param array $options 参数
  29. * @return Email
  30. */
  31. public static function instance($options = [])
  32. {
  33. if (is_null(self::$instance)) {
  34. self::$instance = new static($options);
  35. }
  36. return self::$instance;
  37. }
  38. /**
  39. * 构造函数
  40. * @param array $options
  41. */
  42. public function __construct($options = [])
  43. {
  44. if ($config = Config::get('site')) {
  45. $this->options = array_merge($this->options, $config);
  46. }
  47. $this->options = array_merge($this->options, $options);
  48. $securArr = [1 => 'tls', 2 => 'ssl'];
  49. $this->mail = new \PHPMailer\PHPMailer\PHPMailer(true);
  50. $this->mail->CharSet = $this->options['charset'];
  51. if ($this->options['mail_type'] == 1) {
  52. $this->mail->SMTPDebug = $this->options['debug'];
  53. $this->mail->isSMTP();
  54. $this->mail->SMTPAuth = true;
  55. } else {
  56. $this->mail->isMail();
  57. }
  58. $this->mail->Host = $this->options['mail_smtp_host'];
  59. $this->mail->Username = $this->options['mail_from'];
  60. $this->mail->Password = $this->options['mail_smtp_pass'];
  61. $this->mail->SMTPSecure = isset($securArr[$this->options['mail_verify_type']]) ? $securArr[$this->options['mail_verify_type']] : '';
  62. $this->mail->Port = $this->options['mail_smtp_port'];
  63. //设置发件人
  64. $this->from($this->options['mail_from'], $this->options['mail_smtp_user']);
  65. }
  66. /**
  67. * 设置邮件主题
  68. * @param string $subject 邮件主题
  69. * @return $this
  70. */
  71. public function subject($subject)
  72. {
  73. $this->mail->Subject = $subject;
  74. return $this;
  75. }
  76. /**
  77. * 设置发件人
  78. * @param string $email 发件人邮箱
  79. * @param string $name 发件人名称
  80. * @return $this
  81. */
  82. public function from($email, $name = '')
  83. {
  84. $this->mail->setFrom($email, $name);
  85. return $this;
  86. }
  87. /**
  88. * 设置收件人
  89. * @param mixed $email 收件人,多个收件人以,进行分隔
  90. * @param string $name 收件人名称
  91. * @return $this
  92. */
  93. public function to($email, $name = '')
  94. {
  95. $emailArr = $this->buildAddress($email);
  96. foreach ($emailArr as $address => $name) {
  97. $this->mail->addAddress($address, $name);
  98. }
  99. return $this;
  100. }
  101. /**
  102. * 设置抄送
  103. * @param mixed $email 收件人,多个收件人以,进行分隔
  104. * @param string $name 收件人名称
  105. * @return Email
  106. */
  107. public function cc($email, $name = '')
  108. {
  109. $emailArr = $this->buildAddress($email);
  110. foreach ($emailArr as $address => $name) {
  111. $this->mail->addCC($address, $name);
  112. }
  113. return $this;
  114. }
  115. /**
  116. * 设置密送
  117. * @param mixed $email 收件人,多个收件人以,进行分隔
  118. * @param string $name 收件人名称
  119. * @return Email
  120. */
  121. public function bcc($email, $name = '')
  122. {
  123. $emailArr = $this->buildAddress($email);
  124. foreach ($emailArr as $address => $name) {
  125. $this->mail->addBCC($address, $name);
  126. }
  127. return $this;
  128. }
  129. /**
  130. * 设置邮件正文
  131. * @param string $body 邮件下方
  132. * @param boolean $ishtml 是否HTML格式
  133. * @return $this
  134. */
  135. public function message($body, $ishtml = true)
  136. {
  137. if ($ishtml) {
  138. $this->mail->msgHTML($body);
  139. } else {
  140. $this->mail->Body = $body;
  141. }
  142. return $this;
  143. }
  144. /**
  145. * 添加附件
  146. * @param string $path 附件路径
  147. * @param string $name 附件名称
  148. * @return Email
  149. */
  150. public function attachment($path, $name = '')
  151. {
  152. $this->mail->addAttachment($path, $name);
  153. return $this;
  154. }
  155. /**
  156. * 构建Email地址
  157. * @param mixed $emails Email数据
  158. * @return array
  159. */
  160. protected function buildAddress($emails)
  161. {
  162. $emails = is_array($emails) ? $emails : explode(',', str_replace(";", ",", $emails));
  163. $result = [];
  164. foreach ($emails as $key => $value) {
  165. $email = is_numeric($key) ? $value : $key;
  166. $result[$email] = is_numeric($key) ? "" : $value;
  167. }
  168. return $result;
  169. }
  170. /**
  171. * 获取最后产生的错误
  172. * @return string
  173. */
  174. public function getError()
  175. {
  176. return $this->_error;
  177. }
  178. /**
  179. * 设置错误
  180. * @param string $error 信息信息
  181. */
  182. protected function setError($error)
  183. {
  184. $this->_error = $error;
  185. }
  186. /**
  187. * 发送邮件
  188. * @return boolean
  189. */
  190. public function send()
  191. {
  192. $result = false;
  193. if (in_array($this->options['mail_type'], [1, 2])) {
  194. try {
  195. $result = $this->mail->send();
  196. } catch (\PHPMailer\PHPMailer\Exception $e) {
  197. $this->setError($e->getMessage());
  198. }
  199. $this->setError($result ? '' : $this->mail->ErrorInfo);
  200. } else {
  201. //邮件功能已关闭
  202. $this->setError(__('Mail already closed'));
  203. }
  204. return $result;
  205. }
  206. }