Skip to content

Commit

Permalink
fix: smtp starttls compatibility issue (pr #292)
Browse files Browse the repository at this point in the history
fix: smtp starttls compatibility issue
  • Loading branch information
zmh-program authored Nov 27, 2024
2 parents 068f2dd + 5986f49 commit 928319a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
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=
34 changes: 22 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,32 +38,42 @@ 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
// Create gomail message object
message := gomail.NewMessage()

// Determine sender address based on whether the username contains "@"
var from string
if strings.Contains(s.Username, "@") {
dialer = mail.NewDialer(s.Host, s.Port, s.Username, s.Password)
// If the username contains "@", use From as the sender
from = s.From
} else {
dialer = mail.NewDialer(s.Host, s.Port, s.From, s.Password)
// Otherwise, combine the username and From to form the sender's email address
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)

dialer := gomail.NewDialer(s.Host, s.Port, s.Username, s.Password)

// If TLS protocol is enabled
if s.Protocol {
dialer.StartTLSPolicy = mail.MandatoryStartTLS
dialer.TLSConfig = &tls.Config{
InsecureSkipVerify: false, // Disable insecure certificate verification
ServerName: s.Host, // Set ServerName to the SMTP host
}
} else {
dialer.StartTLSPolicy = mail.NoStartTLS
// When SSL is enabled, no need for STARTTLS, directly establish an encrypted connection
dialer.SSL = true
}

// outlook STARTTLS policy adapter
// outlook starttls policy
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 {
Expand Down

0 comments on commit 928319a

Please sign in to comment.