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