Email.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace catchAdmin\email\model;
  3. use catcher\base\CatchModel as Model;
  4. use PHPMailer\PHPMailer\PHPMailer;
  5. use PHPMailer\PHPMailer\Exception;
  6. use PHPMailer\PHPMailer\SMTP;
  7. use think\facade\Db;
  8. class Email extends Model
  9. {
  10. // 表名
  11. public $name = 'email';
  12. // 数据库字段映射
  13. public $field = array(
  14. 'id',
  15. // 邮箱
  16. 'email',
  17. // 1 有效 -1 报废
  18. 'status',
  19. // 详情
  20. 'info',
  21. // 创建人ID
  22. 'creator_id',
  23. // 创建时间
  24. 'created_at',
  25. // 更新时间
  26. 'updated_at',
  27. // 软删除
  28. 'deleted_at',
  29. );
  30. public function getStatusAttr()
  31. {
  32. return (string) $this->getData('status');
  33. }
  34. public function setEmail($emails, $title, $body)
  35. {
  36. $where = [];
  37. $where[] = ['type', '=', 'email_config'];
  38. $Host = Db::name("sys_config")->where($where)->where('field', 'SmtpHost')->value('fieldValue');
  39. $Username = Db::name("sys_config")->where($where)->where('field', 'SmtpUserName')->value('fieldValue');
  40. $Password = Db::name("sys_config")->where($where)->where('field', 'SmtpPassword')->value('fieldValue');
  41. $SMTPSecure = Db::name("sys_config")->where($where)->where('field', 'SMTPSecure')->value('fieldValue');
  42. $Port = Db::name("sys_config")->where($where)->where('field', 'SmtpPort')->value('fieldValue');
  43. $Name = Db::name("sys_config")->where($where)->where('field', 'Name')->value('fieldValue');
  44. $mail = new PHPMailer(true);
  45. try {
  46. //服务器配置
  47. $mail->CharSet = "UTF-8"; //设定邮件编码
  48. $mail->SMTPDebug = 0; // 调试模式输出
  49. $mail->isSMTP(); // 使用SMTP
  50. $mail->Host = trim($Host); // SMTP服务器
  51. $mail->SMTPAuth = true; // 允许 SMTP 认证
  52. $mail->Username = trim($Username); // SMTP 用户名 即邮箱的用户名
  53. $mail->Password = trim($Password); // SMTP 密码 部分邮箱是授权码(例如163邮箱)
  54. $mail->SMTPSecure = trim($SMTPSecure); // 允许 TLS 或者ssl协议
  55. $mail->Port = trim($Port); // 服务器端口 25 或者465 具体要看邮箱服务器支持
  56. $mail->setFrom(trim($Username), $Name); //发件人
  57. //$mail->addAddress('aaaa@126.com', 'Joe'); // 收件人
  58. //$mail->addAddress('ellen@example.com'); // 可添加多个收件人
  59. //$mail->addReplyTo('xxxx@163.com', 'info'); //回复的时候回复给哪个邮箱 建议和发件人一致
  60. //$mail->addCC('cc@example.com'); //抄送
  61. //$mail->addBCC('bcc@example.com'); //密送
  62. foreach ($emails as $item) {
  63. $mail->addAddress($item);
  64. }
  65. //发送附件
  66. // $mail->addAttachment('../xy.zip'); // 添加附件
  67. // $mail->addAttachment('../thumb-1.jpg', 'new.jpg'); // 发送附件并且重命名
  68. $mail->isHTML(true); // 是否以HTML文档格式发送 发送后客户端可直接显示对应HTML内容
  69. $mail->Subject = $title;
  70. $mail->Body = $body;
  71. $mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';
  72. $mail->send();
  73. echo '邮件发送成功';
  74. } catch (Exception $e) {
  75. echo '邮件发送失败: ', $mail->ErrorInfo;
  76. }
  77. }
  78. }