...
|
...
|
@@ -239,113 +239,15 @@ class Mail { |
|
|
|
|
|
// 开始同步
|
|
|
if($uids){
|
|
|
$results = $this->client->fetchHeader($uids,true);
|
|
|
|
|
|
if($results && is_array($results)){
|
|
|
// 表示已存在新邮件
|
|
|
if($folder == 'INBOX') redis()->incr('have_new_mail_'.$email_id,120);
|
|
|
|
|
|
// 批量插入
|
|
|
foreach ($results as $key=>$result){
|
|
|
$header = $result['HEADER.FIELDS'];
|
|
|
|
|
|
foreach ($result['FLAGS'] as $k=>$FLAG){
|
|
|
$result['FLAGS'][$k] = strtolower(str_replace('\\','',$FLAG));
|
|
|
}
|
|
|
try {
|
|
|
|
|
|
// 没有收件人
|
|
|
if(!empty($header['To'])){
|
|
|
$header['To'] = MailFun::toOrFrom($header['To']);
|
|
|
}else{
|
|
|
$header['To'] = [];
|
|
|
}
|
|
|
|
|
|
$header['From'] = MailFun::toOrFrom($header['From']??'');
|
|
|
// 抄送 ,密送
|
|
|
$cc = [];
|
|
|
$bcc = [];
|
|
|
if($header['Cc']??''){
|
|
|
$cc = MailFun::toOrFrom($header['Cc']);
|
|
|
}
|
|
|
if($header['Bcc']??''){
|
|
|
$bcc = MailFun::toOrFrom($header['Bcc']);
|
|
|
}
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
'uid' => $result['UID'],
|
|
|
'subject' => $header['Subject']??'',
|
|
|
'cc' => $cc,
|
|
|
'bcc' => $bcc,
|
|
|
'from' => $header['From'][0]['email']??'',
|
|
|
'from_name' => $header['From'][0]['name']??'',
|
|
|
'to' => $header['To']?implode(',',array_column($header['To'],'email')):'',
|
|
|
'to_name' => json_encode($header['To']),
|
|
|
'date' => strtotime(is_array($header['Date']??'') ? $header['Date'][0] : $header['Date']??''),
|
|
|
'message_id' => $header['Message-ID']??'',
|
|
|
'udate' => strtotime($result['INTERNALDATE']),
|
|
|
'size' => $result['RFC822.SIZE']??0,
|
|
|
'recent' => in_array('recent',$result['FLAGS']) ? 1 : 0,
|
|
|
'seen' => in_array('seen',$result['FLAGS']) ? 1 : 0,
|
|
|
'draft' => in_array('draft',$result['FLAGS']) ? 1 : 0,
|
|
|
'flagged' => in_array('flagged',$result['FLAGS']) ? 1 : 0,
|
|
|
'answered' => in_array('answered',$result['FLAGS']) ? 1 : 0,
|
|
|
'folder_id' => $folder_id,
|
|
|
'email_id' => $email_id,
|
|
|
'is_file' => MailFun::isFile($result['BODYSTRUCTURE']??'') ? 1: 0 //是否附件
|
|
|
];
|
|
|
$data['date'] = $data['date'] ? : 0;
|
|
|
|
|
|
// 验证是否存在黑名单中
|
|
|
if($blacklist){
|
|
|
// 邮箱是否在黑名单中
|
|
|
$isBlacklist = false;
|
|
|
if (!empty($blacklist['emails']) && is_array($blacklist['emails']) && in_array($data['from'],$blacklist['emails'])){
|
|
|
$isBlacklist = true;
|
|
|
}
|
|
|
// 域是否存在
|
|
|
if (!empty($blacklist['domain']) && is_array($blacklist['domain']) && in_array(explode('@',$data['from'])[1],$blacklist['domain'])){
|
|
|
$isBlacklist = true;
|
|
|
}
|
|
|
|
|
|
if($isBlacklist && $blackFolder){
|
|
|
// 移入垃圾箱
|
|
|
$this->client->move($result['UID'],$blackFolder);
|
|
|
continue;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
}catch (\Throwable $e){
|
|
|
logs(
|
|
|
'邮件解析失败:'.PHP_EOL.$e->getMessage().PHP_EOL.print_r($result,true),
|
|
|
LOG_PATH.'/imap/mail/'.$email_id.'/'.$result['UID'].'.log'
|
|
|
);
|
|
|
unset($results[$key]);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 插入数据库
|
|
|
try {
|
|
|
$id = $db->insert(listsSql::$table,$data);
|
|
|
if($id){
|
|
|
// 同步body内容
|
|
|
redis()->rPush('sync_email_body', [
|
|
|
'lists_id' => $id,
|
|
|
'email_id' => $email_id,
|
|
|
'folder_id' => $folder_id,
|
|
|
'folder' => $folder,
|
|
|
'uid' => $data['uid'],
|
|
|
]);
|
|
|
}
|
|
|
}catch (\Throwable $e){
|
|
|
|
|
|
}
|
|
|
|
|
|
$results[$key] = [];
|
|
|
}
|
|
|
}
|
|
|
$this->syncUidEmail(
|
|
|
$uids,
|
|
|
$email_id,
|
|
|
$folder,
|
|
|
$folder_id,
|
|
|
$blacklist,
|
|
|
$blackFolder,
|
|
|
$db
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
...
|
...
|
@@ -363,6 +265,129 @@ class Mail { |
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 同步邮件 只通过 uid获取
|
|
|
* @param array $uids
|
|
|
* @param $email_id
|
|
|
* @param $folder
|
|
|
* @param $folder_id
|
|
|
* @param $blacklist
|
|
|
* @param $blackFolder
|
|
|
* @param $db
|
|
|
* @throws \Exception
|
|
|
* @author:dc
|
|
|
* @time 2023/8/2 15:35
|
|
|
*/
|
|
|
public function syncUidEmail(array $uids,$email_id,$folder,$folder_id,$blacklist,$blackFolder,$db){
|
|
|
$results = $this->client->fetchHeader($uids,true);
|
|
|
|
|
|
if($results && is_array($results)){
|
|
|
// 表示已存在新邮件
|
|
|
if($folder == 'INBOX') redis()->incr('have_new_mail_'.$email_id,120);
|
|
|
|
|
|
// 批量插入
|
|
|
foreach ($results as $key=>$result){
|
|
|
$header = $result['HEADER.FIELDS'];
|
|
|
|
|
|
foreach ($result['FLAGS'] as $k=>$FLAG){
|
|
|
$result['FLAGS'][$k] = strtolower(str_replace('\\','',$FLAG));
|
|
|
}
|
|
|
try {
|
|
|
|
|
|
// 没有收件人
|
|
|
if(!empty($header['To'])){
|
|
|
$header['To'] = MailFun::toOrFrom($header['To']);
|
|
|
}else{
|
|
|
$header['To'] = [];
|
|
|
}
|
|
|
|
|
|
$header['From'] = MailFun::toOrFrom($header['From']??'');
|
|
|
// 抄送 ,密送
|
|
|
$cc = [];
|
|
|
$bcc = [];
|
|
|
if($header['Cc']??''){
|
|
|
$cc = MailFun::toOrFrom($header['Cc']);
|
|
|
}
|
|
|
if($header['Bcc']??''){
|
|
|
$bcc = MailFun::toOrFrom($header['Bcc']);
|
|
|
}
|
|
|
|
|
|
|
|
|
$data = [
|
|
|
'uid' => $result['UID'],
|
|
|
'subject' => $header['Subject']??'',
|
|
|
'cc' => $cc,
|
|
|
'bcc' => $bcc,
|
|
|
'from' => $header['From'][0]['email']??'',
|
|
|
'from_name' => $header['From'][0]['name']??'',
|
|
|
'to' => $header['To']?implode(',',array_column($header['To'],'email')):'',
|
|
|
'to_name' => json_encode($header['To']),
|
|
|
'date' => strtotime(is_array($header['Date']??'') ? $header['Date'][0] : $header['Date']??''),
|
|
|
'message_id' => $header['Message-ID']??'',
|
|
|
'udate' => strtotime($result['INTERNALDATE']),
|
|
|
'size' => $result['RFC822.SIZE']??0,
|
|
|
'recent' => in_array('recent',$result['FLAGS']) ? 1 : 0,
|
|
|
'seen' => in_array('seen',$result['FLAGS']) ? 1 : 0,
|
|
|
'draft' => in_array('draft',$result['FLAGS']) ? 1 : 0,
|
|
|
'flagged' => in_array('flagged',$result['FLAGS']) ? 1 : 0,
|
|
|
'answered' => in_array('answered',$result['FLAGS']) ? 1 : 0,
|
|
|
'folder_id' => $folder_id,
|
|
|
'email_id' => $email_id,
|
|
|
'is_file' => MailFun::isFile($result['BODYSTRUCTURE']??'') ? 1: 0 //是否附件
|
|
|
];
|
|
|
$data['date'] = $data['date'] ? : 0;
|
|
|
|
|
|
// 验证是否存在黑名单中
|
|
|
if($blacklist){
|
|
|
// 邮箱是否在黑名单中
|
|
|
$isBlacklist = false;
|
|
|
if (!empty($blacklist['emails']) && is_array($blacklist['emails']) && in_array($data['from'],$blacklist['emails'])){
|
|
|
$isBlacklist = true;
|
|
|
}
|
|
|
// 域是否存在
|
|
|
if (!empty($blacklist['domain']) && is_array($blacklist['domain']) && in_array(explode('@',$data['from'])[1],$blacklist['domain'])){
|
|
|
$isBlacklist = true;
|
|
|
}
|
|
|
|
|
|
if($isBlacklist && $blackFolder){
|
|
|
// 移入垃圾箱
|
|
|
$this->client->move($result['UID'],$blackFolder);
|
|
|
continue;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
}catch (\Throwable $e){
|
|
|
logs(
|
|
|
'邮件解析失败:'.PHP_EOL.$e->getMessage().PHP_EOL.print_r($result,true),
|
|
|
LOG_PATH.'/imap/mail/'.$email_id.'/'.$result['UID'].'.log'
|
|
|
);
|
|
|
unset($results[$key]);
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
// 插入数据库
|
|
|
try {
|
|
|
$id = $db->insert(listsSql::$table,$data);
|
|
|
if($id){
|
|
|
// 同步body内容
|
|
|
redis()->rPush('sync_email_body', [
|
|
|
'lists_id' => $id,
|
|
|
'email_id' => $email_id,
|
|
|
'folder_id' => $folder_id,
|
|
|
'folder' => $folder,
|
|
|
'uid' => $data['uid'],
|
|
|
]);
|
|
|
}
|
|
|
}catch (\Throwable $e){
|
|
|
|
|
|
}
|
|
|
|
|
|
$results[$key] = [];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 同步 邮件 内容 body
|
...
|
...
|
|