作者 邓超

1

@@ -9,6 +9,8 @@ use Model\bodySql; @@ -9,6 +9,8 @@ use Model\bodySql;
9 use Model\emailSql; 9 use Model\emailSql;
10 use Model\folderSql; 10 use Model\folderSql;
11 use Model\listsSql; 11 use Model\listsSql;
  12 +use PHPMailer\PHPMailer\PHPMailer;
  13 +use PHPMailer\PHPMailer\SMTP;
12 14
13 15
14 /** 16 /**
@@ -149,7 +151,7 @@ class Home extends Base { @@ -149,7 +151,7 @@ class Home extends Base {
149 'attachment|'.__('files_email') => [ 151 'attachment|'.__('files_email') => [
150 'file'=>[ 152 'file'=>[
151 'ext' => [], 153 'ext' => [],
152 - 'size' => 500, 154 + 'size' => 1024*1024*5,
153 'mine' => [] 155 'mine' => []
154 ] 156 ]
155 ], 157 ],
@@ -158,24 +160,76 @@ class Home extends Base { @@ -158,24 +160,76 @@ class Home extends Base {
158 160
159 ]); 161 ]);
160 162
161 - $ret = MailFun::sendEmail(  
162 - $email['smtp'],  
163 - $email['email'],  
164 - base64_decode($email['password']),  
165 - $formData['nickname']??'',  
166 - $formData['tos'],  
167 - $formData['subject'],  
168 - $formData['body'],  
169 - $formData['attachment']??'',  
170 - $formData['receipt']??'',  
171 - $formData['priority']??3,  
172 - ); 163 + // 邮件对象
  164 + $mail = new PHPMailer();
  165 + //Server settings
  166 + $mail->SMTPDebug = SMTP::DEBUG_OFF;//调试输出 SMTP::DEBUG_SERVER; //Enable verbose debug output
  167 + $mail->isSMTP(); //Send using SMTP
  168 + $mail->Host = $email['smtp']; //Set the SMTP server to send through
  169 + $mail->SMTPAuth = true; //Enable SMTP authentication
  170 + $mail->Username = $email['email']; //SMTP username
  171 + $mail->Password = base64_decode($email['password']); //SMTP password
  172 + $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
  173 + $mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
  174 + $mail->CharSet = 'utf-8';
  175 + $mail->Encoding = PHPMailer::ENCODING_QUOTED_PRINTABLE;
  176 +
  177 + //Recipients,设置发件人
  178 + $mail->setFrom($email['email'], $formData['nickname']??'');// 显示邮件来自谁
  179 + // //设置收件人
  180 + foreach ($formData['tos'] as $to){
  181 + $mail->addAddress($to['email'], $to['name']??'');
  182 + }
173 183
174 - if($ret[0]===true) {  
175 - app()->_json(['messageId'=>$ret[1]]);  
176 - }else {  
177 - app()->e($ret[1]); 184 +// //回复到那个邮件
  185 +// $mail->addReplyTo($reply_to['email'], $reply_to['name']); //Add a recipient
  186 +// // 抄送
  187 + if(!empty($formData['cc'])){
  188 + foreach ($formData['cc'] as $to){
  189 + $mail->addCC($to['email'], $to['name']??'');
  190 + }
  191 + }
  192 + // 密送
  193 + if(!empty($formData['bcc'])){
  194 + foreach ($formData['bcc'] as $to){
  195 + $mail->addBCC($to['email'], $to['name']??'');
  196 + }
178 } 197 }
  198 +
  199 +
  200 + //Attachments 附件
  201 + $attachment = app()->file('attachment');
  202 + if($attachment){
  203 + foreach ($attachment as $file){
  204 + if($file->move()){
  205 + // 添加到邮箱中
  206 + $mail->addAttachment($file->savePath.$file->saveName, $file->name); //Add attachments
  207 + }else{
  208 + app()->e(['attachment_upload_error',$file->name]);
  209 + }
  210 + }
  211 + }
  212 +
  213 + // 回执,阅读后收回执的邮箱
  214 + if(!empty($formData['receipt'])){
  215 + $mail->ConfirmReadingTo = true;
  216 + }
  217 + // 是否紧急邮件
  218 +// Options: null (default), 1 = High, 3 = Normal, 5 = low.
  219 + $mail->Priority = $formData['priority']??3;
  220 +
  221 + //Content 主题,标题
  222 + $mail->Subject = $formData['subject'];
  223 +
  224 + $mail->isHTML(true); //Set email format to HTML
  225 + $mail->Body = $formData['body'];// html格式的内容
  226 +
  227 + // 发送
  228 + if($mail->send()){
  229 + app()->_json(['messageId' => $mail->getLastMessageID()]);
  230 + }
  231 + app()->e($mail->ErrorInfo);
  232 +
179 } 233 }
180 234
181 235
@@ -68,6 +68,7 @@ return [ @@ -68,6 +68,7 @@ return [
68 'to_bcc' => '密送', 68 'to_bcc' => '密送',
69 'cc_verify_error' => '抄送人邮箱地址错误 %s', 69 'cc_verify_error' => '抄送人邮箱地址错误 %s',
70 'bcc_verify_error' => '密送人邮箱地址错误 %s', 70 'bcc_verify_error' => '密送人邮箱地址错误 %s',
  71 + 'attachment_upload_error' => '附件上传失败 %s',
71 72
72 73
73 74
@@ -37,6 +37,13 @@ class UploadFile @@ -37,6 +37,13 @@ class UploadFile
37 public string $ext; 37 public string $ext;
38 38
39 /** 39 /**
  40 + * @var string
  41 + */
  42 + public string $saveName = '';
  43 +
  44 + public string $savePath = '';
  45 +
  46 + /**
40 * UploadFile constructor. 47 * UploadFile constructor.
41 * @param $name 48 * @param $name
42 * @param $tempFile 49 * @param $tempFile
@@ -56,9 +63,30 @@ class UploadFile @@ -56,9 +63,30 @@ class UploadFile
56 $ext = explode('.',$name); 63 $ext = explode('.',$name);
57 $this->ext = end($ext); 64 $this->ext = end($ext);
58 65
  66 + $this->savePath = PUBLIC_PATH.'/storage/';
  67 +
59 } 68 }
60 69
61 70
  71 + /**
  72 + * @param null $name
  73 + * @return bool
  74 + * @author:dc
  75 + * @time 2023/4/6 13:58
  76 + */
  77 + public function move($name = null){
  78 +
  79 + if(!is_dir($this->savePath)){
  80 + @mkdir($this->savePath,0775,true);
  81 + }
  82 +
  83 + // 保存的文件
  84 + $this->saveName = $name ? $name : md5_file($this->tempFile).'.'.$this->ext;
  85 +
  86 + return move_uploaded_file($this->tempFile,$this->savePath.$this->saveName);
  87 +
  88 + }
  89 +
62 90
63 91
64 } 92 }
@@ -182,9 +182,21 @@ class Verify { @@ -182,9 +182,21 @@ class Verify {
182 * @time 2023/3/13 17:14 182 * @time 2023/3/13 17:14
183 */ 183 */
184 public function required($key){ 184 public function required($key){
185 - return $this->_get($key) !== '' || app()->file($key); 185 + return $this->_get($key) !== '';
186 } 186 }
187 187
  188 + /**
  189 + * 文件必须
  190 + * @param $key
  191 + * @return false|UploadFile[]
  192 + * @author:dc
  193 + * @time 2023/4/6 13:45
  194 + */
  195 + public function required_file($key){
  196 + return app()->file($key);
  197 + }
  198 +
  199 +
188 200
189 /** 201 /**
190 * 验证长度 202 * 验证长度