作者 邓超

1

... ... @@ -161,50 +161,32 @@ class Home extends Base {
]);
// 邮件对象
$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']??'');
}
// //回复到那个邮件
// $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']??'');
}
$sendData = [];
$sendData['email'] = $email['email'];
$sendData['nickname'] = $formData['nickname']??'';
$sendData['tos'] = $formData['tos'];
// 抄送
if(($formData['isCc']??0) && !empty($formData['cc'])){
$sendData['cc'] = $formData['cc'];
}
// 密送
if(!empty($formData['bcc'])){
foreach ($formData['bcc'] as $to){
$mail->addBCC($to['email'], $to['name']??'');
if(($formData['isBcc']??0) && !empty($formData['bcc'])){
$sendData['bcc'] = $formData['bcc'];
}
}
//Attachments 附件
$sendData['reply_to'] = [];//回复到那个邮件
//Attachments 附件 上传的
$sendData['attachment'] = [];
$attachment = app()->file('attachment');
if($attachment){
foreach ($attachment as $file){
if($file->move()){
// 添加到邮箱中
$mail->addAttachment($file->savePath.$file->saveName, $file->name); //Add attachments
$sendData['attachment'][] = [
'name' => $file->name,
'filename' => $file->name,
'signName' => $file->saveName,
'path' => $file->savePath.$file->saveName
];
}else{
app()->e(['attachment_upload_error',$file->name]);
}
... ... @@ -212,15 +194,18 @@ class Home extends Base {
}
// 远程路径,云文件
$attachmentUrl = app()->request('attachmentUrl');
if(is_array($attachmentUrl)){
foreach ($attachmentUrl as $file){
$file = is_array($file) ? $file : json_decode($file,true);
if(!empty($file['url']) && !empty($file['name'])){
$file = new UploadFile($file['name'],$file['url']);
if($file->move()){
// 添加到邮箱中
$mail->addAttachment($file->savePath.$file->saveName, $file->name); //Add attachments
$sendData['attachment'][] = [
'name' => $file->name,
'filename' => $file->name,
'signName' => $file->saveName,
'path' => $file->savePath.$file->saveName
];
}else{
app()->e(['attachment_upload_error',$file->name]);
}
... ... @@ -228,26 +213,36 @@ class Home extends Base {
}
}
$sendData['receipt'] = empty($formData['receipt']) ? '' : 1;// 回执,阅读后收回执的邮箱
$sendData['priority'] = $formData['priority']??3;// 是否紧急邮件
$sendData['subject'] = $formData['subject'];// //Content 主题,标题
$sendData['body'] = $formData['body'];
// 回执,阅读后收回执的邮箱
if(!empty($formData['receipt'])){
$mail->ConfirmReadingTo = true;
// 定时发送时间
$timer = strtotime(app()->request('timerValue','2023-04-10'));
// 是否存草稿
if(app()->request('saveType')=='draft'){
// 保存
$draftid = listsSql::saveDraft($sendData,$email,app()->request('draft_id',0,'intval'));
// 保存失败
if($draftid){
app()->_json(['draft_id'=>$draftid]);
}
// 是否紧急邮件
// Options: null (default), 1 = High, 3 = Normal, 5 = low.
$mail->Priority = $formData['priority']??3;
app()->e('save_draft_error');
//Content 主题,标题
$mail->Subject = $formData['subject'];
$mail->isHTML(true); //Set email format to HTML
$mail->Body = $formData['body'];// html格式的内容
}
// 定时发送
else if(app()->request('timer') && $timer > time()){
}else{
// 发送
if($mail->send()){
app()->_json(['messageId' => $mail->getLastMessageID()]);
// $result = MailFun::sendEmail($sendData,$email);
// if($result[0]){
// app()->_json(['messageId' => $result[1]]);
// }
// 错误
app()->e($result[1]);
}
app()->e($mail->ErrorInfo);
}
... ... @@ -292,6 +287,12 @@ class Home extends Base {
$this->setFlags('seen');
}
/**
* 是否已回复
* @throws \Lib\Err
* @author:dc
* @time 2023/4/10 16:30
*/
public function answered_2_unanswered(){
$this->setFlags('answered');
}
... ...
... ... @@ -79,7 +79,10 @@ return [
'upload_file_load_error' => '上传文件加载失败'
'upload_file_load_error' => '上传文件加载失败',
'save_draft_error' => '草稿保存失败',
... ...
... ... @@ -361,16 +361,10 @@ class Mail {
}
try {
$db->insert(bodySql::$table,[
bodySql::insertOrUpdate($db,[
'lists_id' => $id,
'text_html' => $body // todo::因为邮件会出现多编码问题,会导致数据库写不进去
],false);
}catch (\Throwable $e){
$db->update(bodySql::$table,[
'text_html' => $body // todo::因为邮件会出现多编码问题,会导致数据库写不进去
],dbWhere(['lists_id' => $id]),false);
}
]);
// 更新描述
... ...
... ... @@ -2,6 +2,10 @@
namespace Lib\Mail;
use Lib\UploadFile;
use Model\bodySql;
use Model\folderSql;
use Model\listsSql;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
... ... @@ -105,75 +109,80 @@ class MailFun {
/**
* 发送邮件
* @param string $smtp smtp服务器地址
* @param string $username 发件人
* @param string $password 发件人密码
* @param string $nickname 昵称
* @param string|array $to_email 收件人,邮件或['email'=>'','name'=>'']
* @param string $subject 标题,主题
* @param string $body 文本内容
* @param array $files 文件 ['origin_name'=>'','path'=>'']
* @param false $receipt 是否回执
* @param int $priority 是否紧急 1紧急 3正常 5慢
* @param array $data 数据
* @param array $email 邮箱信息
* @return array
* @throws \Lib\Err
* @throws \PHPMailer\PHPMailer\Exception
* @author:dc
* @time 2023/2/18 17:45
* @time 2023/4/11 9:12
*/
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){
public static function sendEmail(array $data, array $email){
// 邮件对象
$mail = new PHPMailer();
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_OFF;//调试输出 SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = $smtp; //Set the SMTP server to send through
$mail->Host = $email['smtp']; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = $username; //SMTP username
$mail->Password = $password; //SMTP password
$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($username, $nickname);// 显示邮件来自谁
// //Add a recipient,设置收件人 这里必须是一对一发送
if(is_array($to_email)){
$mail->addAddress($to_email['email'], $to_email['name']);
}else{
$mail->addAddress($to_email, '');
$mail->setFrom($email['email'], $data['nickname']??'');// 显示邮件来自谁
// //设置收件人
foreach ($data['tos'] as $to){
$mail->addAddress($to['email'], $to['name']??'');
}
// //回复到那个邮件
// $mail->addAddress($reply_to['email'], $reply_to['name']); //Add a recipient
// // 抄送
// $mail->addCC($cc['email'],$cc['name']);//
// // 密送
// $mail->addBCC($bcc['email'],$bcc['name']);
//回复到那个邮件
if(!empty($data['reply_to'])){
if(is_string($data['reply_to'])){
$mail->addReplyTo($data['reply_to']);
}else{
$mail->addReplyTo($data['reply_to']['email'], $data['reply_to']['name']); //Add a recipient
}
}
// 抄送
if(!empty($data['cc'])){
foreach ($data['cc'] as $to){
$mail->addCC($to['email'], $to['name']??'');
}
}
// 密送
if(!empty($data['bcc'])){
foreach ($data['bcc'] as $to){
$mail->addBCC($to['email'], $to['name']??'');
}
}
//Attachments 附件
if($files){
foreach ($files as $file){
// 添加到邮箱中
$mail->addAttachment($file['path'], $file['origin_name']); //Add attachments
if(!empty($data['attachment'])){
foreach ($data['attachment'] as $file){
$mail->addAttachment($file['path'], $file['filename']);
}
}
// 回执,阅读后收回执的邮箱
if($receipt){
$mail->ConfirmReadingTo = $receipt;
if(!empty($data['receipt'])){
$mail->ConfirmReadingTo = true;
}
// 是否紧急邮件
// Options: null (default), 1 = High, 3 = Normal, 5 = low.
$mail->Priority = $priority;
$mail->Priority = $data['priority']??3;
//Content 主题,标题
$mail->Subject = $subject;
$mail->Subject = $data['subject'];
$mail->isHTML(true); //Set email format to HTML
$mail->Body = $body;// html格式的内容
$mail->Body = $data['body'];// html格式的内容
// 发送
if($mail->send()){
... ...
... ... @@ -2,6 +2,8 @@
namespace Model;
use Lib\DbPool;
/**
* body
* @author:dc
... ... @@ -37,5 +39,21 @@ class bodySql {
}
/**
* 插入或者更新
* @param DbPool $db
* @param $data
* @return int
* @author:dc
* @time 2023/4/10 17:30
*/
public static function insertOrUpdate(DbPool $db,$data){
try {
return $db->insert(bodySql::$table,$data,false);
}catch (\Throwable $e){
return $db->update(bodySql::$table,$data,dbWhere(['lists_id' => $data['lists_id']]),false);
}
}
}
... ...
... ... @@ -100,5 +100,72 @@ class listsSql {
}
/**
* 存草稿
* @param array $data
* @param array $email
* @param int $draftid
* @return int
* @author:dc
* @time 2023/4/11 9:44
*/
public static function saveDraft(array $data, array $email,int $draftid = 0):int {
$draftData = [
'subject' => $data['subject'],
'from' => $data['email'],
'from_name' => $data['nickname'],
'date' => time(),
'udate' => time(),
'draft' => 1,
'folder_id' => db()->value(folderSql::first(['folder'=>'草稿箱','email_id'=>$email['id']],'`id`')),
'email_id' => $email['id'],
'is_file' => $data['attachment'] ? 1 : 0, //是否附件
];
$draftData['to'] = $data['tos'][0]['email'];
$draftData['to_name'] = $data['tos'];
$draftData['cc'] = $data['cc'];
$draftData['bcc'] = $data['bcc'];
$draftData['uuid'] = md5($draftData['email_id'].$draftData['folder_id'].rand(1111111,9999999999));
if($draftid){
// 修改
if(!db()->update(listsSql::$table,$draftData,dbWhere(
[
'id' => $draftid,
'email_id' => $draftData['email_id']
]
))){
return 0;
}
}else{
$draftid = db()->insert(listsSql::$table,$draftData);
}
if($draftid){
$draftBody = [
[
'type' => 'text/html',
'charset' => 'utf-8',
'body' => base64_encode($data['body'])
]
];
foreach ($data['attachment'] as $da){
$draftBody[] = $da;
}
bodySql::insertOrUpdate(db(),[
'lists_id' => $draftid,
'text_html' => $draftBody
]);
return $draftid;
}else{
return 0;
}
}
}
... ...
<?php
namespace Model;
/**
* 邮件发送任务状态
* @author:dc
* @time 2023/4/10 16:27
* Class sendJobStatusSql
* @package Model
*/
class sendJobStatusSql {
/**
* 表
* @var string
*/
public static $table = 'lists';
}
... ...
<?php
namespace Model;
/**
* 邮件发送任务
* @author:dc
* @time 2023/4/10 16:28
* Class sendJobsSql
* @package Model
*/
class sendJobsSql {
/**
* 表
* @var string
*/
public static $table = 'lists';
}
... ...