From b57eb954970fab493d1a15558f35479d04cbdca0 Mon Sep 17 00:00:00 2001 From: urko Date: Thu, 29 May 2025 09:24:26 +0200 Subject: [PATCH] refactor: insecure no auth/tls --- examples/main.go | 2 +- pkg/email/email.go | 47 +++++++++++++++++++++++++++++------------ pkg/email/email_test.go | 6 +++--- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/examples/main.go b/examples/main.go index 38ae0b2..c763df6 100644 --- a/examples/main.go +++ b/examples/main.go @@ -9,7 +9,7 @@ import ( func main() { // Here fill with real data - emailService := email.NewInsecure(email.MailServiceConfig{ + emailService := email.NewInsecure(email.SecureConfig{ Auth: smtp.PlainAuth("", "your@email.com", "your-password", "smtp.youremail.com"), Host: "smtp.youremail.com", Port: "587", diff --git a/pkg/email/email.go b/pkg/email/email.go index 245a0b2..8bb9031 100644 --- a/pkg/email/email.go +++ b/pkg/email/email.go @@ -18,6 +18,19 @@ const ( delimeter = "**=myohmy689407924327" ) +type InsecureConfig struct { + Host string + Port string + From string // Sender email address +} + +type SecureConfig struct { + Auth smtp.Auth + Host string + Port string + From string // Sender email address +} + type EmailMessage struct { To string Subject string @@ -46,7 +59,16 @@ type EmailService struct { dial SmtpDialFn } -func NewInsecure(config MailServiceConfig) *EmailService { +func NewInsecureNoAuth(config InsecureConfig) *EmailService { + return &EmailService{ + host: config.Host, + port: config.Port, + from: config.From, + dial: dial, + } +} + +func NewInsecure(config SecureConfig) *EmailService { return &EmailService{ auth: config.Auth, host: config.Host, @@ -121,7 +143,7 @@ func customVerify(host string) func(cs tls.ConnectionState) error { } } -func NewSecure(config MailServiceConfig) *EmailService { +func NewSecure(config SecureConfig) *EmailService { return &EmailService{ auth: config.Auth, host: config.Host, @@ -136,7 +158,7 @@ func NewSecure(config MailServiceConfig) *EmailService { } } -func NewSecure465(config MailServiceConfig) *EmailService { +func NewSecure465(config SecureConfig) *EmailService { tlsCfg := tls.Config{ // Ideally, InsecureSkipVerify: false, // or do a proper certificate validation @@ -156,13 +178,6 @@ func NewSecure465(config MailServiceConfig) *EmailService { } } -type MailServiceConfig struct { - Auth smtp.Auth - Host string - Port string - From string // Sender email address -} - func dial(hostPort string) (SMTPClientIface, error) { client, err := smtp.Dial(hostPort) if err != nil { @@ -212,13 +227,17 @@ func (e *EmailService) send(to string, msg []byte) error { return fmt.Errorf("DIAL: %s", err) } - if err = c.StartTLS(e.tlsconfig); err != nil { - return fmt.Errorf("c.StartTLS: %s", err) + if e.tlsconfig != nil { + if err = c.StartTLS(e.tlsconfig); err != nil { + return fmt.Errorf("c.StartTLS: %s", err) + } } // Auth - if err = c.Auth(e.auth); err != nil { - return fmt.Errorf("c.Auth: %s", err) + if e.auth != nil { + if err = c.Auth(e.auth); err != nil { + return fmt.Errorf("c.Auth: %s", err) + } } // To && From diff --git a/pkg/email/email_test.go b/pkg/email/email_test.go index 64ad4c4..fba5296 100755 --- a/pkg/email/email_test.go +++ b/pkg/email/email_test.go @@ -60,7 +60,7 @@ func TestMockSendEmail(t *testing.T) { func TestNewInsecure(t *testing.T) { cfg := newConfig(".env.test") - mailSrv := NewInsecure(MailServiceConfig{ + mailSrv := NewInsecure(SecureConfig{ Auth: smtp.PlainAuth("", cfg.MailUser, cfg.MailPassword, cfg.MailHost), Host: cfg.MailHost, Port: cfg.MailPort, @@ -131,7 +131,7 @@ func TestNewInsecure(t *testing.T) { t.Run("TestSendEmail_FailedAuthentication", func(t *testing.T) { // set up authentication to fail - mailSrv := NewInsecure(MailServiceConfig{ + mailSrv := NewInsecure(SecureConfig{ Auth: smtp.PlainAuth("", "wronguser", "wrongpassword", cfg.MailHost), Host: cfg.MailHost, Port: cfg.MailPort, @@ -150,7 +150,7 @@ func TestNewInsecure(t *testing.T) { func TestSecure(t *testing.T) { cfg := newConfig(".env.test") - emailService := NewSecure(MailServiceConfig{ + emailService := NewSecure(SecureConfig{ Auth: smtp.PlainAuth("", cfg.MailUser, cfg.MailPassword, cfg.MailHost), Host: cfg.MailHost, Port: cfg.MailPort,