|
@@ -1,7 +1,6 @@
|
|
|
package mail
|
|
|
|
|
|
import (
|
|
|
- "eta/eta_crawler/models/email"
|
|
|
"eta/eta_crawler/utils"
|
|
|
"fmt"
|
|
|
"io"
|
|
@@ -30,7 +29,7 @@ type MailMessage struct {
|
|
|
Attachment map[string][]byte `description:"附件资源"`
|
|
|
}
|
|
|
|
|
|
-func ListenMail(mailAddress, folder, userName, password string, readBatchSize, fromEmailIndex int) (err error) { // 收件箱
|
|
|
+func ListenMail(mailAddress, folder, userName, password string, readBatchSize, lastNday int) (err error) { // 收件箱
|
|
|
defer func() {
|
|
|
// 处理结束
|
|
|
if err != nil {
|
|
@@ -100,6 +99,9 @@ func ListenMail(mailAddress, folder, userName, password string, readBatchSize, f
|
|
|
seqSet := new(imap.SeqSet)
|
|
|
to := mbox.Messages // 此文件下的邮件总数
|
|
|
|
|
|
+ now := time.Now()
|
|
|
+ startTime := time.Date(now.Year(), now.Month(), now.Day()-lastNday, 0, 0, 0, 0, time.Local)
|
|
|
+
|
|
|
var isStopFor bool
|
|
|
step := uint32(1)
|
|
|
for i := to; i >= 1; {
|
|
@@ -128,17 +130,20 @@ func ListenMail(mailAddress, folder, userName, password string, readBatchSize, f
|
|
|
}
|
|
|
// 获取邮件内容 End
|
|
|
for msg := range messages {
|
|
|
- utils.FileLog.Info("正在读取邮件uid: %d", msg.Uid)
|
|
|
// 如果需要终止,那么就不处理了
|
|
|
if isStopFor {
|
|
|
continue
|
|
|
}
|
|
|
-
|
|
|
- // 判断当前邮件id是否小于等于已经监听到的最小id,如果是,那么就不处理了
|
|
|
- if msg.Uid <= uint32(fromEmailIndex) {
|
|
|
- isStopFor = true
|
|
|
- break
|
|
|
+ // 判断当前邮件收件时间是否小于设定的时间,如果是,那么就不处理了
|
|
|
+ envelope := msg.Envelope
|
|
|
+ if envelope != nil {
|
|
|
+ if envelope.Date.Before(startTime) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ continue
|
|
|
}
|
|
|
+ utils.FileLog.Info("正在读取邮件uid: %d", msg.Uid)
|
|
|
|
|
|
emailMessage, tmpErr := readEveryMsg(msg)
|
|
|
if tmpErr != nil {
|
|
@@ -223,13 +228,10 @@ func readEveryMsg(msg *imap.Message) (emailMessage MailMessage, err error) {
|
|
|
// 邮件标题
|
|
|
subject, err := mr.Header.Subject()
|
|
|
if err != nil {
|
|
|
- utils.FileLog.Warning("邮件主题 Subject ERR:", err)
|
|
|
+ utils.FileLog.Warning("邮件主题 Subject ERR:%v", err)
|
|
|
}
|
|
|
emailMessage.Title = subject
|
|
|
|
|
|
- // 过滤
|
|
|
-
|
|
|
- logList := make([]*email.EmailListenLog, 0)
|
|
|
for {
|
|
|
p, tmpErr := mr.NextPart()
|
|
|
if tmpErr == io.EOF {
|
|
@@ -285,14 +287,6 @@ func readEveryMsg(msg *imap.Message) (emailMessage MailMessage, err error) {
|
|
|
}
|
|
|
fmt.Printf("保存到文件: %s \n", filePath)
|
|
|
utils.FileLog.Info("保存到文件: %s ", filePath)
|
|
|
- logList = append(logList, &email.EmailListenLog{
|
|
|
- Title: emailMessage.Title,
|
|
|
- Author: emailMessage.From,
|
|
|
- Email: emailMessage.FromAddress,
|
|
|
- EmailMessageUid: emailMessage.Uid,
|
|
|
- FileName: filename,
|
|
|
- CreateTime: time.Now(),
|
|
|
- })
|
|
|
// 这是附件资源
|
|
|
if contentDisposition := p.Header.Get("Content-Disposition"); contentDisposition != "" {
|
|
|
if strings.HasPrefix(contentDisposition, "attachment") {
|
|
@@ -311,10 +305,6 @@ func readEveryMsg(msg *imap.Message) (emailMessage MailMessage, err error) {
|
|
|
if emailMessage.Content == `` {
|
|
|
emailMessage.Content = textStr
|
|
|
}
|
|
|
- err = email.BatchAddEmailListenLog(logList)
|
|
|
- if err != nil {
|
|
|
- utils.FileLog.Error("邮件日志保存失败:%v", err)
|
|
|
- }
|
|
|
|
|
|
return
|
|
|
}
|