67 lines
1.9 KiB
Go
67 lines
1.9 KiB
Go
package services
|
||
|
||
import (
|
||
"crypto/tls"
|
||
"fmt"
|
||
"strings"
|
||
|
||
"mengpost-backend/models"
|
||
)
|
||
|
||
// 微软个人邮箱常见后缀(Outlook / Hotmail / Live 等).
|
||
var microsoftConsumerSuffixes = []string{
|
||
"@outlook.com",
|
||
"@hotmail.com",
|
||
"@live.com",
|
||
"@msn.com",
|
||
}
|
||
|
||
// IsMicrosoftConsumerEmail 判断是否为消费者微软邮箱(非 Exchange 自建域).
|
||
func IsMicrosoftConsumerEmail(email string) bool {
|
||
e := strings.ToLower(strings.TrimSpace(email))
|
||
for _, suf := range microsoftConsumerSuffixes {
|
||
if strings.HasSuffix(e, suf) {
|
||
return true
|
||
}
|
||
}
|
||
return false
|
||
}
|
||
|
||
// IMAPTLSConfig 为 IMAP TLS 握手提供 ServerName(微软等场景与证书一致).
|
||
func IMAPTLSConfig(acc *models.Account) *tls.Config {
|
||
if acc == nil || !acc.UseTLS {
|
||
return nil
|
||
}
|
||
sn := acc.IMAPHost
|
||
if IsMicrosoftConsumerEmail(acc.Email) && strings.EqualFold(acc.IMAPHost, "outlook.office365.com") {
|
||
sn = "outlook.office365.com"
|
||
}
|
||
return &tls.Config{
|
||
MinVersion: tls.VersionTLS12,
|
||
ServerName: sn,
|
||
}
|
||
}
|
||
|
||
// SMTPTLSConfig 发信使用 587 STARTTLS 时校验证书(微软 smtp.office365.com).
|
||
func SMTPTLSConfig(acc *models.Account) *tls.Config {
|
||
if acc == nil || !IsMicrosoftConsumerEmail(acc.Email) {
|
||
return nil
|
||
}
|
||
if strings.EqualFold(acc.SMTPHost, "smtp.office365.com") {
|
||
return &tls.Config{
|
||
MinVersion: tls.VersionTLS12,
|
||
ServerName: "smtp.office365.com",
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// WrapMicrosoftMailErr 为微软邮箱附加常见配置说明(IMAP 开关、基础认证与 OAuth).
|
||
func WrapMicrosoftMailErr(email string, err error) error {
|
||
if err == nil || !IsMicrosoftConsumerEmail(email) {
|
||
return err
|
||
}
|
||
hint := "(微软邮箱:网页端需开启 IMAP;OAuth 账户无需应用密码。若仍失败请检查令牌是否过期、Azure 权限是否含 IMAP。)"
|
||
return fmt.Errorf("%w %s", err, hint)
|
||
}
|