fix mailer, use sendgrid client with api key after deprecated smtp basic auth

This commit is contained in:
dhax 2020-11-06 17:56:06 +01:00
parent 21175bda62
commit 13d31c62a6
3 changed files with 75 additions and 41 deletions

View file

@ -3,6 +3,7 @@ package email
import (
"bytes"
"errors"
"fmt"
"html/template"
"log"
@ -12,7 +13,10 @@ import (
"strings"
"time"
"github.com/go-mail/mail"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
// "github.com/go-mail/mail"
"github.com/jaytaylor/html2text"
"github.com/spf13/viper"
"github.com/vanng822/go-premailer/premailer"
@ -25,7 +29,7 @@ var (
// Mailer is a SMTP mailer.
type Mailer struct {
client *mail.Dialer
client *sendgrid.Client
from Email
}
@ -35,35 +39,49 @@ func NewMailer() (*Mailer, error) {
return nil, err
}
smtp := struct {
Host string
Port int
User string
Password string
}{
viper.GetString("email_smtp_host"),
viper.GetInt("email_smtp_port"),
viper.GetString("email_smtp_user"),
viper.GetString("email_smtp_password"),
// usage with sendgrid api key client
apiKey := viper.GetString("SENDGRID_API_KEY")
if apiKey == "" {
return nil, errors.New("missing sendgrid api key")
}
client := sendgrid.NewSendClient(apiKey)
s := &Mailer{
client: mail.NewPlainDialer(smtp.Host, smtp.Port, smtp.User, smtp.Password),
mailer := &Mailer{
client: client,
from: NewEmail(viper.GetString("email_from_name"), viper.GetString("email_from_address")),
}
return mailer, nil
if smtp.Host == "" {
log.Println("SMTP host not set => printing emails to stdout")
debug = true
return s, nil
}
// usage with go-mail basic smtp auth client
// smtp := struct {
// Host string
// Port int
// User string
// Password string
// }{
// viper.GetString("email_smtp_host"),
// viper.GetInt("email_smtp_port"),
// viper.GetString("email_smtp_user"),
// viper.GetString("email_smtp_password"),
// }
d, err := s.client.Dial()
if err == nil {
d.Close()
return s, nil
}
return nil, err
// s := &Mailer{
// client: mail.NewDialer(smtp.Host, smtp.Port, smtp.User, smtp.Password),
// from: NewEmail(viper.GetString("email_from_name"), viper.GetString("email_from_address")),
// }
// if smtp.Host == "" {
// log.Println("SMTP host not set => printing emails to stdout")
// debug = true
// return s, nil
// }
// d, err := s.client.Dial()
// if err != nil {
// return nil, err
// }
// d.Close()
// return s, nil
}
// Send sends the mail via smtp.
@ -75,14 +93,33 @@ func (m *Mailer) Send(email *message) error {
return nil
}
msg := mail.NewMessage()
msg.SetAddressHeader("From", email.from.Address, email.from.Name)
msg.SetAddressHeader("To", email.to.Address, email.to.Name)
msg.SetHeader("Subject", email.subject)
msg.SetBody("text/plain", email.text)
msg.AddAlternative("text/html", email.html)
// usage with sendgrid
from := mail.NewEmail(email.from.Name, email.from.Address)
to := mail.NewEmail(email.to.Name, email.to.Address)
message := mail.NewSingleEmail(from, email.subject, to, email.text, email.html)
return m.client.DialAndSend(msg)
response, err := m.client.Send(message)
if err != nil {
return err
}
if debug {
log.Println(response.StatusCode)
log.Println(response.Body)
log.Println(response.Headers)
}
return nil
// usage with go-mail
// msg := mail.NewMessage()
// msg.SetAddressHeader("From", email.from.Address, email.from.Name)
// msg.SetAddressHeader("To", email.to.Address, email.to.Name)
// msg.SetHeader("Subject", email.subject)
// msg.SetBody("text/plain", email.text)
// msg.AddAlternative("text/html", email.html)
// return m.client.DialAndSend(msg)
}
// message struct holds all parts of a specific email message.

5
go.mod
View file

@ -10,7 +10,6 @@ require (
github.com/go-chi/docgen v1.0.5
github.com/go-chi/jwtauth v4.0.4+incompatible
github.com/go-chi/render v1.0.1
github.com/go-mail/mail v2.3.1+incompatible
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible
github.com/go-pg/migrations v6.2.0+incompatible
github.com/go-pg/pg v7.1.7+incompatible
@ -25,6 +24,8 @@ require (
github.com/olekukonko/tablewriter v0.0.4 // indirect
github.com/onsi/gomega v1.4.2 // indirect
github.com/pelletier/go-toml v1.6.0 // indirect
github.com/sendgrid/rest v2.6.2+incompatible // indirect
github.com/sendgrid/sendgrid-go v3.7.1+incompatible
github.com/sirupsen/logrus v1.4.2
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cast v1.3.1 // indirect
@ -39,9 +40,7 @@ require (
golang.org/x/net v0.0.0-20200301022130-244492dfa37a // indirect
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
golang.org/x/text v0.3.2 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/ini.v1 v1.52.0 // indirect
gopkg.in/mail.v2 v2.3.1 // indirect
gopkg.in/yaml.v2 v2.2.8 // indirect
mellium.im/sasl v0.2.1 // indirect
)

10
go.sum
View file

@ -45,8 +45,6 @@ github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-mail/mail v2.3.1+incompatible h1:UzNOn0k5lpfVtO31cK3hn6I4VEVGhe3lX8AJBAxXExM=
github.com/go-mail/mail v2.3.1+incompatible/go.mod h1:VPWjmmNyRsWXQZHVHT3g0YbIINUkSmuKOiLIDkWbL6M=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible h1:msy24VGS42fKO9K1vLz82/GeYW1cILu7Nuuj1N3BBkE=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible/go.mod h1:gsEKFIVnabGBt6mXmxK0MoFy+cZoTJY6mu5Ll3LVLBU=
github.com/go-pg/migrations v6.2.0+incompatible h1:OCaoPbrTHwyd+zgWbbhVZyZ8CekfD2xbtap7BZAb27Q=
@ -141,6 +139,10 @@ github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7z
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sendgrid/rest v2.6.2+incompatible h1:zGMNhccsPkIc8SvU9x+qdDz2qhFoGUPGGC4mMvTondA=
github.com/sendgrid/rest v2.6.2+incompatible/go.mod h1:kXX7q3jZtJXK5c5qK83bSGMdV6tsOE70KbHoqJls4lE=
github.com/sendgrid/sendgrid-go v3.7.1+incompatible h1:Pw98qJ7CvR7I5vVlV3CnA+OofRBkAxxR9sgsPX0qEoY=
github.com/sendgrid/sendgrid-go v3.7.1+incompatible/go.mod h1:QRQt+LX/NmgVEvmdRw0VT/QgUn499+iza2FnDca9fg8=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
@ -245,8 +247,6 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
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 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
@ -256,8 +256,6 @@ gopkg.in/ini.v1 v1.51.0 h1:AQvPpx3LzTDM0AjnIRlVFwFFGC+npRopjZxLJj6gdno=
gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.52.0 h1:j+Lt/M1oPPejkniCg1TkWE2J3Eh1oZTsHSXzMTzUXn4=
gopkg.in/ini.v1 v1.52.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/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=