diff --git a/email.go b/email.go index 7da60e4..561af91 100644 --- a/email.go +++ b/email.go @@ -22,7 +22,6 @@ import ( "github.com/aymerick/douceur/inliner" "github.com/gorilla/mux" - "github.com/mailgun/mailgun-go" stripmd "github.com/writeas/go-strip-markdown/v2" "github.com/writeas/impart" "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%` - gun := mailgun.NewMailgun(app.cfg.Email.Domain, app.cfg.Email.MailgunPrivate) - - if app.cfg.Email.MailgunEurope { - gun.SetAPIBase("https://api.eu.mailgun.net/v3") + mlr, err := mailer.New(app.cfg.Email) + if err != nil { + return err + } + 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) if replyTo != "" { m.SetReplyTo(replyTo) @@ -412,7 +411,7 @@ Sent to %recipient.to%. Unsubscribe: ` + p.Collection.CanonicalURL() + `email/un return err } - m.SetHtml(html) + m.SetHTML(html) log.Info("[email] Adding %d recipient(s)", len(subs)) for _, s := range subs { @@ -428,8 +427,8 @@ Sent to %recipient.to%. Unsubscribe: ` + p.Collection.CanonicalURL() + `email/un } } - res, _, err := gun.Send(m) - log.Info("[email] Send result: %s", res) + err = mlr.Send(m) + log.Info("[email] Email sent") if err != nil { log.Error("Unable to send post email: %v", err) return err diff --git a/mailer/mailer.go b/mailer/mailer.go index a75b585..974796a 100644 --- a/mailer/mailer.go +++ b/mailer/mailer.go @@ -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. func (m *Message) AddTag(tag string) { 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. func (m *Mailer) Send(msg *Message) error { if m.smtp != nil { @@ -97,6 +115,7 @@ func (m *Mailer) Send(msg *Message) error { if err != nil { return err } + // TODO: handle possible limits (new config?) on max recipients (multiple batches, with delay?) return msg.smtpMsg.Send(client) } else if m.mailGun != nil { _, _, err := m.mailGun.Send(msg.mgMsg)