Email.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. 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. vendor('phpmailer.phpmailer.PHPMailerAutoload');
  49. $securArr = [1 => 'tls', 2 => 'ssl'];
  50. $this->mail = new \PHPMailer(true);
  51. $this->mail->CharSet = $this->options['charset'];
  52. $this->mail->SMTPDebug = $this->options['debug'];
  53. $this->mail->isSMTP();
  54. $this->mail->SMTPAuth = true;
  55. $this->mail->Host = $this->options['mail_smtp_host'];
  56. $this->mail->Username = $this->options['mail_from'];
  57. $this->mail->Password = $this->options['mail_smtp_pass'];
  58. $this->mail->SMTPSecure = isset($securArr[$this->options['mail_verify_type']]) ? $securArr[$this->options['mail_verify_type']] : '';
  59. $this->mail->Port = $this->options['mail_smtp_port'];
  60. //设置发件人
  61. $this->from($this->options['mail_from'], $this->options['mail_smtp_user']);
  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['mail_type']) {
  133. case 1:
  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. } catch (\phpmailerException $e) {
  146. $this->setError($e->getMessage());
  147. }
  148. $this->setError($result ? '' : $this->mail->ErrorInfo);
  149. break;
  150. case 2:
  151. //使用mail方法发送邮件
  152. $headers = 'MIME-Version: 1.0' . "\r\n";
  153. $headers .= "Content-type: text/html; charset=" . $this->options['charset'] . "\r\n";
  154. $headers .= "To: {$this->options['to_name']} <{$this->options['to']}>\r\n"; //收件人
  155. $headers .= "From: {$this->options['from_name']} <{$this->options['from']}>\r\n"; //发件人
  156. $result = mail($this->options['to'], $this->options['subject'], $this->options['body'], $headers);
  157. $this->setError($result ? '' : error_get_last()['message']);
  158. break;
  159. default:
  160. //邮件功能已关闭
  161. $this->setError(__('Mail already closed'));
  162. break;
  163. }
  164. return $result;
  165. }
  166. }