Email.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace laytp\library;
  3. use PHPMailer\PHPMailer\PHPMailer;
  4. use think\facade\Config;
  5. class Email
  6. {
  7. /**
  8. * 单例对象
  9. */
  10. protected static $instance;
  11. /**
  12. * phpmailer对象
  13. */
  14. protected $mail = [];
  15. /**
  16. * 错误内容
  17. */
  18. protected $_error = '';
  19. /**
  20. * 默认配置
  21. */
  22. public $options = [
  23. 'charset' => 'utf-8', //编码格式
  24. 'debug' => 0, //调式模式
  25. ];
  26. /**
  27. * 初始化
  28. * @access public
  29. * @param array $options 参数
  30. * @return Email
  31. */
  32. public static function instance($options = [])
  33. {
  34. if (is_null(self::$instance)) {
  35. self::$instance = new static($options);
  36. }
  37. return self::$instance;
  38. }
  39. /**
  40. * 构造函数
  41. * @param array $options
  42. */
  43. public function __construct($options = [])
  44. {
  45. if ($config = Config::get('laytp.email')) {
  46. $this->options = array_merge($this->options, $config);
  47. }
  48. $this->options = array_merge($this->options, $options);
  49. $securArr = [1 => 'tls', 2 => 'ssl'];
  50. $this->mail = new PHPMailer(true);
  51. $this->mail->CharSet = $this->options['charset'];
  52. $this->mail->SMTPDebug = false;
  53. $this->mail->isSMTP();
  54. $this->mail->SMTPAuth = true;
  55. $this->mail->Host = $this->options['smtp_host'];
  56. $this->mail->Username = $this->options['smtp_user'];
  57. $this->mail->Password = $this->options['smtp_password'];
  58. $this->mail->SMTPSecure = isset($securArr[$this->options['verify_type']]) ? $securArr[$this->options['verify_type']] : '';
  59. $this->mail->Port = $this->options['smtp_port'];
  60. //设置发件人
  61. $this->from($this->options['from'], $this->options['from_name']);
  62. }
  63. /**
  64. * 设置邮件主题
  65. * @param string $subject
  66. * @return $this
  67. */
  68. public function subject($subject)
  69. {
  70. $this->options['subject'] = $subject;
  71. return $this;
  72. }
  73. /**
  74. * 设置发件人
  75. * @param string $email
  76. * @param string $name
  77. * @return $this
  78. */
  79. public function from($email, $name = '')
  80. {
  81. $this->options['from'] = $email;
  82. $this->options['from_name'] = $name;
  83. return $this;
  84. }
  85. /**
  86. * 设置收件人
  87. * @param string $email
  88. * @param string $name
  89. * @return $this
  90. */
  91. public function to($email, $name = '')
  92. {
  93. $this->options['to'] = $email;
  94. $this->options['to_name'] = $name;
  95. return $this;
  96. }
  97. /**
  98. * 设置邮件正文
  99. * @param string $body
  100. * @param boolean $ishtml
  101. * @return $this
  102. */
  103. public function message($body, $ishtml = true)
  104. {
  105. $this->options['body'] = $body;
  106. $this->options['ishtml'] = $ishtml;
  107. return $this;
  108. }
  109. /**
  110. * 获取最后产生的错误
  111. * @return string
  112. */
  113. public function getError()
  114. {
  115. return $this->_error;
  116. }
  117. /**
  118. * 设置错误
  119. * @param string $error 信息信息
  120. */
  121. protected function setError($error)
  122. {
  123. $this->_error = $error;
  124. }
  125. /**
  126. * 发送邮件
  127. * @return boolean
  128. */
  129. public function send()
  130. {
  131. $result = false;
  132. switch ($this->options['send_type']) {
  133. case 'smtp':
  134. //使用phpmailer发送
  135. $this->mail->setFrom($this->options['from'], $this->options['from_name']);
  136. $this->mail->addAddress($this->options['to'], $this->options['to_name']);
  137. $this->mail->Subject = $this->options['subject'];
  138. if ($this->options['ishtml']) {
  139. $this->mail->msgHTML($this->options['body']);
  140. } else {
  141. $this->mail->Body = $this->options['body'];
  142. }
  143. try {
  144. $result = $this->mail->send();
  145. if ($result) {
  146. return $result;
  147. } else {
  148. $this->setError($this->mail->ErrorInfo);
  149. }
  150. } catch (\phpmailerException $e) {
  151. $this->setError($e->getMessage());
  152. return false;
  153. }
  154. break;
  155. case 'mail':
  156. //使用mail方法发送邮件
  157. $headers = 'MIME-Version: 1.0' . "\r\n";
  158. $headers .= "Content-type: text/html; charset=" . $this->options['charset'] . "\r\n";
  159. $headers .= "To: {$this->options['to_name']} <{$this->options['to']}>\r\n"; //收件人
  160. $headers .= "From: {$this->options['from_name']} <{$this->options['from']}>\r\n"; //发件人
  161. $result = mail($this->options['to'], $this->options['subject'], $this->options['body'], $headers);
  162. $this->setError($result ? '' : error_get_last()['message']);
  163. break;
  164. default:
  165. //邮件功能已关闭
  166. $this->setError('邮件功能已关闭');
  167. break;
  168. }
  169. return $result;
  170. }
  171. }