Implement remaining plumbing for sending stmp emails to subscribers

This commit is contained in:
Jean-Baptiste Giraudeau 2024-12-04 15:11:01 +01:00
parent e65b73dc73
commit d9deb29730
No known key found for this signature in database
GPG key ID: 7CEF8C9CC2D9933B
2 changed files with 29 additions and 11 deletions

View file

@ -22,7 +22,6 @@ import (
"github.com/aymerick/douceur/inliner" "github.com/aymerick/douceur/inliner"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/mailgun/mailgun-go"
stripmd "github.com/writeas/go-strip-markdown/v2" stripmd "github.com/writeas/go-strip-markdown/v2"
"github.com/writeas/impart" "github.com/writeas/impart"
"github.com/writeas/web-core/data" "github.com/writeas/web-core/data"
@ -308,14 +307,14 @@ Originally published on ` + p.Collection.DisplayTitle() + ` (` + p.Collection.Ca
Sent to %recipient.to%. Unsubscribe: ` + p.Collection.CanonicalURL() + `email/unsubscribe/%recipient.id%?t=%recipient.token%` Sent to %recipient.to%. Unsubscribe: ` + p.Collection.CanonicalURL() + `email/unsubscribe/%recipient.id%?t=%recipient.token%`
gun := mailgun.NewMailgun(app.cfg.Email.Domain, app.cfg.Email.MailgunPrivate) mlr, err := mailer.New(app.cfg.Email)
if err != nil {
if app.cfg.Email.MailgunEurope { return err
gun.SetAPIBase("https://api.eu.mailgun.net/v3") }
m, err := mlr.NewMessage(p.Collection.DisplayTitle()+" <"+p.Collection.Alias+"@"+app.cfg.Email.Domain+">", stripmd.Strip(p.DisplayTitle()), plainMsg)
if err != nil {
return err
} }
m := mailgun.NewMessage(p.Collection.DisplayTitle()+" <"+p.Collection.Alias+"@"+app.cfg.Email.Domain+">", stripmd.Strip(p.DisplayTitle()), plainMsg)
replyTo := app.db.GetCollectionAttribute(collID, collAttrLetterReplyTo) replyTo := app.db.GetCollectionAttribute(collID, collAttrLetterReplyTo)
if replyTo != "" { if replyTo != "" {
m.SetReplyTo(replyTo) m.SetReplyTo(replyTo)
@ -412,7 +411,7 @@ Sent to %recipient.to%. Unsubscribe: ` + p.Collection.CanonicalURL() + `email/un
return err return err
} }
m.SetHtml(html) m.SetHTML(html)
log.Info("[email] Adding %d recipient(s)", len(subs)) log.Info("[email] Adding %d recipient(s)", len(subs))
for _, s := range subs { for _, s := range subs {
@ -428,8 +427,8 @@ Sent to %recipient.to%. Unsubscribe: ` + p.Collection.CanonicalURL() + `email/un
} }
} }
res, _, err := gun.Send(m) err = mlr.Send(m)
log.Info("[email] Send result: %s", res) log.Info("[email] Email sent")
if err != nil { if err != nil {
log.Error("Unable to send post email: %v", err) log.Error("Unable to send post email: %v", err)
return err return err

View file

@ -83,6 +83,14 @@ func (m *Message) SetHTML(html string) {
} }
} }
func (m *Message) SetReplyTo(replyTo string) {
if (m.smtpMsg != nil) {
m.smtpMsg.SetReplyTo(replyTo)
} else {
m.mgMsg.SetReplyTo(replyTo)
}
}
// AddTag attaches a tag to the Message for providers that support it. // AddTag attaches a tag to the Message for providers that support it.
func (m *Message) AddTag(tag string) { func (m *Message) AddTag(tag string) {
if m.mgMsg != nil { if m.mgMsg != nil {
@ -90,6 +98,16 @@ func (m *Message) AddTag(tag string) {
} }
} }
// Variable only used by mailgun
func (m *Message) AddRecipientAndVariables(r string, vars map[string]interface{}) error {
if (m.smtpMsg != nil) {
m.smtpMsg.AddBcc(r)
return nil
} else {
return m.mgMsg.AddRecipientAndVariables(r, vars)
}
}
// Send sends the given message via the preferred provider. // Send sends the given message via the preferred provider.
func (m *Mailer) Send(msg *Message) error { func (m *Mailer) Send(msg *Message) error {
if m.smtp != nil { if m.smtp != nil {
@ -97,6 +115,7 @@ func (m *Mailer) Send(msg *Message) error {
if err != nil { if err != nil {
return err return err
} }
// TODO: handle possible limits (new config?) on max recipients (multiple batches, with delay?)
return msg.smtpMsg.Send(client) return msg.smtpMsg.Send(client)
} else if m.mailGun != nil { } else if m.mailGun != nil {
_, _, err := m.mailGun.Send(msg.mgMsg) _, _, err := m.mailGun.Send(msg.mgMsg)