|
@@ -7,8 +7,6 @@ import (
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/wneessen/go-mail"
|
|
"github.com/wneessen/go-mail"
|
|
-
|
|
|
|
- "git.linuxforward.com/byom/byom-golang-lib/pkg/errors"
|
|
|
|
)
|
|
)
|
|
|
|
|
|
// SMTPConfig holds the configuration for the SMTP service
|
|
// SMTPConfig holds the configuration for the SMTP service
|
|
@@ -48,12 +46,12 @@ type SMTPService struct {
|
|
// NewSMTPService creates a new SMTP service instance
|
|
// NewSMTPService creates a new SMTP service instance
|
|
func NewSMTPService(config SMTPConfig, logger *logrus.Logger) (*SMTPService, error) {
|
|
func NewSMTPService(config SMTPConfig, logger *logrus.Logger) (*SMTPService, error) {
|
|
if logger == nil {
|
|
if logger == nil {
|
|
- return nil, errors.NewSMTPError("initialization", errors.ErrInvalidInput)
|
|
|
|
|
|
+ return nil, NewError("initialization", ErrInvalidConfig, "logger is required")
|
|
}
|
|
}
|
|
|
|
|
|
// Validate config
|
|
// Validate config
|
|
if config.Host == "" || config.Port == 0 || config.Username == "" || config.Password == "" || config.From == "" {
|
|
if config.Host == "" || config.Port == 0 || config.Username == "" || config.Password == "" || config.From == "" {
|
|
- return nil, errors.NewSMTPError("initialization", errors.ErrInvalidInput)
|
|
|
|
|
|
+ return nil, NewError("initialization", ErrInvalidConfig, "missing required configuration")
|
|
}
|
|
}
|
|
|
|
|
|
// Create mail client with proper configuration
|
|
// Create mail client with proper configuration
|
|
@@ -66,7 +64,7 @@ func NewSMTPService(config SMTPConfig, logger *logrus.Logger) (*SMTPService, err
|
|
)
|
|
)
|
|
if err != nil {
|
|
if err != nil {
|
|
logger.WithError(err).Error("Failed to create SMTP client")
|
|
logger.WithError(err).Error("Failed to create SMTP client")
|
|
- return nil, errors.NewSMTPError("client creation", err)
|
|
|
|
|
|
+ return nil, NewError("initialization", ErrConnectionFailed, err.Error())
|
|
}
|
|
}
|
|
|
|
|
|
return &SMTPService{
|
|
return &SMTPService{
|
|
@@ -79,29 +77,29 @@ func NewSMTPService(config SMTPConfig, logger *logrus.Logger) (*SMTPService, err
|
|
// SendEmail sends a basic email using the default template
|
|
// SendEmail sends a basic email using the default template
|
|
func (s *SMTPService) SendEmail(to string, data EmailData) error {
|
|
func (s *SMTPService) SendEmail(to string, data EmailData) error {
|
|
if strings.TrimSpace(to) == "" {
|
|
if strings.TrimSpace(to) == "" {
|
|
- return errors.NewSMTPError("send", errors.ErrInvalidInput)
|
|
|
|
|
|
+ return NewSendError("send", to, ErrInvalidRecipient, "empty recipient email")
|
|
}
|
|
}
|
|
|
|
|
|
// Parse and execute template
|
|
// Parse and execute template
|
|
tmpl, err := template.New("email").Parse(defaultTemplate)
|
|
tmpl, err := template.New("email").Parse(defaultTemplate)
|
|
if err != nil {
|
|
if err != nil {
|
|
s.logger.WithError(err).Error("Failed to parse email template")
|
|
s.logger.WithError(err).Error("Failed to parse email template")
|
|
- return errors.NewSMTPError("template parsing", err)
|
|
|
|
|
|
+ return NewTemplateError("send", "default", ErrTemplateParsingFailed, err.Error())
|
|
}
|
|
}
|
|
|
|
|
|
var body bytes.Buffer
|
|
var body bytes.Buffer
|
|
if err := tmpl.Execute(&body, data); err != nil {
|
|
if err := tmpl.Execute(&body, data); err != nil {
|
|
s.logger.WithError(err).Error("Failed to execute email template")
|
|
s.logger.WithError(err).Error("Failed to execute email template")
|
|
- return errors.NewSMTPError("template execution", err)
|
|
|
|
|
|
+ return NewTemplateError("send", "default", ErrTemplateExecutionFailed, err.Error())
|
|
}
|
|
}
|
|
|
|
|
|
// Create and configure message
|
|
// Create and configure message
|
|
msg := mail.NewMsg()
|
|
msg := mail.NewMsg()
|
|
if err := msg.From(s.from); err != nil {
|
|
if err := msg.From(s.from); err != nil {
|
|
- return errors.NewSMTPError("message creation", err)
|
|
|
|
|
|
+ return NewError("send", ErrMessageCreationFailed, "failed to set from address")
|
|
}
|
|
}
|
|
if err := msg.To(to); err != nil {
|
|
if err := msg.To(to); err != nil {
|
|
- return errors.NewSMTPError("message creation", err)
|
|
|
|
|
|
+ return NewSendError("send", to, ErrMessageCreationFailed, "failed to set recipient")
|
|
}
|
|
}
|
|
msg.Subject(data.Subject)
|
|
msg.Subject(data.Subject)
|
|
msg.SetBodyString(mail.TypeTextHTML, body.String())
|
|
msg.SetBodyString(mail.TypeTextHTML, body.String())
|
|
@@ -109,7 +107,7 @@ func (s *SMTPService) SendEmail(to string, data EmailData) error {
|
|
// Send email
|
|
// Send email
|
|
if err := s.client.DialAndSend(msg); err != nil {
|
|
if err := s.client.DialAndSend(msg); err != nil {
|
|
s.logger.WithError(err).Error("Failed to send email")
|
|
s.logger.WithError(err).Error("Failed to send email")
|
|
- return errors.NewSMTPError("sending", err)
|
|
|
|
|
|
+ return NewSendError("send", to, ErrSendFailed, err.Error())
|
|
}
|
|
}
|
|
|
|
|
|
s.logger.WithFields(logrus.Fields{
|
|
s.logger.WithFields(logrus.Fields{
|
|
@@ -123,29 +121,29 @@ func (s *SMTPService) SendEmail(to string, data EmailData) error {
|
|
// SendVerificationEmail sends an email verification message
|
|
// SendVerificationEmail sends an email verification message
|
|
func (s *SMTPService) SendVerificationEmail(to string, data VerificationData) error {
|
|
func (s *SMTPService) SendVerificationEmail(to string, data VerificationData) error {
|
|
if strings.TrimSpace(to) == "" || strings.TrimSpace(data.VerificationURL) == "" {
|
|
if strings.TrimSpace(to) == "" || strings.TrimSpace(data.VerificationURL) == "" {
|
|
- return errors.NewSMTPError("send verification", errors.ErrInvalidInput)
|
|
|
|
|
|
+ return NewError("send_verification", ErrInvalidTemplate, "empty recipient or verification URL")
|
|
}
|
|
}
|
|
|
|
|
|
// Parse and execute template
|
|
// Parse and execute template
|
|
tmpl, err := template.New("verification").Parse(verificationTemplate)
|
|
tmpl, err := template.New("verification").Parse(verificationTemplate)
|
|
if err != nil {
|
|
if err != nil {
|
|
s.logger.WithError(err).Error("Failed to parse verification template")
|
|
s.logger.WithError(err).Error("Failed to parse verification template")
|
|
- return errors.NewSMTPError("template parsing", err)
|
|
|
|
|
|
+ return NewTemplateError("send_verification", "verification", ErrTemplateParsingFailed, err.Error())
|
|
}
|
|
}
|
|
|
|
|
|
var body bytes.Buffer
|
|
var body bytes.Buffer
|
|
if err := tmpl.Execute(&body, data); err != nil {
|
|
if err := tmpl.Execute(&body, data); err != nil {
|
|
s.logger.WithError(err).Error("Failed to execute verification template")
|
|
s.logger.WithError(err).Error("Failed to execute verification template")
|
|
- return errors.NewSMTPError("template execution", err)
|
|
|
|
|
|
+ return NewTemplateError("send_verification", "verification", ErrTemplateExecutionFailed, err.Error())
|
|
}
|
|
}
|
|
|
|
|
|
// Create and configure message
|
|
// Create and configure message
|
|
msg := mail.NewMsg()
|
|
msg := mail.NewMsg()
|
|
if err := msg.From(s.from); err != nil {
|
|
if err := msg.From(s.from); err != nil {
|
|
- return errors.NewSMTPError("message creation", err)
|
|
|
|
|
|
+ return NewError("send_verification", ErrMessageCreationFailed, "failed to set from address")
|
|
}
|
|
}
|
|
if err := msg.To(to); err != nil {
|
|
if err := msg.To(to); err != nil {
|
|
- return errors.NewSMTPError("message creation", err)
|
|
|
|
|
|
+ return NewSendError("send_verification", to, ErrMessageCreationFailed, "failed to set recipient")
|
|
}
|
|
}
|
|
msg.Subject("Verify Your Email Address")
|
|
msg.Subject("Verify Your Email Address")
|
|
msg.SetBodyString(mail.TypeTextHTML, body.String())
|
|
msg.SetBodyString(mail.TypeTextHTML, body.String())
|
|
@@ -153,7 +151,7 @@ func (s *SMTPService) SendVerificationEmail(to string, data VerificationData) er
|
|
// Send email
|
|
// Send email
|
|
if err := s.client.DialAndSend(msg); err != nil {
|
|
if err := s.client.DialAndSend(msg); err != nil {
|
|
s.logger.WithError(err).Error("Failed to send verification email")
|
|
s.logger.WithError(err).Error("Failed to send verification email")
|
|
- return errors.NewSMTPError("sending", err)
|
|
|
|
|
|
+ return NewSendError("send_verification", to, ErrSendFailed, err.Error())
|
|
}
|
|
}
|
|
|
|
|
|
s.logger.WithFields(logrus.Fields{
|
|
s.logger.WithFields(logrus.Fields{
|
|
@@ -166,29 +164,29 @@ func (s *SMTPService) SendVerificationEmail(to string, data VerificationData) er
|
|
// SendWelcomeEmail sends a welcome email with credentials
|
|
// SendWelcomeEmail sends a welcome email with credentials
|
|
func (s *SMTPService) SendWelcomeEmail(to string, data WelcomeData) error {
|
|
func (s *SMTPService) SendWelcomeEmail(to string, data WelcomeData) error {
|
|
if strings.TrimSpace(to) == "" || strings.TrimSpace(data.Username) == "" {
|
|
if strings.TrimSpace(to) == "" || strings.TrimSpace(data.Username) == "" {
|
|
- return errors.NewSMTPError("send welcome", errors.ErrInvalidInput)
|
|
|
|
|
|
+ return NewError("send_welcome", ErrInvalidTemplate, "empty recipient or username")
|
|
}
|
|
}
|
|
|
|
|
|
// Parse and execute template
|
|
// Parse and execute template
|
|
tmpl, err := template.New("welcome").Parse(welcomeTemplate)
|
|
tmpl, err := template.New("welcome").Parse(welcomeTemplate)
|
|
if err != nil {
|
|
if err != nil {
|
|
s.logger.WithError(err).Error("Failed to parse welcome template")
|
|
s.logger.WithError(err).Error("Failed to parse welcome template")
|
|
- return errors.NewSMTPError("template parsing", err)
|
|
|
|
|
|
+ return NewTemplateError("send_welcome", "welcome", ErrTemplateParsingFailed, err.Error())
|
|
}
|
|
}
|
|
|
|
|
|
var body bytes.Buffer
|
|
var body bytes.Buffer
|
|
if err := tmpl.Execute(&body, data); err != nil {
|
|
if err := tmpl.Execute(&body, data); err != nil {
|
|
s.logger.WithError(err).Error("Failed to execute welcome template")
|
|
s.logger.WithError(err).Error("Failed to execute welcome template")
|
|
- return errors.NewSMTPError("template execution", err)
|
|
|
|
|
|
+ return NewTemplateError("send_welcome", "welcome", ErrTemplateExecutionFailed, err.Error())
|
|
}
|
|
}
|
|
|
|
|
|
// Create and configure message
|
|
// Create and configure message
|
|
msg := mail.NewMsg()
|
|
msg := mail.NewMsg()
|
|
if err := msg.From(s.from); err != nil {
|
|
if err := msg.From(s.from); err != nil {
|
|
- return errors.NewSMTPError("message creation", err)
|
|
|
|
|
|
+ return NewError("send_welcome", ErrMessageCreationFailed, "failed to set from address")
|
|
}
|
|
}
|
|
if err := msg.To(to); err != nil {
|
|
if err := msg.To(to); err != nil {
|
|
- return errors.NewSMTPError("message creation", err)
|
|
|
|
|
|
+ return NewSendError("send_welcome", to, ErrMessageCreationFailed, "failed to set recipient")
|
|
}
|
|
}
|
|
msg.Subject("Welcome to Our Platform!")
|
|
msg.Subject("Welcome to Our Platform!")
|
|
msg.SetBodyString(mail.TypeTextHTML, body.String())
|
|
msg.SetBodyString(mail.TypeTextHTML, body.String())
|
|
@@ -196,7 +194,7 @@ func (s *SMTPService) SendWelcomeEmail(to string, data WelcomeData) error {
|
|
// Send email
|
|
// Send email
|
|
if err := s.client.DialAndSend(msg); err != nil {
|
|
if err := s.client.DialAndSend(msg); err != nil {
|
|
s.logger.WithError(err).Error("Failed to send welcome email")
|
|
s.logger.WithError(err).Error("Failed to send welcome email")
|
|
- return errors.NewSMTPError("sending", err)
|
|
|
|
|
|
+ return NewSendError("send_welcome", to, ErrSendFailed, err.Error())
|
|
}
|
|
}
|
|
|
|
|
|
s.logger.WithFields(logrus.Fields{
|
|
s.logger.WithFields(logrus.Fields{
|
|
@@ -212,7 +210,7 @@ func (s *SMTPService) Close() error {
|
|
if s.client != nil {
|
|
if s.client != nil {
|
|
if err := s.client.Close(); err != nil {
|
|
if err := s.client.Close(); err != nil {
|
|
s.logger.WithError(err).Error("Failed to close SMTP client")
|
|
s.logger.WithError(err).Error("Failed to close SMTP client")
|
|
- return errors.NewSMTPError("close", err)
|
|
|
|
|
|
+ return NewError("close", ErrClientClosed, err.Error())
|
|
}
|
|
}
|
|
s.logger.Info("SMTP client closed successfully")
|
|
s.logger.Info("SMTP client closed successfully")
|
|
}
|
|
}
|