Email.php 3.8 KB

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