Skip to content

More refactorings #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove debug delimiter
Makes testing unneccessarily hard.
  • Loading branch information
dmke committed Apr 6, 2018
commit 344d404ed98575a22ac1583edb7fe96fa7f872de
3 changes: 0 additions & 3 deletions sendmail.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var _, debug = os.LookupEnv("DEBUG")

// SendmailDefault points to the default sendmail binary location.
const SendmailDefault = "/usr/sbin/sendmail"
const debugDelimiter = "\n----------------------------------------------------------------------"

// Mail defines basic mail structure and headers
type Mail struct {
Expand Down Expand Up @@ -68,9 +67,7 @@ func (m *Mail) Send() error {
}
m.Header.Set("To", strings.Join(to, ", "))
if m.debugOut != nil {
fmt.Println(debugDelimiter)
m.WriteTo(m.debugOut)
fmt.Println(debugDelimiter)
return nil
}

Expand Down
32 changes: 32 additions & 0 deletions sendmail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"io"
"net/mail"
"os"
"strings"
"testing"
)

Expand Down Expand Up @@ -50,6 +52,36 @@ func testSend(t *testing.T, withDebug bool) {
}
}

func TestTextMail(t *testing.T) {
var buf bytes.Buffer
sm := New(
Subject("Cześć"),
From("Michał", "me@"+domain),
To("Ktoś", "info@"+domain),
To("Ktoś2", "info2@"+domain),
DebugOutput(&buf),
)
io.WriteString(&sm.Text, ":)\r\n")

expected := strings.Join([]string{
"Content-Type: text/plain; charset=UTF-8",
"From: =?utf-8?q?Micha=C5=82?= <[email protected]>",
"Subject: =?utf-8?q?Cze=C5=9B=C4=87?=",
"To: =?utf-8?q?Kto=C5=9B?= <[email protected]>, =?utf-8?q?Kto=C5=9B2?= <[email protected]>",
"",
":)",
"",
}, "\r\n")

if err := sm.Send(); err != nil {
t.Errorf("Error writing to buffer: %v", err)
}
if actual := buf.String(); actual != expected {
fmt.Fprintln(os.Stderr, actual)
t.Errorf("Unexpected mail content")
}
}

func TestFromError(t *testing.T) {
sm := Mail{
To: []*mail.Address{maddr("Ktoś", "info@")},
Expand Down