mirror of https://github.com/jlelse/GoBlog
Use better SMTP client
This commit is contained in:
parent
7b766bbae3
commit
acbcb0c984
50
contact.go
50
contact.go
|
@ -4,11 +4,9 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/emersion/go-sasl"
|
||||
"github.com/emersion/go-smtp"
|
||||
mail "github.com/xhit/go-simple-mail/v2"
|
||||
"go.goblog.app/app/pkgs/bufferpool"
|
||||
)
|
||||
|
||||
|
@ -76,27 +74,35 @@ func (*goBlog) sendContactEmail(cc *configContact, body, replyTo string) error {
|
|||
if cc == nil || cc.SMTPHost == "" || cc.EmailFrom == "" || cc.EmailTo == "" {
|
||||
return fmt.Errorf("email not send as config is missing")
|
||||
}
|
||||
// Build email
|
||||
email := bufferpool.Get()
|
||||
defer bufferpool.Put(email)
|
||||
_, _ = email.WriteString("Content-Type: text/plain; charset=UTF-8\n")
|
||||
_, _ = fmt.Fprintf(email, "To: %s\n", cc.EmailTo)
|
||||
if replyTo != "" {
|
||||
_, _ = fmt.Fprintf(email, "Reply-To: %s\n", replyTo)
|
||||
}
|
||||
_, _ = fmt.Fprintf(email, "Date: %s\n", time.Now().UTC().Format(time.RFC1123Z))
|
||||
_, _ = fmt.Fprintf(email, "From: %s\n", cc.EmailFrom)
|
||||
subject := cc.EmailSubject
|
||||
if subject == "" {
|
||||
subject = "New contact message"
|
||||
}
|
||||
_, _ = fmt.Fprintf(email, "Subject: %s\n\n", subject)
|
||||
_, _ = fmt.Fprintf(email, "%s\n", body)
|
||||
// Send email using SMTP
|
||||
auth := sasl.NewPlainClient("", cc.SMTPUser, cc.SMTPPassword)
|
||||
// Connect to SMTP
|
||||
smtpServer := mail.NewSMTPClient()
|
||||
smtpServer.Host = cc.SMTPHost
|
||||
port := cc.SMTPPort
|
||||
if port == 0 {
|
||||
port = 587
|
||||
}
|
||||
return smtp.SendMail(cc.SMTPHost+":"+strconv.Itoa(port), auth, cc.EmailFrom, []string{cc.EmailTo}, email)
|
||||
smtpServer.Port = port
|
||||
smtpServer.Username = cc.SMTPUser
|
||||
smtpServer.Password = cc.SMTPPassword
|
||||
smtpServer.KeepAlive = false
|
||||
smtpClient, err := smtpServer.Connect()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Build email
|
||||
msg := mail.NewMSG()
|
||||
msg.AddTo(cc.EmailTo)
|
||||
msg.SetFrom(cc.EmailFrom)
|
||||
if replyTo != "" {
|
||||
msg.SetReplyTo(replyTo)
|
||||
}
|
||||
msg.SetDate(time.Now().UTC().Format("2006-01-02 15:04:05 MST"))
|
||||
subject := cc.EmailSubject
|
||||
if subject == "" {
|
||||
subject = "New contact message"
|
||||
}
|
||||
msg.SetSubject(subject)
|
||||
msg.SetBody(mail.TextPlain, body)
|
||||
// Send mail
|
||||
return msg.Send(smtpClient)
|
||||
}
|
||||
|
|
|
@ -15,8 +15,6 @@ import (
|
|||
)
|
||||
|
||||
func Test_contact(t *testing.T) {
|
||||
t.SkipNow()
|
||||
|
||||
// Start the SMTP server
|
||||
port, rd, cancel, err := mocksmtp.StartMockSMTPServer()
|
||||
require.NoError(t, err)
|
||||
|
|
5
go.mod
5
go.mod
|
@ -19,7 +19,6 @@ require (
|
|||
github.com/disintegration/imaging v1.6.2
|
||||
github.com/dmulholl/mp3lib v1.0.0
|
||||
github.com/elnormous/contenttype v1.0.3
|
||||
github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead
|
||||
github.com/emersion/go-smtp v0.16.0
|
||||
github.com/go-ap/activitypub v0.0.0-20221209114049-1ceafda50f9f
|
||||
github.com/go-ap/client v0.0.0-20221211133508-e3087682099e
|
||||
|
@ -59,6 +58,7 @@ require (
|
|||
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80
|
||||
github.com/traefik/yaegi v0.14.3
|
||||
github.com/vcraescu/go-paginator v1.0.1-0.20201114172518-2cfc59fe05c2
|
||||
github.com/xhit/go-simple-mail/v2 v2.13.0
|
||||
github.com/yuin/goldmark v1.5.3
|
||||
// master
|
||||
github.com/yuin/goldmark-emoji v1.0.2-0.20210607094911-0487583eca38
|
||||
|
@ -83,9 +83,11 @@ require (
|
|||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/dlclark/regexp2 v1.4.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.0 // indirect
|
||||
github.com/emersion/go-sasl v0.0.0-20220912192320-0145f2c60ead // indirect
|
||||
github.com/felixge/httpsnoop v1.0.1 // indirect
|
||||
github.com/fsnotify/fsnotify v1.6.0 // indirect
|
||||
github.com/go-ap/errors v0.0.0-20221205040414-01c1adfc98ea // indirect
|
||||
github.com/go-test/deep v1.1.0 // indirect
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/gorilla/css v1.0.0 // indirect
|
||||
|
@ -119,6 +121,7 @@ require (
|
|||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.0 // indirect
|
||||
github.com/tidwall/sjson v1.2.5 // indirect
|
||||
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 // indirect
|
||||
github.com/valyala/fastjson v1.6.3 // indirect
|
||||
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
|
||||
|
|
6
go.sum
6
go.sum
|
@ -152,6 +152,8 @@ github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GO
|
|||
github.com/go-sql-driver/mysql v1.4.1 h1:g24URVg0OFbNUTx9qqY1IRZ9D9z3iPyi5zKhQZpNwpA=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
|
||||
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
|
||||
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
|
||||
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
|
||||
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0=
|
||||
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
|
||||
github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8=
|
||||
|
@ -405,6 +407,8 @@ github.com/tkrajina/gpxgo v1.2.2-0.20220217201249-321f19554eec h1:o5aL1yX+/xzvK4
|
|||
github.com/tkrajina/gpxgo v1.2.2-0.20220217201249-321f19554eec/go.mod h1:795sjVRFo5wWyN6oOZp0RYienGGBJjpAlgOz2nCngA0=
|
||||
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 h1:nrZ3ySNYwJbSpD6ce9duiP+QkD3JuLCcWkdaehUS/3Y=
|
||||
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80/go.mod h1:iFyPdL66DjUD96XmzVL3ZntbzcflLnznH0fr99w5VqE=
|
||||
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208 h1:PM5hJF7HVfNWmCjMdEfbuOBNXSVF2cMFGgQTPdKCbwM=
|
||||
github.com/toorop/go-dkim v0.0.0-20201103131630-e1cd1a0a5208/go.mod h1:BzWtXXrXzZUvMacR0oF/fbDDgUPO8L36tDMmRAf14ns=
|
||||
github.com/traefik/yaegi v0.14.3 h1:LqA0k8DKwvRMc+msfQjNusphHJc+r6WC5tZU5TmUFOM=
|
||||
github.com/traefik/yaegi v0.14.3/go.mod h1:AVRxhaI2G+nUsaM1zyktzwXn69G3t/AuTDrCiTds9p0=
|
||||
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
|
||||
|
@ -417,6 +421,8 @@ github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLr
|
|||
github.com/vcraescu/go-paginator v1.0.1-0.20201114172518-2cfc59fe05c2 h1:l5j4nE6rosbObXB/uPmzxOQ2z5uXlFOgttlHJ2YL/0w=
|
||||
github.com/vcraescu/go-paginator v1.0.1-0.20201114172518-2cfc59fe05c2/go.mod h1:NEDNuq1asYbAeX+uy6w56MDQSFmBQz9k+N9Hy6m4r2U=
|
||||
github.com/wsxiaoys/terminal v0.0.0-20160513160801-0940f3fc43a0/go.mod h1:IXCdmsXIht47RaVFLEdVnh1t+pgYtTAhQGj73kz+2DM=
|
||||
github.com/xhit/go-simple-mail/v2 v2.13.0 h1:OANWU9jHZrVfBkNkvLf8Ww0fexwpQVF/v/5f96fFTLI=
|
||||
github.com/xhit/go-simple-mail/v2 v2.13.0/go.mod h1:b7P5ygho6SYE+VIqpxA6QkYfv4teeyG4MKqB3utRu98=
|
||||
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
|
|
Loading…
Reference in New Issue