refactor: removed error logs from smtp service since they are redundant
This commit is contained in:
@@ -26,7 +26,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
|
||||||
errs "easywish/internal/errors"
|
errs "easywish/internal/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -35,23 +34,20 @@ type SmtpService interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type smtpServiceImpl struct {
|
type smtpServiceImpl struct {
|
||||||
log *zap.Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSmtpService(_log *zap.Logger) SmtpService {
|
func NewSmtpService() SmtpService {
|
||||||
return &smtpServiceImpl{log: _log}
|
return &smtpServiceImpl{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *smtpServiceImpl) SendEmail(to string, subject, body string) error {
|
func (s *smtpServiceImpl) SendEmail(to string, subject, body string) error {
|
||||||
cfg := config.GetConfig()
|
cfg := config.GetConfig()
|
||||||
|
|
||||||
if !cfg.SmtpEnabled {
|
if !cfg.SmtpEnabled {
|
||||||
s.log.Error("Attempted to send an email with SMTP disabled in the config")
|
|
||||||
return errs.ErrSmtpDisabled
|
return errs.ErrSmtpDisabled
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.SmtpServer == "" || cfg.SmtpPort == 0 || cfg.SmtpFrom == "" {
|
if cfg.SmtpServer == "" || cfg.SmtpPort == 0 || cfg.SmtpFrom == "" {
|
||||||
s.log.Error("SMTP service settings or the SMTP From paramater are not set")
|
|
||||||
return errs.ErrSmtpMissingConfiguration
|
return errs.ErrSmtpMissingConfiguration
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,14 +80,12 @@ func (s *smtpServiceImpl) SendEmail(to string, subject, body string) error {
|
|||||||
conn, err = net.DialTimeout("tcp", hostPort, timeout)
|
conn, err = net.DialTimeout("tcp", hostPort, timeout)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.log.Error("SMTP connection failure", zap.Error(err))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
client, err := smtp.NewClient(conn, cfg.SmtpServer)
|
client, err := smtp.NewClient(conn, cfg.SmtpServer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.log.Error("SMTP client creation failed", zap.Error(err))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer client.Close()
|
defer client.Close()
|
||||||
@@ -107,19 +101,16 @@ func (s *smtpServiceImpl) SendEmail(to string, subject, body string) error {
|
|||||||
if cfg.SmtpUser != "" && cfg.SmtpPassword != "" {
|
if cfg.SmtpUser != "" && cfg.SmtpPassword != "" {
|
||||||
auth := smtp.PlainAuth("", cfg.SmtpUser, cfg.SmtpPassword, cfg.SmtpServer)
|
auth := smtp.PlainAuth("", cfg.SmtpUser, cfg.SmtpPassword, cfg.SmtpServer)
|
||||||
if err = client.Auth(auth); err != nil {
|
if err = client.Auth(auth); err != nil {
|
||||||
s.log.Error("SMTP authentication failure", zap.Error(err))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = client.Mail(cfg.SmtpFrom); err != nil {
|
if err = client.Mail(cfg.SmtpFrom); err != nil {
|
||||||
s.log.Error("SMTP sender set failed", zap.Error(err))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, recipient := range toSlice {
|
for _, recipient := range toSlice {
|
||||||
if err = client.Rcpt(recipient); err != nil {
|
if err = client.Rcpt(recipient); err != nil {
|
||||||
s.log.Error("SMTP recipient set failed", zap.Error(err))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -127,15 +118,12 @@ func (s *smtpServiceImpl) SendEmail(to string, subject, body string) error {
|
|||||||
// Send email body
|
// Send email body
|
||||||
w, err := client.Data()
|
w, err := client.Data()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.log.Error("SMTP data command failed", zap.Error(err))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err = w.Write(message); err != nil {
|
if _, err = w.Write(message); err != nil {
|
||||||
s.log.Error("SMTP message write failed", zap.Error(err))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = w.Close(); err != nil {
|
if err = w.Close(); err != nil {
|
||||||
s.log.Error("SMTP message close failed", zap.Error(err))
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user