改进邮件转发的投递兼容性 (#347)
优先使用原始邮件数据转发,保留原始发件人、主题、正文、MIME 和 DKIM 签名,避免重建邮件导致严格 DMARC 收件方拒收。 当原始转发被拒时,回退为使用本地转发账号重新投递,同时保持原始主题不变。 同时修正本地域名转发逻辑:精确识别本地域名,自转发时报错,转发给其他本地用户时直接投递到本地收件箱,避免旧的字符串包含判断误拦截。 测试:go test -v ./services/rule ./dto/parsemail ./utils/send Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
6455635f74
commit
05bca5d5a8
6 files changed
+293
-53
No files matched your search
@@ -1,6 +1,9 @@
|
||||
package rule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/Jinnrry/pmail/config"
|
||||
"github.com/Jinnrry/pmail/consts"
|
||||
"github.com/Jinnrry/pmail/db"
|
||||
@@ -12,7 +15,6 @@ import (
|
||||
"github.com/Jinnrry/pmail/utils/send"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cast"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetAllRules(ctx *context.Context, userId int) []*dto.Rule {
|
||||
@@ -60,7 +62,7 @@ func MatchRule(ctx *context.Context, rule *dto.Rule, email *parsemail.Email) boo
|
||||
return true
|
||||
}
|
||||
|
||||
func DoRule(ctx *context.Context, rule *dto.Rule, email *parsemail.Email, user *models.User) {
|
||||
func DoRule(ctx *context.Context, rule *dto.Rule, email *parsemail.Email, user *models.User, rawEmailData []byte) {
|
||||
log.WithContext(ctx).Debugf("执行规则:%s", rule.Name)
|
||||
|
||||
switch rule.Action {
|
||||
@@ -77,13 +79,11 @@ func DoRule(ctx *context.Context, rule *dto.Rule, email *parsemail.Email, user *
|
||||
log.WithContext(ctx).Errorf("sqlERror :%v", err)
|
||||
}
|
||||
case dto.FORWARD:
|
||||
if strings.Contains(rule.Params, config.Instance.Domain) {
|
||||
log.WithContext(ctx).Errorf("Forward Error! loop forwarding!")
|
||||
return
|
||||
}
|
||||
err := send.Forward(ctx, email, rule.Params, user)
|
||||
err := doForward(ctx, email, rule.Params, user, rawEmailData)
|
||||
if err != nil {
|
||||
log.WithContext(ctx).Errorf("Forward Error:%v", err)
|
||||
} else {
|
||||
log.WithContext(ctx).Infof("Forward Success:%s@%s -> %s", user.Account, config.Instance.Domains[0], rule.Params)
|
||||
}
|
||||
case dto.MOVE:
|
||||
doMove(ctx, rule, email, user)
|
||||
@@ -91,6 +91,78 @@ func DoRule(ctx *context.Context, rule *dto.Rule, email *parsemail.Email, user *
|
||||
|
||||
}
|
||||
|
||||
func doForward(ctx *context.Context, email *parsemail.Email, forwardAddress string, user *models.User, rawEmailData []byte) error {
|
||||
forwardUser := parsemail.BuilderUser(forwardAddress)
|
||||
if forwardUser == nil || forwardUser.EmailAddress == "" {
|
||||
return fmt.Errorf("invalid forward address: %s", forwardAddress)
|
||||
}
|
||||
|
||||
account, domain := forwardUser.GetDomainAccount()
|
||||
if account == "" || domain == "" {
|
||||
return fmt.Errorf("invalid forward address: %s", forwardAddress)
|
||||
}
|
||||
|
||||
if isLocalDomain(domain) {
|
||||
if strings.EqualFold(account, user.Account) {
|
||||
return fmt.Errorf("loop forwarding to self: %s", forwardAddress)
|
||||
}
|
||||
return forwardToLocalUser(ctx, email, account, forwardAddress)
|
||||
}
|
||||
|
||||
if len(rawEmailData) > 0 {
|
||||
err := send.ForwardRaw(ctx, email, rawEmailData, forwardAddress, user)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
log.WithContext(ctx).Warnf("Raw forward failed, retry remail forward:%v", err)
|
||||
}
|
||||
return send.Forward(ctx, email, forwardAddress, user)
|
||||
}
|
||||
|
||||
func forwardToLocalUser(ctx *context.Context, email *parsemail.Email, account, forwardAddress string) error {
|
||||
if email.MessageId <= 0 {
|
||||
return fmt.Errorf("email has not been saved before local forwarding")
|
||||
}
|
||||
|
||||
var user models.User
|
||||
has, err := db.Instance.Table(&models.User{}).
|
||||
Where("LOWER(account)=LOWER(?) and disabled=0", account).
|
||||
Get(&user)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !has {
|
||||
return fmt.Errorf("local forward user not found: %s", forwardAddress)
|
||||
}
|
||||
|
||||
var ue models.UserEmail
|
||||
has, err = db.Instance.Table(&models.UserEmail{}).
|
||||
Where("email_id=? and user_id=?", email.MessageId, user.ID).
|
||||
Get(&ue)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if has {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err = db.Instance.Insert(&models.UserEmail{
|
||||
UserID: user.ID,
|
||||
EmailID: cast.ToInt(email.MessageId),
|
||||
Status: cast.ToInt8(email.Status),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func isLocalDomain(domain string) bool {
|
||||
for _, localDomain := range config.Instance.Domains {
|
||||
if strings.EqualFold(domain, localDomain) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return strings.EqualFold(domain, config.Instance.Domain)
|
||||
}
|
||||
|
||||
func doMove(ctx *context.Context, rule *dto.Rule, email *parsemail.Email, user *models.User) {
|
||||
|
||||
groupId := cast.ToInt(rule.Params)
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package rule
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Jinnrry/pmail/config"
|
||||
"github.com/Jinnrry/pmail/db"
|
||||
"github.com/Jinnrry/pmail/dto/parsemail"
|
||||
"github.com/Jinnrry/pmail/models"
|
||||
"github.com/Jinnrry/pmail/utils/context"
|
||||
_ "modernc.org/sqlite"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func TestForwardToLocalUser(t *testing.T) {
|
||||
oldConfig := config.Instance
|
||||
oldDB := db.Instance
|
||||
defer func() {
|
||||
config.Instance = oldConfig
|
||||
db.Instance = oldDB
|
||||
}()
|
||||
|
||||
config.Instance = &config.Config{
|
||||
Domain: "example.com",
|
||||
Domains: []string{"example.com"},
|
||||
}
|
||||
|
||||
engine, err := xorm.NewEngine("sqlite", ":memory:")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer engine.Close()
|
||||
db.Instance = engine
|
||||
|
||||
if err := engine.Sync2(&models.User{}, &models.UserEmail{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sender := &models.User{ID: 1, Account: "sender", Name: "sender"}
|
||||
recipient := &models.User{ID: 2, Account: "recipient", Name: "recipient", IsAdmin: 1}
|
||||
if _, err := engine.Insert(sender, recipient); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
email := &parsemail.Email{MessageId: 123, Status: 0}
|
||||
if err := doForward(&context.Context{}, email, "recipient@example.com", sender, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var links []models.UserEmail
|
||||
if err := engine.Table(&models.UserEmail{}).Where("email_id=?", email.MessageId).Find(&links); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(links) != 1 || links[0].UserID != recipient.ID {
|
||||
t.Fatalf("expected one user_email link for recipient, got %+v", links)
|
||||
}
|
||||
|
||||
if err := doForward(&context.Context{}, email, "recipient@example.com", sender, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var count int64
|
||||
count, err = engine.Table(&models.UserEmail{}).Where("email_id=? and user_id=?", email.MessageId, recipient.ID).Count()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("expected idempotent local forward link, got %d links", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestForwardToLocalUserRejectsSelfLoop(t *testing.T) {
|
||||
oldConfig := config.Instance
|
||||
defer func() { config.Instance = oldConfig }()
|
||||
|
||||
config.Instance = &config.Config{
|
||||
Domain: "example.com",
|
||||
Domains: []string{"example.com"},
|
||||
}
|
||||
|
||||
err := doForward(&context.Context{}, &parsemail.Email{MessageId: 1}, "sender@example.com", &models.User{Account: "sender"}, nil)
|
||||
if err == nil || !strings.Contains(err.Error(), "loop forwarding to self") {
|
||||
t.Fatalf("expected self-loop error, got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user