From c85898ebc8cd057d504894e4919053e41168f9ff Mon Sep 17 00:00:00 2001 From: Oganneson Date: Tue, 26 Nov 2024 04:00:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=B0=86smtp.go=E8=BD=AC=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?gomail.v2=E5=AE=9E=E7=8E=B0=E4=BB=A5=E8=A7=A3=E5=86=B3StartTLS?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 6 +++--- go.sum | 6 +++--- utils/smtp.go | 36 ++++++++++++++++++++++++------------ 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 4a097262..823420d3 100644 --- a/go.mod +++ b/go.mod @@ -15,6 +15,7 @@ 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 @@ -22,7 +23,7 @@ require ( 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 ( @@ -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 @@ -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 -) \ No newline at end of file +) diff --git a/go.sum b/go.sum index f31efbe4..5ab0f5a7 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= \ No newline at end of file +sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc= diff --git a/utils/smtp.go b/utils/smtp.go index 1e86983b..24c51c30 100644 --- a/utils/smtp.go +++ b/utils/smtp.go @@ -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 { @@ -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()) } From 5986f49bd9ea47c7945643a954719ea2fee30cd4 Mon Sep 17 00:00:00 2001 From: Minghan Zhang <112773885+zmh-program@users.noreply.github.com> Date: Wed, 27 Nov 2024 14:31:00 +0800 Subject: [PATCH 2/2] chore: modify comment lang (by copilot space) --- utils/smtp.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/utils/smtp.go b/utils/smtp.go index 24c51c30..467f2400 100644 --- a/utils/smtp.go +++ b/utils/smtp.go @@ -38,16 +38,16 @@ func (s *SmtpPoster) SendMail(to string, subject string, body string) error { return fmt.Errorf("smtp not configured properly") } - // 创建 gomail 消息对象 + // Create gomail message object message := gomail.NewMessage() - // 根据用户名是否包含"@"来决定发件人地址 + // Determine sender address based on whether the username contains "@" var from string if strings.Contains(s.Username, "@") { - // 如果用户名包含"@", 则直接使用 From 作为发件人 + // If the username contains "@", use From as the sender from = s.From } else { - // 否则,将用户名和 From 组合成发件人的邮箱地址 + // Otherwise, combine the username and From to form the sender's email address from = fmt.Sprintf("%s <%s>", s.Username, s.From) } message.SetHeader("From", from) @@ -55,21 +55,20 @@ func (s *SmtpPoster) SendMail(to string, subject string, body string) error { message.SetHeader("Subject", subject) message.SetBody("text/html", body) - // 创建 gomail 拨号器 dialer := gomail.NewDialer(s.Host, s.Port, s.Username, s.Password) - // 如果启用TLS协议 + // If TLS protocol is enabled if s.Protocol { dialer.TLSConfig = &tls.Config{ - InsecureSkipVerify: false, // 禁用不安全的证书验证 - ServerName: s.Host, // 设置ServerName为SMTP主机 + InsecureSkipVerify: false, // Disable insecure certificate verification + ServerName: s.Host, // Set ServerName to the SMTP host } } else { - // 启用SSL时,不需要STARTTLS,直接进行加密连接 + // When SSL is enabled, no need for STARTTLS, directly establish an encrypted connection dialer.SSL = true } - // 针对Outlook的STARTTLS策略适配器 + // outlook starttls policy if strings.Contains(s.Host, "outlook") { dialer.TLSConfig = &tls.Config{ InsecureSkipVerify: false, @@ -77,7 +76,6 @@ func (s *SmtpPoster) SendMail(to string, subject string, body string) error { } } - // 拨号并发送邮件 if err := dialer.DialAndSend(message); err != nil { return fmt.Errorf("sent mail failed: %s", err.Error()) }