Email.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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' => 0, //调式模式
  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. {
  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('site'))
  46. {
  47. $this->options = array_merge($this->options, $config);
  48. }
  49. $this->options = array_merge($this->options, $options);
  50. vendor('phpmailer.phpmailer.PHPMailerAutoload');
  51. $securArr = [1 => 'tls', 2 => 'ssl'];
  52. $this->mail = new \PHPMailer(true);
  53. $this->mail->CharSet = $this->options['charset'];
  54. $this->mail->SMTPDebug = $this->options['debug'];
  55. $this->mail->isSMTP();
  56. $this->mail->SMTPAuth = true;
  57. $this->mail->Host = $this->options['mail_smtp_host'];
  58. $this->mail->Username = $this->options['mail_smtp_user'];
  59. $this->mail->Password = $this->options['mail_smtp_pass'];
  60. $this->mail->SMTPSecure = isset($securArr[$this->options['mail_verify_type']]) ? $securArr[$this->options['mail_verify_type']] : '';
  61. $this->mail->Port = $this->options['mail_smtp_port'];
  62. //设置发件人
  63. $this->from($this->options['mail_from']);
  64. }
  65. /**
  66. * 设置邮件主题
  67. * @param string $subject
  68. * @return $this
  69. */
  70. public function subject($subject)
  71. {
  72. $this->options['subject'] = $subject;
  73. return $this;
  74. }
  75. /**
  76. * 设置发件人
  77. * @param string $email
  78. * @param string $name
  79. * @return $this
  80. */
  81. public function from($email, $name = '')
  82. {
  83. $this->options['from'] = $email;
  84. $this->options['from_name'] = $name;
  85. return $this;
  86. }
  87. /**
  88. * 设置收件人
  89. * @param string $email
  90. * @param string $name
  91. * @return $this
  92. */
  93. public function to($email, $name = '')
  94. {
  95. $this->options['to'] = $email;
  96. $this->options['to_name'] = $name;
  97. return $this;
  98. }
  99. /**
  100. * 设置邮件正文
  101. * @param string $body
  102. * @param boolean $ishtml
  103. * @return $this
  104. */
  105. public function message($body, $ishtml = true)
  106. {
  107. $this->options['body'] = $body;
  108. $this->options['ishtml'] = $ishtml;
  109. return $this;
  110. }
  111. /**
  112. * 获取最后产生的错误
  113. * @return string
  114. */
  115. public function getError()
  116. {
  117. return $this->_error;
  118. }
  119. /**
  120. * 设置错误
  121. * @param string $error 信息信息
  122. */
  123. protected function setError($error)
  124. {
  125. $this->_error = $error;
  126. }
  127. /**
  128. * 发送邮件
  129. * @return boolean
  130. */
  131. public function send()
  132. {
  133. $result = false;
  134. switch ($this->options['mail_type'])
  135. {
  136. case 1:
  137. //使用phpmailer发送
  138. $this->mail->setFrom($this->options['from'], $this->options['from_name']);
  139. $this->mail->addAddress($this->options['to'], $this->options['to_name']);
  140. $this->mail->Subject = $this->options['subject'];
  141. if ($this->options['ishtml'])
  142. {
  143. $this->mail->msgHTML($this->options['body']);
  144. }
  145. else
  146. {
  147. $this->mail->Body = $this->options['body'];
  148. }
  149. try
  150. {
  151. $result = $this->mail->send();
  152. }
  153. catch (\phpmailerException $e)
  154. {
  155. $this->setError($e->getMessage());
  156. }
  157. $this->setError($result ? '' : $this->mail->ErrorInfo);
  158. break;
  159. case 2:
  160. //使用mail方法发送邮件
  161. $headers = 'MIME-Version: 1.0' . "\r\n";
  162. $headers .= "Content-type: text/html; charset=" . $this->options['charset'] . "\r\n";
  163. $headers .= "To: {$this->options['to_name']} <{$this->options['to']}>\r\n"; //收件人
  164. $headers .= "From: {$this->options['from_name']} <{$this->options['from']}>\r\n"; //发件人
  165. $result = mail($this->options['to'], $this->options['subject'], $this->options['body'], $headers);
  166. $this->setError($result ? '' : error_get_last()['message']);
  167. break;
  168. default:
  169. //邮件功能已关闭
  170. $this->setError(__('Mail already closed'));
  171. break;
  172. }
  173. return $result;
  174. }
  175. }