From 40ce16c71d3251d5a7023d4abdb7a32046d1f671 Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Wed, 7 Dec 2022 13:46:40 -0600 Subject: [PATCH] Adding MailChecker interface for email.ServiceQueue --- email/email.go | 11 +++++++++-- email/helper.go | 9 +++++++++ interfaces.go | 7 +++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/email/email.go b/email/email.go index a4be771..30d14d3 100644 --- a/email/email.go +++ b/email/email.go @@ -22,6 +22,7 @@ type ServiceQueue struct { logger echo.Logger service carrier.Service queue *work.Queue + checker gobwebs.MailChecker } // Service returns the email service @@ -34,6 +35,11 @@ func (s *ServiceQueue) Queue() *work.Queue { return s.queue } +// Checker returns the mail checker +func (s *ServiceQueue) Checker() gobwebs.MailChecker { + return s.checker +} + // SendMail sends an email message func (s *ServiceQueue) SendMail(msg *carrier.Message) error { if s.queue == nil { @@ -66,8 +72,9 @@ func (s *ServiceQueue) mailTask(msg *carrier.Message) *work.Task { } // NewServiceQueue returns a new ServiceQueue instance -func NewServiceQueue(logger echo.Logger, service carrier.Service, queue *work.Queue) *ServiceQueue { - return &ServiceQueue{logger, service, queue} +func NewServiceQueue(logger echo.Logger, service carrier.Service, + queue *work.Queue, checker gobwebs.MailChecker) *ServiceQueue { + return &ServiceQueue{logger, service, queue, checker} } // NewQueue Creates a new email processing queue. diff --git a/email/helper.go b/email/helper.go index 355ad6e..fb63199 100644 --- a/email/helper.go +++ b/email/helper.go @@ -76,6 +76,15 @@ func (h *Helper) Send(name, from, rcpt string, data gobwebs.Map) error { return fmt.Errorf("No email address given to send to") } + checker := h.q.Checker() + if err := checker.Check(rcpt); err != nil { + // Email address check failed. Don't email + // We don't want to return error here. The checker.Check function + // should do all the individual use case logging and other work. + // XXX Maybe re-think this `return nil` decision in the future + return nil + } + var ( htmlok bool sname, tname, hname string diff --git a/interfaces.go b/interfaces.go index 4d8ab0c..1559a8d 100644 --- a/interfaces.go +++ b/interfaces.go @@ -73,3 +73,10 @@ type UserFetch interface { ChangePasswordTemplateVars(c echo.Context) Map UpdateEmailTemplateVars(c echo.Context) Map } + +// MailChecker is an interface that the email module uses to verify +// if a an email address is allowed to be sent email or not. Useful +// to track bounces, spam complaints, etc. +type MailChecker interface { + Check(email string) error +} -- 2.45.2