mirror of
https://github.com/volatiletech/authboss.git
synced 2025-01-24 05:17:10 +02:00
Add real smtp tests
- Allow users to test the smtp mailer by sending themselves e-mails - Add a protection to ensure that either the HTML or the Text body exists so we never send blank e-mails. - Fix a bug where if the html body was blank, the mime section for it was still set and therefore gmail and (probably) other clients would show a blank e-mail instead of the content in the text e-mails.
This commit is contained in:
parent
83d912fd82
commit
e9c5d3e13c
4
.gitignore
vendored
4
.gitignore
vendored
@ -25,4 +25,6 @@ _testmain.go
|
||||
*.out
|
||||
|
||||
*.iml
|
||||
.idea
|
||||
.idea
|
||||
|
||||
defaults/smtp_mailer_test.json
|
||||
|
@ -10,10 +10,14 @@ import (
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/volatiletech/authboss"
|
||||
)
|
||||
|
||||
// NewSMTPMailer creates an SMTP Mailer to send emails with.
|
||||
// An example usage might be something like:
|
||||
//
|
||||
// NewSMTPMailer("smtp.gmail.com", smtp.PlainAuth("", "admin@yoursite.com", "password", "smtp.gmail.com"))
|
||||
func NewSMTPMailer(server string, auth smtp.Auth) *SMTPMailer {
|
||||
if len(server) == 0 {
|
||||
panic("SMTP Mailer must be created with a server string.")
|
||||
@ -31,6 +35,10 @@ type SMTPMailer struct {
|
||||
|
||||
// Send an e-mail
|
||||
func (s SMTPMailer) Send(ctx context.Context, mail authboss.Email) error {
|
||||
if len(mail.TextBody) == 0 && len(mail.HTMLBody) == 0 {
|
||||
return errors.New("refusing to send mail without text or html body")
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
data := struct {
|
||||
@ -110,15 +118,19 @@ MIME-Version: 1.0
|
||||
Content-Type: multipart/alternative; boundary="==============={{.Boundary}}=="
|
||||
Content-Transfer-Encoding: 7bit
|
||||
|
||||
{{if .Mail.TextBody -}}
|
||||
--==============={{.Boundary}}==
|
||||
Content-Type: text/plain; charset=UTF-8
|
||||
Content-Transfer-Encoding: 7bit
|
||||
|
||||
{{.Mail.TextBody}}
|
||||
{{end -}}
|
||||
{{if .Mail.HTMLBody -}}
|
||||
--==============={{.Boundary}}==
|
||||
Content-Type: text/html; charset=UTF-8
|
||||
Content-Transfer-Encoding: 7bit
|
||||
|
||||
{{.Mail.HTMLBody}}
|
||||
{{end -}}
|
||||
--==============={{.Boundary}}==--
|
||||
`))
|
||||
|
@ -1,9 +1,77 @@
|
||||
package defaults
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/smtp"
|
||||
"testing"
|
||||
|
||||
"github.com/volatiletech/authboss"
|
||||
)
|
||||
|
||||
var (
|
||||
flagTestSMTPMailer = flag.Bool("test-smtp-mailer", false, "Test the smtp mailer")
|
||||
)
|
||||
|
||||
func TestSMTPMailer(t *testing.T) {
|
||||
t.Skip("must implement test against real smtp servers here")
|
||||
t.Parallel()
|
||||
|
||||
if !*flagTestSMTPMailer {
|
||||
t.Skip("SMTP Mailer Testing not enabled (-test-smtp-mailer flag)")
|
||||
}
|
||||
|
||||
creds := struct {
|
||||
Server string `json:"server,omitempty"`
|
||||
Port int `json:"port,omitempty"`
|
||||
Email string `json:"email,omitempty"`
|
||||
Password string `json:"password,omitempty"`
|
||||
}{}
|
||||
|
||||
b, err := ioutil.ReadFile("smtp_mailer_test.json")
|
||||
if err != nil {
|
||||
t.Fatal(`error reading file: "smtp_mailer_test.json`, err)
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(b, &creds); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
server := fmt.Sprintf("%s:%d", creds.Server, creds.Port)
|
||||
mailer := NewSMTPMailer(server, smtp.PlainAuth("", creds.Email, creds.Password, creds.Server))
|
||||
|
||||
mail := authboss.Email{
|
||||
From: creds.Email,
|
||||
To: []string{creds.Email},
|
||||
Subject: "Authboss Test SMTP Mailer",
|
||||
}
|
||||
|
||||
txtOnly := mail
|
||||
txtOnly.Subject += ": Text Content"
|
||||
txtOnly.TextBody = "Authboss\nSMTP\nTest\nWith\nNewlines"
|
||||
|
||||
if err = mailer.Send(context.Background(), txtOnly); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
htmlOnly := mail
|
||||
htmlOnly.Subject += ": HTML Content"
|
||||
htmlOnly.HTMLBody = "Authboss<br>Test<br>\nWith<br>Newlines\nand<br>breaks"
|
||||
|
||||
if err = mailer.Send(context.Background(), htmlOnly); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
mixed := mail
|
||||
mixed.Subject += ": Mixed Content"
|
||||
mixed.HTMLBody = htmlOnly.HTMLBody
|
||||
mixed.TextBody = txtOnly.TextBody
|
||||
|
||||
if err = mailer.Send(context.Background(), mixed); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSMTPMailerPanic(t *testing.T) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user