作者 邓超

1

@@ -161,50 +161,32 @@ class Home extends Base { @@ -161,50 +161,32 @@ class Home extends Base {
161 161
162 ]); 162 ]);
163 163
164 - // 邮件对象  
165 - $mail = new PHPMailer();  
166 - //Server settings  
167 - $mail->SMTPDebug = SMTP::DEBUG_OFF;//调试输出 SMTP::DEBUG_SERVER; //Enable verbose debug output  
168 - $mail->isSMTP(); //Send using SMTP  
169 - $mail->Host = $email['smtp']; //Set the SMTP server to send through  
170 - $mail->SMTPAuth = true; //Enable SMTP authentication  
171 - $mail->Username = $email['email']; //SMTP username  
172 - $mail->Password = base64_decode($email['password']); //SMTP password  
173 - $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption  
174 - $mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`  
175 - $mail->CharSet = 'utf-8';  
176 - $mail->Encoding = PHPMailer::ENCODING_QUOTED_PRINTABLE;  
177 -  
178 - //Recipients,设置发件人  
179 - $mail->setFrom($email['email'], $formData['nickname']??'');// 显示邮件来自谁  
180 - // //设置收件人  
181 - foreach ($formData['tos'] as $to){  
182 - $mail->addAddress($to['email'], $to['name']??'');  
183 - }  
184 164
185 -// //回复到那个邮件  
186 -// $mail->addReplyTo($reply_to['email'], $reply_to['name']); //Add a recipient  
187 -// // 抄送  
188 - if(!empty($formData['cc'])){  
189 - foreach ($formData['cc'] as $to){  
190 - $mail->addCC($to['email'], $to['name']??'');  
191 - } 165 + $sendData = [];
  166 + $sendData['email'] = $email['email'];
  167 + $sendData['nickname'] = $formData['nickname']??'';
  168 + $sendData['tos'] = $formData['tos'];
  169 +// 抄送
  170 + if(($formData['isCc']??0) && !empty($formData['cc'])){
  171 + $sendData['cc'] = $formData['cc'];
192 } 172 }
193 // 密送 173 // 密送
194 - if(!empty($formData['bcc'])){  
195 - foreach ($formData['bcc'] as $to){  
196 - $mail->addBCC($to['email'], $to['name']??'');  
197 - } 174 + if(($formData['isBcc']??0) && !empty($formData['bcc'])){
  175 + $sendData['bcc'] = $formData['bcc'];
198 } 176 }
199 -  
200 -  
201 - //Attachments 附件 177 + $sendData['reply_to'] = [];//回复到那个邮件
  178 + //Attachments 附件 上传的
  179 + $sendData['attachment'] = [];
202 $attachment = app()->file('attachment'); 180 $attachment = app()->file('attachment');
203 if($attachment){ 181 if($attachment){
204 foreach ($attachment as $file){ 182 foreach ($attachment as $file){
205 if($file->move()){ 183 if($file->move()){
206 - // 添加到邮箱中  
207 - $mail->addAttachment($file->savePath.$file->saveName, $file->name); //Add attachments 184 + $sendData['attachment'][] = [
  185 + 'name' => $file->name,
  186 + 'filename' => $file->name,
  187 + 'signName' => $file->saveName,
  188 + 'path' => $file->savePath.$file->saveName
  189 + ];
208 }else{ 190 }else{
209 app()->e(['attachment_upload_error',$file->name]); 191 app()->e(['attachment_upload_error',$file->name]);
210 } 192 }
@@ -212,15 +194,18 @@ class Home extends Base { @@ -212,15 +194,18 @@ class Home extends Base {
212 } 194 }
213 // 远程路径,云文件 195 // 远程路径,云文件
214 $attachmentUrl = app()->request('attachmentUrl'); 196 $attachmentUrl = app()->request('attachmentUrl');
215 -  
216 if(is_array($attachmentUrl)){ 197 if(is_array($attachmentUrl)){
217 foreach ($attachmentUrl as $file){ 198 foreach ($attachmentUrl as $file){
218 $file = is_array($file) ? $file : json_decode($file,true); 199 $file = is_array($file) ? $file : json_decode($file,true);
219 if(!empty($file['url']) && !empty($file['name'])){ 200 if(!empty($file['url']) && !empty($file['name'])){
220 $file = new UploadFile($file['name'],$file['url']); 201 $file = new UploadFile($file['name'],$file['url']);
221 if($file->move()){ 202 if($file->move()){
222 - // 添加到邮箱中  
223 - $mail->addAttachment($file->savePath.$file->saveName, $file->name); //Add attachments 203 + $sendData['attachment'][] = [
  204 + 'name' => $file->name,
  205 + 'filename' => $file->name,
  206 + 'signName' => $file->saveName,
  207 + 'path' => $file->savePath.$file->saveName
  208 + ];
224 }else{ 209 }else{
225 app()->e(['attachment_upload_error',$file->name]); 210 app()->e(['attachment_upload_error',$file->name]);
226 } 211 }
@@ -228,26 +213,36 @@ class Home extends Base { @@ -228,26 +213,36 @@ class Home extends Base {
228 213
229 } 214 }
230 } 215 }
  216 + $sendData['receipt'] = empty($formData['receipt']) ? '' : 1;// 回执,阅读后收回执的邮箱
  217 + $sendData['priority'] = $formData['priority']??3;// 是否紧急邮件
  218 + $sendData['subject'] = $formData['subject'];// //Content 主题,标题
  219 + $sendData['body'] = $formData['body'];
  220 +
  221 + // 定时发送时间
  222 + $timer = strtotime(app()->request('timerValue','2023-04-10'));
  223 + // 是否存草稿
  224 + if(app()->request('saveType')=='draft'){
  225 + // 保存
  226 + $draftid = listsSql::saveDraft($sendData,$email,app()->request('draft_id',0,'intval'));
  227 + // 保存失败
  228 + if($draftid){
  229 + app()->_json(['draft_id'=>$draftid]);
  230 + }
  231 + app()->e('save_draft_error');
231 232
232 - // 回执,阅读后收回执的邮箱  
233 - if(!empty($formData['receipt'])){  
234 - $mail->ConfirmReadingTo = true;  
235 } 233 }
236 - // 是否紧急邮件  
237 -// Options: null (default), 1 = High, 3 = Normal, 5 = low.  
238 - $mail->Priority = $formData['priority']??3;  
239 -  
240 - //Content 主题,标题  
241 - $mail->Subject = $formData['subject']; 234 + // 定时发送
  235 + else if(app()->request('timer') && $timer > time()){
242 236
243 - $mail->isHTML(true); //Set email format to HTML  
244 - $mail->Body = $formData['body'];// html格式的内容  
245 -  
246 - // 发送  
247 - if($mail->send()){  
248 - app()->_json(['messageId' => $mail->getLastMessageID()]); 237 + }else{
  238 + // 发送
  239 +// $result = MailFun::sendEmail($sendData,$email);
  240 +// if($result[0]){
  241 +// app()->_json(['messageId' => $result[1]]);
  242 +// }
  243 + // 错误
  244 + app()->e($result[1]);
249 } 245 }
250 - app()->e($mail->ErrorInfo);  
251 246
252 } 247 }
253 248
@@ -292,6 +287,12 @@ class Home extends Base { @@ -292,6 +287,12 @@ class Home extends Base {
292 $this->setFlags('seen'); 287 $this->setFlags('seen');
293 } 288 }
294 289
  290 + /**
  291 + * 是否已回复
  292 + * @throws \Lib\Err
  293 + * @author:dc
  294 + * @time 2023/4/10 16:30
  295 + */
295 public function answered_2_unanswered(){ 296 public function answered_2_unanswered(){
296 $this->setFlags('answered'); 297 $this->setFlags('answered');
297 } 298 }
@@ -79,7 +79,10 @@ return [ @@ -79,7 +79,10 @@ return [
79 79
80 80
81 81
82 - 'upload_file_load_error' => '上传文件加载失败' 82 + 'upload_file_load_error' => '上传文件加载失败',
  83 +
  84 + 'save_draft_error' => '草稿保存失败',
  85 +
83 86
84 87
85 88
@@ -361,16 +361,10 @@ class Mail { @@ -361,16 +361,10 @@ class Mail {
361 361
362 } 362 }
363 363
364 - try {  
365 - $db->insert(bodySql::$table,[  
366 - 'lists_id' => $id,  
367 - 'text_html' => $body // todo::因为邮件会出现多编码问题,会导致数据库写不进去  
368 - ],false);  
369 - }catch (\Throwable $e){  
370 - $db->update(bodySql::$table,[  
371 - 'text_html' => $body // todo::因为邮件会出现多编码问题,会导致数据库写不进去  
372 - ],dbWhere(['lists_id' => $id]),false);  
373 - } 364 + bodySql::insertOrUpdate($db,[
  365 + 'lists_id' => $id,
  366 + 'text_html' => $body // todo::因为邮件会出现多编码问题,会导致数据库写不进去
  367 + ]);
374 368
375 369
376 // 更新描述 370 // 更新描述
@@ -2,6 +2,10 @@ @@ -2,6 +2,10 @@
2 namespace Lib\Mail; 2 namespace Lib\Mail;
3 3
4 4
  5 +use Lib\UploadFile;
  6 +use Model\bodySql;
  7 +use Model\folderSql;
  8 +use Model\listsSql;
5 use PHPMailer\PHPMailer\PHPMailer; 9 use PHPMailer\PHPMailer\PHPMailer;
6 use PHPMailer\PHPMailer\SMTP; 10 use PHPMailer\PHPMailer\SMTP;
7 11
@@ -105,75 +109,80 @@ class MailFun { @@ -105,75 +109,80 @@ class MailFun {
105 109
106 /** 110 /**
107 * 发送邮件 111 * 发送邮件
108 - * @param string $smtp smtp服务器地址  
109 - * @param string $username 发件人  
110 - * @param string $password 发件人密码  
111 - * @param string $nickname 昵称  
112 - * @param string|array $to_email 收件人,邮件或['email'=>'','name'=>'']  
113 - * @param string $subject 标题,主题  
114 - * @param string $body 文本内容  
115 - * @param array $files 文件 ['origin_name'=>'','path'=>'']  
116 - * @param false $receipt 是否回执  
117 - * @param int $priority 是否紧急 1紧急 3正常 5慢 112 + * @param array $data 数据
  113 + * @param array $email 邮箱信息
118 * @return array 114 * @return array
  115 + * @throws \Lib\Err
119 * @throws \PHPMailer\PHPMailer\Exception 116 * @throws \PHPMailer\PHPMailer\Exception
120 * @author:dc 117 * @author:dc
121 - * @time 2023/2/18 17:45 118 + * @time 2023/4/11 9:12
122 */ 119 */
123 - public static function sendEmail(string $smtp,string $username,string $password,string $nickname,string|array $to_email,string $subject,string $body,$files=[],$receipt=false,$priority=3){ 120 + public static function sendEmail(array $data, array $email){
124 121
125 // 邮件对象 122 // 邮件对象
126 $mail = new PHPMailer(); 123 $mail = new PHPMailer();
127 //Server settings 124 //Server settings
128 $mail->SMTPDebug = SMTP::DEBUG_OFF;//调试输出 SMTP::DEBUG_SERVER; //Enable verbose debug output 125 $mail->SMTPDebug = SMTP::DEBUG_OFF;//调试输出 SMTP::DEBUG_SERVER; //Enable verbose debug output
129 $mail->isSMTP(); //Send using SMTP 126 $mail->isSMTP(); //Send using SMTP
130 - $mail->Host = $smtp; //Set the SMTP server to send through 127 + $mail->Host = $email['smtp']; //Set the SMTP server to send through
131 $mail->SMTPAuth = true; //Enable SMTP authentication 128 $mail->SMTPAuth = true; //Enable SMTP authentication
132 - $mail->Username = $username; //SMTP username  
133 - $mail->Password = $password; //SMTP password 129 + $mail->Username = $email['email']; //SMTP username
  130 + $mail->Password = base64_decode($email['password']); //SMTP password
134 $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption 131 $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
135 $mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS` 132 $mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
136 $mail->CharSet = 'utf-8'; 133 $mail->CharSet = 'utf-8';
137 $mail->Encoding = PHPMailer::ENCODING_QUOTED_PRINTABLE; 134 $mail->Encoding = PHPMailer::ENCODING_QUOTED_PRINTABLE;
138 135
139 //Recipients,设置发件人 136 //Recipients,设置发件人
140 - $mail->setFrom($username, $nickname);// 显示邮件来自谁  
141 - // //Add a recipient,设置收件人 这里必须是一对一发送  
142 - if(is_array($to_email)){  
143 - $mail->addAddress($to_email['email'], $to_email['name']);  
144 - }else{  
145 - $mail->addAddress($to_email, ''); 137 + $mail->setFrom($email['email'], $data['nickname']??'');// 显示邮件来自谁
  138 + // //设置收件人
  139 + foreach ($data['tos'] as $to){
  140 + $mail->addAddress($to['email'], $to['name']??'');
146 } 141 }
147 142
148 -// //回复到那个邮件  
149 -// $mail->addAddress($reply_to['email'], $reply_to['name']); //Add a recipient  
150 -// // 抄送  
151 -// $mail->addCC($cc['email'],$cc['name']);//  
152 -// // 密送  
153 -// $mail->addBCC($bcc['email'],$bcc['name']); 143 + //回复到那个邮件
  144 + if(!empty($data['reply_to'])){
  145 + if(is_string($data['reply_to'])){
  146 + $mail->addReplyTo($data['reply_to']);
  147 + }else{
  148 + $mail->addReplyTo($data['reply_to']['email'], $data['reply_to']['name']); //Add a recipient
  149 + }
  150 + }
154 151
  152 + // 抄送
  153 + if(!empty($data['cc'])){
  154 + foreach ($data['cc'] as $to){
  155 + $mail->addCC($to['email'], $to['name']??'');
  156 + }
  157 + }
  158 + // 密送
  159 + if(!empty($data['bcc'])){
  160 + foreach ($data['bcc'] as $to){
  161 + $mail->addBCC($to['email'], $to['name']??'');
  162 + }
  163 + }
155 164
156 //Attachments 附件 165 //Attachments 附件
157 - if($files){  
158 - foreach ($files as $file){  
159 - // 添加到邮箱中  
160 - $mail->addAttachment($file['path'], $file['origin_name']); //Add attachments 166 + if(!empty($data['attachment'])){
  167 + foreach ($data['attachment'] as $file){
  168 + $mail->addAttachment($file['path'], $file['filename']);
161 } 169 }
162 } 170 }
163 171
164 // 回执,阅读后收回执的邮箱 172 // 回执,阅读后收回执的邮箱
165 - if($receipt){  
166 - $mail->ConfirmReadingTo = $receipt; 173 + if(!empty($data['receipt'])){
  174 + $mail->ConfirmReadingTo = true;
167 } 175 }
  176 +
168 // 是否紧急邮件 177 // 是否紧急邮件
169 // Options: null (default), 1 = High, 3 = Normal, 5 = low. 178 // Options: null (default), 1 = High, 3 = Normal, 5 = low.
170 - $mail->Priority = $priority; 179 + $mail->Priority = $data['priority']??3;
171 180
172 //Content 主题,标题 181 //Content 主题,标题
173 - $mail->Subject = $subject; 182 + $mail->Subject = $data['subject'];
174 183
175 $mail->isHTML(true); //Set email format to HTML 184 $mail->isHTML(true); //Set email format to HTML
176 - $mail->Body = $body;// html格式的内容 185 + $mail->Body = $data['body'];// html格式的内容
177 186
178 // 发送 187 // 发送
179 if($mail->send()){ 188 if($mail->send()){
@@ -2,6 +2,8 @@ @@ -2,6 +2,8 @@
2 2
3 namespace Model; 3 namespace Model;
4 4
  5 +use Lib\DbPool;
  6 +
5 /** 7 /**
6 * body 8 * body
7 * @author:dc 9 * @author:dc
@@ -37,5 +39,21 @@ class bodySql { @@ -37,5 +39,21 @@ class bodySql {
37 } 39 }
38 40
39 41
  42 + /**
  43 + * 插入或者更新
  44 + * @param DbPool $db
  45 + * @param $data
  46 + * @return int
  47 + * @author:dc
  48 + * @time 2023/4/10 17:30
  49 + */
  50 + public static function insertOrUpdate(DbPool $db,$data){
  51 + try {
  52 + return $db->insert(bodySql::$table,$data,false);
  53 + }catch (\Throwable $e){
  54 + return $db->update(bodySql::$table,$data,dbWhere(['lists_id' => $data['lists_id']]),false);
  55 + }
  56 + }
  57 +
40 58
41 } 59 }
@@ -100,5 +100,72 @@ class listsSql { @@ -100,5 +100,72 @@ class listsSql {
100 } 100 }
101 101
102 102
  103 + /**
  104 + * 存草稿
  105 + * @param array $data
  106 + * @param array $email
  107 + * @param int $draftid
  108 + * @return int
  109 + * @author:dc
  110 + * @time 2023/4/11 9:44
  111 + */
  112 + public static function saveDraft(array $data, array $email,int $draftid = 0):int {
  113 + $draftData = [
  114 + 'subject' => $data['subject'],
  115 + 'from' => $data['email'],
  116 + 'from_name' => $data['nickname'],
  117 + 'date' => time(),
  118 + 'udate' => time(),
  119 + 'draft' => 1,
  120 + 'folder_id' => db()->value(folderSql::first(['folder'=>'草稿箱','email_id'=>$email['id']],'`id`')),
  121 + 'email_id' => $email['id'],
  122 + 'is_file' => $data['attachment'] ? 1 : 0, //是否附件
  123 + ];
  124 + $draftData['to'] = $data['tos'][0]['email'];
  125 + $draftData['to_name'] = $data['tos'];
  126 + $draftData['cc'] = $data['cc'];
  127 + $draftData['bcc'] = $data['bcc'];
  128 +
  129 + $draftData['uuid'] = md5($draftData['email_id'].$draftData['folder_id'].rand(1111111,9999999999));
  130 +
  131 + if($draftid){
  132 + // 修改
  133 + if(!db()->update(listsSql::$table,$draftData,dbWhere(
  134 + [
  135 + 'id' => $draftid,
  136 + 'email_id' => $draftData['email_id']
  137 + ]
  138 + ))){
  139 + return 0;
  140 + }
  141 + }else{
  142 + $draftid = db()->insert(listsSql::$table,$draftData);
  143 + }
  144 +
  145 + if($draftid){
  146 + $draftBody = [
  147 + [
  148 + 'type' => 'text/html',
  149 + 'charset' => 'utf-8',
  150 + 'body' => base64_encode($data['body'])
  151 + ]
  152 + ];
  153 +
  154 + foreach ($data['attachment'] as $da){
  155 + $draftBody[] = $da;
  156 + }
  157 +
  158 + bodySql::insertOrUpdate(db(),[
  159 + 'lists_id' => $draftid,
  160 + 'text_html' => $draftBody
  161 + ]);
  162 +
  163 + return $draftid;
  164 +
  165 + }else{
  166 + return 0;
  167 + }
  168 + }
  169 +
103 170
104 } 171 }
  1 +<?php
  2 +
  3 +namespace Model;
  4 +
  5 +/**
  6 + * 邮件发送任务状态
  7 + * @author:dc
  8 + * @time 2023/4/10 16:27
  9 + * Class sendJobStatusSql
  10 + * @package Model
  11 + */
  12 +class sendJobStatusSql {
  13 +
  14 + /**
  15 + * 表
  16 + * @var string
  17 + */
  18 + public static $table = 'lists';
  19 +
  20 +
  21 +
  22 +
  23 +
  24 +}
  1 +<?php
  2 +
  3 +namespace Model;
  4 +
  5 +/**
  6 + * 邮件发送任务
  7 + * @author:dc
  8 + * @time 2023/4/10 16:28
  9 + * Class sendJobsSql
  10 + * @package Model
  11 + */
  12 +class sendJobsSql {
  13 +
  14 + /**
  15 + * 表
  16 + * @var string
  17 + */
  18 + public static $table = 'lists';
  19 +
  20 +
  21 +
  22 +
  23 +
  24 +}