|
|
@@ -13,16 +13,18 @@ import (
|
|
|
"pmail/dto/parsemail"
|
|
|
"pmail/hooks"
|
|
|
"pmail/services/rule"
|
|
|
+ "pmail/utils/array"
|
|
|
"pmail/utils/async"
|
|
|
"pmail/utils/context"
|
|
|
- "pmail/utils/id"
|
|
|
+ "pmail/utils/send"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
func (s *Session) Data(r io.Reader) error {
|
|
|
- ctx := &context.Context{}
|
|
|
- ctx.SetValue(context.LogID, id.GenLogID())
|
|
|
+
|
|
|
+ ctx := s.Ctx
|
|
|
+
|
|
|
log.WithContext(ctx).Debugf("收到邮件")
|
|
|
|
|
|
emailData, err := io.ReadAll(r)
|
|
|
@@ -44,19 +46,38 @@ func (s *Session) Data(r io.Reader) error {
|
|
|
|
|
|
log.WithContext(ctx).Infof("邮件原始内容: %s", emailData)
|
|
|
|
|
|
- var dkimStatus, SPFStatus bool
|
|
|
+ email := parsemail.NewEmailFromReader(s.From, s.To, bytes.NewReader(emailData))
|
|
|
+ // 判断是收信还是转发
|
|
|
+ if array.InArray(email.From.GetDomain(), config.Instance.Domains) {
|
|
|
+ // 转发
|
|
|
+ err := saveEmail(ctx, email, 1, true, true)
|
|
|
+ if err != nil {
|
|
|
+ log.WithContext(ctx).Errorf("Email Save Error %v", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ send.Send(ctx, email)
|
|
|
|
|
|
- // DKIM校验
|
|
|
- dkimStatus = parsemail.Check(bytes.NewReader(emailData))
|
|
|
+ } else {
|
|
|
+ // 收件
|
|
|
|
|
|
- email := parsemail.NewEmailFromReader(bytes.NewReader(emailData))
|
|
|
+ var dkimStatus, SPFStatus bool
|
|
|
|
|
|
- if err != nil {
|
|
|
- log.WithContext(ctx).Errorf("邮件内容解析失败! Error : %v \n", err)
|
|
|
+ // DKIM校验
|
|
|
+ dkimStatus = parsemail.Check(bytes.NewReader(emailData))
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ log.WithContext(ctx).Errorf("邮件内容解析失败! Error : %v \n", err)
|
|
|
+ }
|
|
|
+
|
|
|
+ SPFStatus = spfCheck(s.RemoteAddress.String(), email.Sender, email.Sender.EmailAddress)
|
|
|
+
|
|
|
+ saveEmail(ctx, email, 0, SPFStatus, dkimStatus)
|
|
|
}
|
|
|
|
|
|
- SPFStatus = spfCheck(s.RemoteAddress.String(), email.Sender, email.Sender.EmailAddress)
|
|
|
+ return nil
|
|
|
+}
|
|
|
|
|
|
+func saveEmail(ctx *context.Context, email *parsemail.Email, emailType int, SPFStatus, dkimStatus bool) error {
|
|
|
var dkimV, spfV int8
|
|
|
if dkimStatus {
|
|
|
dkimV = 1
|
|
|
@@ -102,8 +123,9 @@ func (s *Session) Data(r io.Reader) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
- sql := "INSERT INTO email (send_date, subject, reply_to, from_name, from_address, `to`, bcc, cc, text, html, sender, attachments,spf_check, dkim_check, create_time,is_read,status,group_id) VALUES (?,?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
|
|
- _, err = db.Instance.Exec(sql,
|
|
|
+ sql := "INSERT INTO email (type, send_date, subject, reply_to, from_name, from_address, `to`, bcc, cc, text, html, sender, attachments,spf_check, dkim_check, create_time,is_read,status,group_id) VALUES (?,?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
|
|
+ _, err := db.Instance.Exec(sql,
|
|
|
+ emailType,
|
|
|
email.Date,
|
|
|
email.Subject,
|
|
|
json2string(email.ReplyTo),
|