Email.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace mall\utils;
  10. use PHPMailer\PHPMailer\PHPMailer;
  11. use PHPMailer\PHPMailer\SMTP;
  12. use PHPMailer\PHPMailer\Exception;
  13. use think\Db;
  14. class Email {
  15. public static function send($name="",$subject="",$body=""){
  16. $content = Db::name("setting")->where(["name"=>"email"])->value("value");
  17. $setting = json_decode($content,true);
  18. $mail = new PHPMailer(true);
  19. try {
  20. $mail->SMTPDebug = SMTP::DEBUG_OFF;
  21. $mail->isSMTP();
  22. $mail->Host = $setting["address"];
  23. $mail->SMTPAuth = true;
  24. $mail->Username = $setting["username"];
  25. $mail->Password = $setting["password"];
  26. $mail->SMTPSecure = $setting["is_ssl"] ? PHPMailer::ENCRYPTION_SMTPS : PHPMailer::ENCRYPTION_STARTTLS;
  27. $mail->Port = $setting["port"];
  28. //Recipients
  29. $mail->setFrom($setting["smtp_send"], $setting["smtp_name"]);
  30. $arr = explode(",",$name);
  31. foreach($arr as $val){
  32. $mail->addAddress($val);
  33. }
  34. // Content
  35. $mail->isHTML(true);
  36. $mail->Subject = $subject;
  37. $mail->Body = $body;
  38. $mail->send();
  39. } catch (\Exception $e) {
  40. throw new \Exception("Message could not be sent. Mailer Error: {$mail->ErrorInfo}");
  41. }
  42. return true;
  43. }
  44. }