Skip to content

Commit

Permalink
Refactor SMTP mail sending code using gomail package
Browse files Browse the repository at this point in the history
The gomail package is now used to handle the generation  of emails in the SMTP client code. This refactoring simplifies and standardizes the mail sending code. The necessary dependencies have also been added to go.mod and vendor/modules.txt.

Signed-off-by: Christian Roessner <[email protected]>
  • Loading branch information
Christian Roessner committed Jul 23, 2024
1 parent 917b662 commit 72d854d
Show file tree
Hide file tree
Showing 24 changed files with 1,890 additions and 16 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
golang.org/x/net v0.24.0
golang.org/x/oauth2 v0.19.0
golang.org/x/text v0.14.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
)

require (
Expand Down Expand Up @@ -113,6 +114,7 @@ require (
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/sys v0.19.0 // indirect
google.golang.org/protobuf v1.34.0 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/xmlpath.v2 v2.0.0-20150820204837-860cbeca3ebc // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -500,12 +500,16 @@ google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw
google.golang.org/protobuf v1.34.0 h1:Qo/qEd2RZPCf2nKuorzksSknv0d3ERwp1vFG38gSmH4=
google.golang.org/protobuf v1.34.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk=
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
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/xmlpath.v2 v2.0.0-20150820204837-860cbeca3ebc h1:LMEBgNcZUqXaP7evD1PZcL6EcDVa2QOFuI+cqM3+AJM=
Expand Down
35 changes: 19 additions & 16 deletions server/lualib/smtp/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package smtp

import (
"bytes"
"crypto/tls"
"fmt"
"io"
Expand All @@ -11,6 +12,8 @@ import (
"strconv"
"strings"
"time"

"gopkg.in/gomail.v2"
)

// GenericClient is an interface that defines the methods required for sending emails using an SMTP or LMTP server.
Expand Down Expand Up @@ -278,6 +281,7 @@ type Client interface {
// Otherwise, it uses the smtp.SendMail function to send the email without encryption.
func SendMail(options *MailOptions) error {
var (
buf bytes.Buffer
auth smtp.Auth
err error
)
Expand Down Expand Up @@ -311,22 +315,21 @@ func SendMail(options *MailOptions) error {
}
}

// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
toConcatenated := strings.Join(options.To, ",")
msg := []byte(
"Date: " + time.Now().Format(time.RFC1123Z) + "\r\n" +
"Message-ID: <" + strconv.FormatInt(time.Now().UnixNano(), 10) + "@" + msgIDDomain + ">\r\n" +
"From: " + options.From + "\r\n" +
"To: " + toConcatenated + "\r\n" +
"Subject: " + options.Subject + "\r\n" +
"Content-Type: text/plain; charset=utf-8\r\n" +
"MIME-Version: 1.0\r\n" +
"\r\n" +
options.Body +
"\r\n")

err = sendMail(options.Server+fmt.Sprintf(":%d", options.Port), options.HeloName, auth, options.From, options.To, msg, options.TLS, options.StartTLS, options.LMTP)
msg := gomail.NewMessage()

msg.SetHeader("Date", msg.FormatDate(time.Now()))
msg.SetHeader("Message-ID", strconv.FormatInt(time.Now().UnixNano(), 10)+"@"+msgIDDomain)
msg.SetHeader("From", options.From)
msg.SetHeader("To", options.To...)
msg.SetHeader("Subject", options.Subject)
msg.SetBody("text/plain; charset=UTF-8", options.Body)

_, err = msg.WriteTo(&buf)
if err != nil {
return err
}

err = sendMail(options.Server+fmt.Sprintf(":%d", options.Port), options.HeloName, auth, options.From, options.To, buf.Bytes(), options.TLS, options.StartTLS, options.LMTP)

return err
}
Expand Down
20 changes: 20 additions & 0 deletions vendor/gopkg.in/alexcesaro/quotedprintable.v3/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions vendor/gopkg.in/alexcesaro/quotedprintable.v3/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 72d854d

Please sign in to comment.