作者 邓超

1

... ... @@ -9,6 +9,8 @@ use Model\bodySql;
use Model\emailSql;
use Model\folderSql;
use Model\listsSql;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
/**
... ... @@ -149,7 +151,7 @@ class Home extends Base {
'attachment|'.__('files_email') => [
'file'=>[
'ext' => [],
'size' => 500,
'size' => 1024*1024*5,
'mine' => []
]
],
... ... @@ -158,24 +160,76 @@ class Home extends Base {
]);
$ret = MailFun::sendEmail(
$email['smtp'],
$email['email'],
base64_decode($email['password']),
$formData['nickname']??'',
$formData['tos'],
$formData['subject'],
$formData['body'],
$formData['attachment']??'',
$formData['receipt']??'',
$formData['priority']??3,
);
// 邮件对象
$mail = new PHPMailer();
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_OFF;//调试输出 SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = $email['smtp']; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = $email['email']; //SMTP username
$mail->Password = base64_decode($email['password']); //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$mail->CharSet = 'utf-8';
$mail->Encoding = PHPMailer::ENCODING_QUOTED_PRINTABLE;
//Recipients,设置发件人
$mail->setFrom($email['email'], $formData['nickname']??'');// 显示邮件来自谁
// //设置收件人
foreach ($formData['tos'] as $to){
$mail->addAddress($to['email'], $to['name']??'');
}
if($ret[0]===true) {
app()->_json(['messageId'=>$ret[1]]);
}else {
app()->e($ret[1]);
// //回复到那个邮件
// $mail->addReplyTo($reply_to['email'], $reply_to['name']); //Add a recipient
// // 抄送
if(!empty($formData['cc'])){
foreach ($formData['cc'] as $to){
$mail->addCC($to['email'], $to['name']??'');
}
}
// 密送
if(!empty($formData['bcc'])){
foreach ($formData['bcc'] as $to){
$mail->addBCC($to['email'], $to['name']??'');
}
}
//Attachments 附件
$attachment = app()->file('attachment');
if($attachment){
foreach ($attachment as $file){
if($file->move()){
// 添加到邮箱中
$mail->addAttachment($file->savePath.$file->saveName, $file->name); //Add attachments
}else{
app()->e(['attachment_upload_error',$file->name]);
}
}
}
// 回执,阅读后收回执的邮箱
if(!empty($formData['receipt'])){
$mail->ConfirmReadingTo = true;
}
// 是否紧急邮件
// Options: null (default), 1 = High, 3 = Normal, 5 = low.
$mail->Priority = $formData['priority']??3;
//Content 主题,标题
$mail->Subject = $formData['subject'];
$mail->isHTML(true); //Set email format to HTML
$mail->Body = $formData['body'];// html格式的内容
// 发送
if($mail->send()){
app()->_json(['messageId' => $mail->getLastMessageID()]);
}
app()->e($mail->ErrorInfo);
}
... ...
... ... @@ -68,6 +68,7 @@ return [
'to_bcc' => '密送',
'cc_verify_error' => '抄送人邮箱地址错误 %s',
'bcc_verify_error' => '密送人邮箱地址错误 %s',
'attachment_upload_error' => '附件上传失败 %s',
... ...
... ... @@ -37,6 +37,13 @@ class UploadFile
public string $ext;
/**
* @var string
*/
public string $saveName = '';
public string $savePath = '';
/**
* UploadFile constructor.
* @param $name
* @param $tempFile
... ... @@ -56,9 +63,30 @@ class UploadFile
$ext = explode('.',$name);
$this->ext = end($ext);
$this->savePath = PUBLIC_PATH.'/storage/';
}
/**
* @param null $name
* @return bool
* @author:dc
* @time 2023/4/6 13:58
*/
public function move($name = null){
if(!is_dir($this->savePath)){
@mkdir($this->savePath,0775,true);
}
// 保存的文件
$this->saveName = $name ? $name : md5_file($this->tempFile).'.'.$this->ext;
return move_uploaded_file($this->tempFile,$this->savePath.$this->saveName);
}
}
\ No newline at end of file
... ...
... ... @@ -182,9 +182,21 @@ class Verify {
* @time 2023/3/13 17:14
*/
public function required($key){
return $this->_get($key) !== '' || app()->file($key);
return $this->_get($key) !== '';
}
/**
* 文件必须
* @param $key
* @return false|UploadFile[]
* @author:dc
* @time 2023/4/6 13:45
*/
public function required_file($key){
return app()->file($key);
}
/**
* 验证长度
... ...