Skip to content

Commit

Permalink
Add first gomailer test
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Jul 17, 2017
1 parent a6a2ea4 commit 813618e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*_env
21 changes: 0 additions & 21 deletions gomail/gomail_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion gomail/gomail.go → gomailer/gomail.go
@@ -1,4 +1,4 @@
package gomail
package gomailer

import (
"github.com/qor/mailer"
Expand Down
53 changes: 53 additions & 0 deletions gomailer/gomail_test.go
@@ -0,0 +1,53 @@
package gomailer_test

import (
"crypto/tls"
"fmt"
"net/mail"
"testing"

gomail "gopkg.in/gomail.v2"

"github.com/jinzhu/configor"
"github.com/qor/mailer"
"github.com/qor/mailer/gomailer"
)

var Mailer *mailer.Mailer

var Config = struct {
Address string `env:"SMTP_Address"`
Port int `env:"SMTP_Port"`
User string `env:"SMTP_User"`
Password string `env:"SMTP_Password"`
}{}

func init() {
configor.Load(&Config)

d := gomail.NewDialer(Config.Address, Config.Port, Config.User, Config.Password)
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
dailer, err := d.Dial()
if err != nil {
panic(fmt.Sprintf("Got error %v when dail mail server: %#v", err, Config))
}

sender := gomailer.New(&gomailer.Config{Sender: dailer})

Mailer = mailer.New(&mailer.Config{
Sender: sender,
})
}

func TestSendEmail(t *testing.T) {
err := Mailer.Send(mailer.Email{
TO: []mail.Address{{Address: "jinzhu@example.org"}},
From: &mail.Address{Address: "from@example.org"},
Text: "text email",
})

if err != nil {
t.Errorf("No error should raise when send email")
}
t.Errorf("hello world")
}
4 changes: 3 additions & 1 deletion mailer.go
Expand Up @@ -45,7 +45,9 @@ func New(config *Config) *Mailer {

// Send send email
func (mailer Mailer) Send(email Email, templates ...Template) error {
email = mailer.DefaultEmailTemplate.Merge(email)
if mailer.DefaultEmailTemplate != nil {
email = mailer.DefaultEmailTemplate.Merge(email)
}

if len(templates) == 0 {
return mailer.Sender.Send(email)
Expand Down

0 comments on commit 813618e

Please sign in to comment.