Email.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace email;
  3. use PHPMailer\PHPMailer\PHPMailer;
  4. use PHPMailer\PHPMailer\Exception;
  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' => false, //调式模式
  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 = sysconfig('mail')) {
  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();
  51. $this->mail->CharSet = $this->options['charset'];
  52. if ($this->options['mail_type'] == 1) {
  53. $this->mail->SMTPDebug = $this->options['debug'];
  54. $this->mail->isSMTP();
  55. $this->mail->SMTPAuth = true;
  56. } else {
  57. $this->mail->isMail();
  58. }
  59. $this->mail->Host = $this->options['mail_smtp_host'];
  60. $this->mail->Username = $this->options['mail_from'];
  61. $this->mail->Password = $this->options['mail_smtp_pass'];
  62. $this->mail->SMTPSecure = isset($securArr[$this->options['mail_verify_type']]) ? $securArr[$this->options['mail_verify_type']] : '';
  63. $this->mail->Port = $this->options['mail_smtp_port'];
  64. //设置发件人
  65. $this->from($this->options['mail_from'], $this->options['mail_smtp_user']);
  66. }
  67. /**
  68. * 设置邮件主题
  69. * @param string $subject 邮件主题
  70. * @return $this
  71. */
  72. public function subject($subject)
  73. {
  74. $this->mail->Subject = $subject;
  75. return $this;
  76. }
  77. /**
  78. * 设置发件人
  79. * @param string $email 发件人邮箱
  80. * @param string $name 发件人名称
  81. * @return $this
  82. */
  83. public function from($email, $name = '')
  84. {
  85. $this->mail->setFrom($email, $name);
  86. return $this;
  87. }
  88. /**
  89. * 设置收件人
  90. * @param mixed $email 收件人,多个收件人以,进行分隔
  91. * @param string $name 收件人名称
  92. * @return $this
  93. */
  94. public function to($email, $name = '')
  95. {
  96. $emailArr = $this->buildAddress($email);
  97. foreach ($emailArr as $address => $name) {
  98. $this->mail->addAddress($address, $name);
  99. }
  100. return $this;
  101. }
  102. /**
  103. * 设置抄送
  104. * @param mixed $email 收件人,多个收件人以,进行分隔
  105. * @param string $name 收件人名称
  106. * @return Email
  107. */
  108. public function cc($email, $name = '')
  109. {
  110. $emailArr = $this->buildAddress($email);
  111. foreach ($emailArr as $address => $name) {
  112. $this->mail->addCC($address, $name);
  113. }
  114. return $this;
  115. }
  116. /**
  117. * 设置密送
  118. * @param mixed $email 收件人,多个收件人以,进行分隔
  119. * @param string $name 收件人名称
  120. * @return Email
  121. */
  122. public function bcc($email, $name = '')
  123. {
  124. $emailArr = $this->buildAddress($email);
  125. foreach ($emailArr as $address => $name) {
  126. $this->mail->addBCC($address, $name);
  127. }
  128. return $this;
  129. }
  130. /**
  131. * 设置邮件正文
  132. * @param string $body 邮件下方
  133. * @param boolean $ishtml 是否HTML格式
  134. * @return $this
  135. */
  136. public function message($body, $ishtml = true)
  137. {
  138. if ($ishtml) {
  139. $this->mail->msgHTML($body);
  140. } else {
  141. $this->mail->Body = $body;
  142. }
  143. return $this;
  144. }
  145. /**
  146. * 添加附件
  147. * @param string $path 附件路径
  148. * @param string $name 附件名称
  149. * @return Email
  150. */
  151. public function attachment($path, $name = '')
  152. {
  153. $this->mail->addAttachment($path, $name);
  154. return $this;
  155. }
  156. /**
  157. * 构建Email地址
  158. * @param mixed $emails Email数据
  159. * @return array
  160. */
  161. protected function buildAddress($emails)
  162. {
  163. $emails = is_array($emails) ? $emails : explode(',', str_replace(";", ",", $emails));
  164. $result = [];
  165. foreach ($emails as $key => $value) {
  166. $email = is_numeric($key) ? $value : $key;
  167. $result[$email] = is_numeric($key) ? "" : $value;
  168. }
  169. return $result;
  170. }
  171. /**
  172. * 获取最后产生的错误
  173. * @return string
  174. */
  175. public function getError()
  176. {
  177. return $this->_error;
  178. }
  179. /**
  180. * 设置错误
  181. * @param string $error 信息信息
  182. */
  183. protected function setError($error)
  184. {
  185. $this->_error = $error;
  186. }
  187. /**
  188. * 发送邮件
  189. * @return boolean
  190. */
  191. public function send()
  192. {
  193. $result = false;
  194. if (in_array($this->options['mail_type'], [1, 2])) {
  195. try {
  196. $result = $this->mail->send();
  197. } catch (\PHPMailer\PHPMailer\Exception $e) {
  198. $this->setError($e->getMessage());
  199. }
  200. $this->setError($result ? '' : $this->mail->ErrorInfo);
  201. } else {
  202. //邮件功能已关闭
  203. $this->setError('邮件功能已关闭');
  204. }
  205. return $result;
  206. }
  207. }