Skip to content

Commit

Permalink
Use a config struct instead of plain variables for smtp details
Browse files Browse the repository at this point in the history
  • Loading branch information
irnes committed Apr 18, 2017
1 parent b7be382 commit c2ce8c5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
12 changes: 3 additions & 9 deletions mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 25 additions & 10 deletions mailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = `
<h2>Hello from Go Mailer</h2>
<p>
This is a test e-mail from Go Mailer via SMTP SSL
</p>
<p>This is a test e-mail from Go Mailer via SMTP SSL</p>
`

// 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)
}
Expand Down

0 comments on commit c2ce8c5

Please sign in to comment.