Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: smtp starttls compatibility issue #292

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ require (
github.com/google/uuid v1.3.1
github.com/gorilla/websocket v1.5.0
github.com/lukasjarosch/go-docx v0.4.7
github.com/mattn/go-sqlite3 v1.14.22
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pkoukk/tiktoken-go v0.1.6
github.com/russross/blackfriday/v2 v2.1.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.16.0
github.com/volcengine/volc-sdk-golang v1.0.127
golang.org/x/net v0.15.0
gopkg.in/mail.v2 v2.3.1
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
)

require (
Expand Down Expand Up @@ -53,7 +54,6 @@ require (
github.com/leodido/go-urn v1.2.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand All @@ -76,4 +76,4 @@ require (
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
)
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1018,10 +1018,10 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE=
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/mail.v2 v2.3.1 h1:WYFn/oANrAGP2C0dcV6/pbkPzv8yGzqTjPmTeO7qoXk=
gopkg.in/mail.v2 v2.3.1/go.mod h1:htwXN1Qh09vZJ1NVKxQqHPBaCBbzKhp5GzuJEA4VJWw=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
Expand Down Expand Up @@ -1053,4 +1053,4 @@ rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
36 changes: 24 additions & 12 deletions utils/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package utils

import (
"bytes"
"crypto/tls"
"fmt"
"gopkg.in/gomail.v2"
"strings"
"text/template"

"gopkg.in/mail.v2"
)

type SmtpPoster struct {
Expand Down Expand Up @@ -38,34 +38,46 @@ func (s *SmtpPoster) SendMail(to string, subject string, body string) error {
return fmt.Errorf("smtp not configured properly")
}

var dialer *mail.Dialer
var from string
// 创建 gomail 消息对象
message := gomail.NewMessage()

// 根据用户名是否包含"@"来决定发件人地址
var from string
if strings.Contains(s.Username, "@") {
dialer = mail.NewDialer(s.Host, s.Port, s.Username, s.Password)
// 如果用户名包含"@", 则直接使用 From 作为发件人
from = s.From
} else {
dialer = mail.NewDialer(s.Host, s.Port, s.From, s.Password)
// 否则,将用户名和 From 组合成发件人的邮箱地址
from = fmt.Sprintf("%s <%s>", s.Username, s.From)
}

message := mail.NewMessage()
message.SetHeader("From", from)
message.SetHeader("To", to)
message.SetHeader("Subject", subject)
message.SetBody("text/html", body)

// 创建 gomail 拨号器
dialer := gomail.NewDialer(s.Host, s.Port, s.Username, s.Password)

// 如果启用TLS协议
if s.Protocol {
dialer.StartTLSPolicy = mail.MandatoryStartTLS
dialer.TLSConfig = &tls.Config{
InsecureSkipVerify: false, // 禁用不安全的证书验证
ServerName: s.Host, // 设置ServerName为SMTP主机
}
} else {
dialer.StartTLSPolicy = mail.NoStartTLS
// 启用SSL时,不需要STARTTLS,直接进行加密连接
dialer.SSL = true
}

// outlook STARTTLS policy adapter
// 针对Outlook的STARTTLS策略适配器
if strings.Contains(s.Host, "outlook") {
dialer.StartTLSPolicy = mail.MandatoryStartTLS
dialer.TLSConfig = &tls.Config{
InsecureSkipVerify: false,
ServerName: s.Host,
}
}

// 拨号并发送邮件
if err := dialer.DialAndSend(message); err != nil {
return fmt.Errorf("sent mail failed: %s", err.Error())
}
Expand Down