控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  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->isSMTP();
  47. $this->mail->Host = $this->options['mail_smtp_host']; //SMTP服务器
  48. // 设置为“需要验证”
  49. if ($this->options['mail_auth']) {
  50. $this->mail->SMTPAuth = true;
  51. } else {
  52. $this->mail->SMTPAuth = false;
  53. }
  54. $this->mail->Username = $this->options['mail_smtp_user']; //SMTP username
  55. $this->mail->Password = $this->options['mail_smtp_pass']; // SMTP password
  56. $this->mail->SMTPSecure = isset($securArr[$this->options['mail_verify_type']]) ? $securArr[$this->options['mail_verify_type']] : ''; //支持TLS加密,还接受了ssl
  57. $this->mail->Port = $this->options['mail_smtp_port']; //TCP端口连接
  58. //设置发件人
  59. $this->from($this->options['mail_from']);
  60. }
  61. /**
  62. * 设置邮件主题
  63. * @param string $subject
  64. * @return $this
  65. */
  66. public function subject($subject)
  67. {
  68. $this->options['subject'] = $subject;
  69. return $this;
  70. }
  71. /**
  72. * 设置发件人
  73. * @param string $email
  74. * @param string $name
  75. * @return $this
  76. */
  77. public function from($email, $name = '')
  78. {
  79. $this->options['from'] = $email;
  80. $this->options['from_name'] = $name;
  81. return $this;
  82. }
  83. /**
  84. * 设置邮件正文
  85. * @param string $body
  86. * @param boolean $ishtml
  87. * @return $this
  88. */
  89. public function message($body, $ishtml = true)
  90. {
  91. $this->options['body'] = $body;
  92. $this->options['ishtml'] = $ishtml;
  93. return $this;
  94. }
  95. /**
  96. * 设置收件人
  97. * @param string $email
  98. * @param string $name
  99. * @return $this
  100. */
  101. public function to($email)
  102. {
  103. $this->options['to'] = $email;
  104. //$this->options['to_name'] = $name;
  105. return $this;
  106. }
  107. //发送邮件
  108. public function send()
  109. {
  110. $result = false;
  111. switch ($this->options['mail_type']) {
  112. case 1:
  113. try
  114. {
  115. //使用phpmailer发送
  116. $this->mail->setFrom($this->options['from'], $this->options['from_name']);
  117. //多个邮箱发送
  118. $this->options['to'] = explode(',', str_replace(',', ',', $this->options['to']));
  119. foreach ($this->options['to'] as $val) {
  120. $this->mail->addAddress($val);
  121. }
  122. $this->mail->Subject = $this->options['subject'];
  123. if ($this->options['ishtml']) {
  124. $this->mail->isHTML(true);
  125. $this->mail->Body = $this->options['body'];
  126. } else {
  127. $this->mail->AltBody = $this->options['body'];
  128. }
  129. $result = $this->mail->send();
  130. } catch (Exception $e) {
  131. $this->setError($e->getMessage());
  132. }
  133. $this->setError($result ? '' : $this->mail->ErrorInfo);
  134. break;
  135. case 2:
  136. //使用mail方法发送邮件
  137. $headers = 'MIME-Version: 1.0' . "\r\n";
  138. $headers .= "Content-type: text/html; charset=" . $this->options['charset'] . "\r\n";
  139. $headers .= "To: {$this->options['to_name']} <{$this->options['to']}>\r\n"; //收件人
  140. $headers .= "From: {$this->options['from_name']} <{$this->options['from']}>\r\n"; //发件人
  141. $result = mail($this->options['to'], $this->options['subject'], $this->options['body'], $headers);
  142. $this->setError($result ? '' : error_get_last()['message']);
  143. break;
  144. default:
  145. $this->setError('邮件功能已关闭');
  146. break;
  147. }
  148. return $result;
  149. }
  150. /**
  151. * 获取最后产生的错误
  152. * @return string
  153. */
  154. public function getError()
  155. {
  156. return $this->_error;
  157. }
  158. /**
  159. * 设置错误
  160. * @param string $error 信息信息
  161. */
  162. protected function setError($error)
  163. {
  164. $this->_error = $error;
  165. }
  166. }