From c2ce8c51f84615d0e79eafd97d7ac650366cb250 Mon Sep 17 00:00:00 2001 From: Irnes Mujkanovic Date: Tue, 18 Apr 2017 16:40:08 +0200 Subject: [PATCH] Use a config struct instead of plain variables for smtp details --- mailer.go | 12 +++--------- mailer_test.go | 35 +++++++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/mailer.go b/mailer.go index 61dc920..1e67966 100644 --- a/mailer.go +++ b/mailer.go @@ -20,21 +20,15 @@ type Mailer interface { } // NewMailer returns a new Mailer using provided configuration -func NewMailer(host string, port int, user, pass string, ssl bool) Mailer { +func NewMailer(config Config, ssl bool) Mailer { var mailer Mailer if ssl { smtpssl := new(SMTPSLL) - smtpssl.Host = host - smtpssl.Port = port - smtpssl.User = user - smtpssl.Pass = pass + smtpssl.Config = config mailer = smtpssl } else { smtp := new(SMTP) - smtp.Host = host - smtp.Port = port - smtp.User = user - smtp.Pass = pass + smtp.Config = config mailer = smtp } return mailer diff --git a/mailer_test.go b/mailer_test.go index 043392d..4da7861 100644 --- a/mailer_test.go +++ b/mailer_test.go @@ -14,37 +14,52 @@ var ( ) func TestSMTP(t *testing.T) { + config := Config{ + Host: host, + Port: 25, + User: user, + Pass: pass, + } + + mailer := NewMailer(config, false) + mail := NewMail() mail.FromName = "Go Mailer" mail.From = user + mail.SetTo(recipent) + mail.Subject = "Go SMTP Test" mail.Body = "This is a test e-mail from Go Mailer via SMTP" - mail.SetTo(recipent) - mailer := NewMailer(host, 25, user, pass, false) if err := mailer.Send(mail); err != nil { t.Errorf("Send error: %s", err) } } func TestSMTPSSL(t *testing.T) { + config := Config{ + Host: host, + Port: 465, + User: user, + Pass: pass, + } + + mailer := NewMailer(config, true) + mail := NewMail() mail.FromName = "Go Mailer" mail.From = user + // multiple recipents + mail.SetTo(recipent) + mail.SetTo(recipent2) + mail.Subject = "Go SMTP SSL Test" // the rich message body mail.Body = `

Hello from Go Mailer

-

- This is a test e-mail from Go Mailer via SMTP SSL -

+

This is a test e-mail from Go Mailer via SMTP SSL

` - // multiple recipents - mail.SetTo(recipent) - mail.SetTo(recipent2) - - mailer := NewMailer(host, 465, user, pass, true) if err := mailer.Send(mail); err != nil { t.Errorf("Send error: %s", err) }