Mailer.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // |
  4. // +----------------------------------------------------------------------
  5. // |
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // |
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | phpemail类
  13. // +----------------------------------------------------------------------
  14. namespace addons\phpmailer\library;
  15. use PHPMailer\PHPMailer\Exception;
  16. use PHPMailer\PHPMailer\PHPMailer;
  17. class Mailer
  18. {
  19. /**
  20. * phpmailer对象
  21. */
  22. protected $mail = [];
  23. /**
  24. * 错误内容
  25. */
  26. protected $_error = '';
  27. /**
  28. * 默认配置
  29. */
  30. public $options = [
  31. 'debug' => 0, //关闭调式模式
  32. ];
  33. /**
  34. * 构造函数
  35. * @param array $options
  36. */
  37. public function __construct($options = [])
  38. {
  39. /*if ($config = \think\facade\Config::get('app.')) {
  40. $this->options = array_merge($this->options, $config);
  41. }*/
  42. $this->options = array_merge($this->options, $options);
  43. $securArr = [1 => 'tls', 2 => 'ssl'];
  44. $this->mail = new PHPMailer(true);
  45. $this->mail->SMTPDebug = $this->options['debug'];
  46. $this->mail->CharSet = 'utf-8';
  47. $this->mail->isSMTP();
  48. $this->mail->Host = $this->options['mail_smtp_host']; //SMTP服务器
  49. // 设置为“需要验证”
  50. if ($this->options['mail_auth']) {
  51. $this->mail->SMTPAuth = true;
  52. } else {
  53. $this->mail->SMTPAuth = false;
  54. }
  55. $this->mail->Username = $this->options['mail_smtp_user']; //SMTP username
  56. $this->mail->Password = $this->options['mail_smtp_pass']; // SMTP password
  57. $this->mail->SMTPSecure = isset($securArr[$this->options['mail_verify_type']]) ? $securArr[$this->options['mail_verify_type']] : ''; //支持TLS加密,还接受了ssl
  58. $this->mail->Port = $this->options['mail_smtp_port']; //TCP端口连接
  59. $this->mail->Timeout = isset($this->options['mail_timeout']) ? (int) $this->options['mail_timeout'] : 8;
  60. if ($this->mail->Timeout <= 0) {
  61. $this->mail->Timeout = 8;
  62. }
  63. //设置发件人
  64. $this->from($this->options['mail_from']);
  65. }
  66. /**
  67. * 设置邮件主题
  68. * @param string $subject
  69. * @return $this
  70. */
  71. public function subject($subject)
  72. {
  73. $this->options['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->options['from'] = $email;
  85. $this->options['from_name'] = $name;
  86. return $this;
  87. }
  88. /**
  89. * 设置邮件正文
  90. * @param string $body
  91. * @param boolean $ishtml
  92. * @return $this
  93. */
  94. public function message($body, $ishtml = true)
  95. {
  96. $this->options['body'] = $body;
  97. $this->options['ishtml'] = $ishtml;
  98. return $this;
  99. }
  100. /**
  101. * 设置收件人
  102. * @param string $email
  103. * @param string $name
  104. * @return $this
  105. */
  106. public function to($email)
  107. {
  108. $this->options['to'] = $email;
  109. //$this->options['to_name'] = $name;
  110. return $this;
  111. }
  112. //发送邮件
  113. public function send()
  114. {
  115. $result = false;
  116. switch ($this->options['mail_type']) {
  117. case 1:
  118. try
  119. {
  120. //使用phpmailer发送
  121. $this->mail->setFrom($this->options['from'], $this->options['from_name']);
  122. //多个邮箱发送
  123. $this->options['to'] = explode(',', str_replace(',', ',', $this->options['to']));
  124. foreach ($this->options['to'] as $val) {
  125. $this->mail->addAddress($val);
  126. }
  127. $this->mail->Subject = $this->options['subject'];
  128. if ($this->options['ishtml']) {
  129. $this->mail->isHTML(true);
  130. $this->mail->Body = $this->options['body'];
  131. } else {
  132. $this->mail->AltBody = $this->options['body'];
  133. }
  134. $result = $this->mail->send();
  135. } catch (Exception $e) {
  136. $this->setError($e->getMessage());
  137. }
  138. $this->setError($result ? '' : $this->mail->ErrorInfo);
  139. break;
  140. case 2:
  141. //使用mail方法发送邮件
  142. $headers = 'MIME-Version: 1.0' . "\r\n";
  143. $headers .= "Content-type: text/html; charset=" . $this->options['charset'] . "\r\n";
  144. $headers .= "To: {$this->options['to_name']} <{$this->options['to']}>\r\n"; //收件人
  145. $headers .= "From: {$this->options['from_name']} <{$this->options['from']}>\r\n"; //发件人
  146. $result = mail($this->options['to'], $this->options['subject'], $this->options['body'], $headers);
  147. $this->setError($result ? '' : error_get_last()['message']);
  148. break;
  149. default:
  150. $this->setError('邮件功能已关闭');
  151. break;
  152. }
  153. return $result;
  154. }
  155. /**
  156. * 获取最后产生的错误
  157. * @return string
  158. */
  159. public function getError()
  160. {
  161. return $this->_error;
  162. }
  163. /**
  164. * 设置错误
  165. * @param string $error 信息信息
  166. */
  167. protected function setError($error)
  168. {
  169. $this->_error = $error;
  170. }
  171. }