浏览代码

Merge branch 'master' of http://gogs.renlianiot.com:4000/zmcoding/smart-tool-api

tongshanglei 3 年之前
父节点
当前提交
efdfdac800
共有 3 个文件被更改,包括 96 次插入2 次删除
  1. 19 0
      catch/email/controller/Email.php
  2. 74 0
      catch/email/model/Email.php
  3. 3 2
      catch/email/route.php

+ 19 - 0
catch/email/controller/Email.php

@@ -1,4 +1,12 @@
 <?php
+/*
+ * @Descripttion: 
+ * @version: X3版本
+ * @Author: 吴胭脂
+ * @Date: 2022-06-07 10:16:15
+ * @LastEditors: 吴胭脂
+ * @LastEditTime: 2022-06-08 16:01:27
+ */
 
 namespace catchAdmin\email\controller;
 
@@ -33,6 +41,7 @@ class Email extends CatchController
      */
     public function save(Request $request) : \think\Response
     {
+        
         return CatchResponse::success($this->emailModel->storeBy($request->post()));
     }
     
@@ -66,4 +75,14 @@ class Email extends CatchController
     {
         return CatchResponse::success($this->emailModel->deleteBy($id));
     }
+   
+    /**
+     * 发送邮件
+     */
+    public function setEmail()
+    {
+        $array = ['17837032683@139.com'];
+        $this->emailModel->setEmail($array,'警告报警','我还不知道',null);
+    }
+
 }

+ 74 - 0
catch/email/model/Email.php

@@ -1,8 +1,20 @@
 <?php
+/*
+ * @Descripttion: 
+ * @version: X3版本
+ * @Author: 吴胭脂
+ * @Date: 2022-06-07 10:16:16
+ * @LastEditors: 吴胭脂
+ * @LastEditTime: 2022-06-08 15:43:33
+ */
 
 namespace catchAdmin\email\model;
 
 use catcher\base\CatchModel as Model;
+use  PHPMailer\PHPMailer\PHPMailer;
+use  PHPMailer\PHPMailer\Exception;
+use  PHPMailer\PHPMailer\SMTP;
+use think\facade\Db;
 
 class Email extends Model
 {
@@ -26,4 +38,66 @@ class Email extends Model
         // 软删除
         'deleted_at',
     );
+    
+    public function getStatusAttr()
+    {
+        return (string) $this->getData('status');
+    }
+
+
+    /**
+     * 
+     */
+    public function setEmail($emails,$title,$body)
+    {
+        $where= [];
+        $where[] = ['type','=','email_config'];
+        $Host = Db::name("sys_config")->where($where)->where('field','SmtpHost')->value('fieldValue');
+        $Username = Db::name("sys_config")->where($where)->where('field','SmtpUserName')->value('fieldValue');
+        $Password = Db::name("sys_config")->where($where)->where('field','SmtpPassword')->value('fieldValue');
+        $SMTPSecure = Db::name("sys_config")->where($where)->where('field','SMTPSecure')->value('fieldValue');
+        $Port =Db::name("sys_config")->where($where)->where('field','SmtpPort')->value('fieldValue');
+        $Name = Db::name("sys_config")->where($where)->where('field','Name')->value('fieldValue');
+        $mail = new PHPMailer(true);
+        try {
+            //服务器配置
+            $mail->CharSet ="UTF-8";                     //设定邮件编码
+            $mail->SMTPDebug = 0;                        // 调试模式输出
+            $mail->isSMTP();                             // 使用SMTP
+            $mail->Host = trim($Host);                // SMTP服务器
+            $mail->SMTPAuth = true;                      // 允许 SMTP 认证
+            $mail->Username = trim($Username);                // SMTP 用户名  即邮箱的用户名
+            $mail->Password = trim($Password);             // SMTP 密码  部分邮箱是授权码(例如163邮箱)
+            $mail->SMTPSecure = trim($SMTPSecure);                    // 允许 TLS 或者ssl协议
+            $mail->Port = trim($Port);                            // 服务器端口 25 或者465 具体要看邮箱服务器支持
+            $mail->setFrom(trim($Username),$Name);  //发件人
+            //$mail->addAddress('aaaa@126.com', 'Joe');  // 收件人
+            //$mail->addAddress('ellen@example.com');  // 可添加多个收件人
+            //$mail->addReplyTo('xxxx@163.com', 'info'); //回复的时候回复给哪个邮箱 建议和发件人一致
+            //$mail->addCC('cc@example.com');                    //抄送
+            //$mail->addBCC('bcc@example.com');                    //密送
+            foreach($emails as $item)
+            {
+                $mail->addAddress($item);
+            }
+           //发送附件
+            // $mail->addAttachment('../xy.zip');         // 添加附件
+            // $mail->addAttachment('../thumb-1.jpg', 'new.jpg');    // 发送附件并且重命名
+            $mail->isHTML(true);                                  // 是否以HTML文档格式发送  发送后客户端可直接显示对应HTML内容
+            $mail->Subject = '系统警告';
+            $mail->Body    = '<h1>这里是邮件内容</h1>' . date('Y-m-d H:i:s');
+            $mail->AltBody = '如果邮件客户端不支持HTML则显示此内容';
+            $mail->send();
+            echo '邮件发送成功';
+        } catch (Exception $e) {
+            echo '邮件发送失败: ', $mail->ErrorInfo;
+        }
+        
+    }
+
+
+
+
+
+
 }

+ 3 - 2
catch/email/route.php

@@ -12,5 +12,6 @@
 // you should use `$router`
 $router->group(function () use ($router){
 	// email路由
-	$router->resource('email', '\catchAdmin\email\controller\email');
-})->middleware('auth');
+	$router->resource('email', '\catchAdmin\email\controller\email'); 
+})->middleware('auth');
+$router->get("test",'\catchAdmin\email\controller\email@setEmail');