Email.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * 邮件类
  4. * 邮件操作类
  5. */
  6. namespace sendmsg;
  7. use PHPMailer\PHPMailer\PHPMailer;
  8. use PHPMailer\PHPMailer\Exception;
  9. require root_path(). 'vendor/phpmailer/phpmailer/src/Exception.php';
  10. require root_path(). 'vendor/phpmailer/phpmailer/src/PHPMailer.php';
  11. require root_path(). 'vendor/phpmailer/phpmailer/src/SMTP.php';
  12. final class Email {
  13. /**
  14. * 邮件服务器
  15. */
  16. private $email_server;
  17. /**
  18. * 协议
  19. */
  20. private $email_secure;
  21. /**
  22. * 端口
  23. */
  24. private $email_port;
  25. /**
  26. * 账号
  27. */
  28. private $email_user;
  29. /**
  30. * 密码
  31. */
  32. private $email_password;
  33. /**
  34. * 发送邮箱
  35. */
  36. private $email_from;
  37. /**
  38. * 间隔符
  39. */
  40. private $email_delimiter = "\n";
  41. /**
  42. * 站点名称
  43. */
  44. private $site_name;
  45. public function get($key) {
  46. if (!empty($this->$key)) {
  47. return $this->$key;
  48. } else {
  49. return false;
  50. }
  51. }
  52. public function set($key, $value) {
  53. if (!isset($this->$key)) {
  54. $this->$key = $value;
  55. return true;
  56. } else {
  57. return false;
  58. }
  59. }
  60. /**
  61. * 发送邮件
  62. *
  63. * @param string $email_to 发送对象邮箱地址
  64. * @param string $subject 邮件标题
  65. * @param string $message 邮件内容
  66. * @param string $from 页头来源内容
  67. * @return bool 布尔形式的返回结果
  68. */
  69. public function send($email_to, $subject, $message, $from = '') {
  70. if (empty($email_to))
  71. return false;
  72. $subject = $this->subject($subject);
  73. $message = $this->html($subject, $message);
  74. $mail = new PHPMailer;
  75. //Server settings
  76. $mail->SMTPDebug = 0; // Enable verbose debug output
  77. $mail->isSMTP(); // Set mailer to use SMTP
  78. $mail->Host = $this->email_server; // Specify main and backup SMTP servers
  79. $mail->SMTPAuth = true; // Enable SMTP authentication
  80. $mail->Username = $this->email_from; // SMTP username
  81. $mail->Password = $this->email_password; // SMTP password
  82. $mail->SMTPSecure = $this->email_secure; // Enable TLS encryption, `ssl` also accepted
  83. $mail->Port = $this->email_port; // TCP port to connect to
  84. //Recipients
  85. $mail->setFrom($this->email_from, $this->site_name);
  86. $mail->addAddress($email_to); // Add a recipient
  87. // $mail->addReplyTo('info@example.com', 'Information');
  88. // $mail->addCC('cc@example.com');
  89. // $mail->addBCC('bcc@example.com');
  90. //Attachments
  91. // $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
  92. // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
  93. //Content
  94. $mail->isHTML(true); // Set email format to HTML
  95. $mail->Subject = $subject;
  96. $mail->Body = $message;
  97. $mail->CharSet ="UTF-8";
  98. // $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
  99. $result = $mail->send();
  100. return $result;
  101. }
  102. public function send_sys_email($email_to, $subject, $message) {
  103. $this->set('email_server', config('ds_config.email_host'));
  104. $this->set('email_secure', config('ds_config.email_secure'));
  105. $this->set('email_port', config('ds_config.email_port'));
  106. $this->set('email_user', config('ds_config.email_id'));
  107. $this->set('email_password', config('ds_config.email_pass'));
  108. $this->set('email_from', config('ds_config.email_addr'));
  109. $this->set('site_name', config('ds_config.site_name'));
  110. $result = $this->send($email_to, $subject, $message);
  111. return $result;
  112. }
  113. /**
  114. * 内容:邮件主体
  115. *
  116. * @param string $subject 邮件标题
  117. * @param string $message 邮件内容
  118. * @return string 字符串形式的返回结果
  119. */
  120. private function html($subject, $message) {
  121. $message = preg_replace("/href\=\"(?!http\:\/\/)(.+?)\"/i", 'href="\\1"', $message);
  122. $tmp = "<html><head>";
  123. $tmp .= '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
  124. $tmp .= "<title>" . $subject . "</title>";
  125. $tmp .= "</head><body>" . $message . "</body></html>";
  126. $message = $tmp;
  127. unset($tmp);
  128. return $message;
  129. }
  130. /**
  131. * 内容:邮件标题
  132. *
  133. * @param string $subject 邮件标题
  134. * @return string 字符串形式的返回结果
  135. */
  136. private function subject($subject) {
  137. $subject = '=?' . CHARSET . '?B?' . base64_encode(preg_replace("/[\r|\n]/", '', '[' . $this->site_name . '] ' . $subject)) . '?=';
  138. return $subject;
  139. }
  140. }