2014-10-15 23:47:07 +08:00
|
|
|
package gomail
|
|
|
|
|
|
|
|
import (
|
2015-07-03 05:08:18 +08:00
|
|
|
"bytes"
|
2014-10-15 23:47:07 +08:00
|
|
|
"encoding/base64"
|
2015-06-30 17:55:03 +08:00
|
|
|
"io"
|
2014-10-15 23:47:07 +08:00
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
func init() {
|
|
|
|
now = func() time.Time {
|
|
|
|
return time.Date(2014, 06, 25, 17, 46, 0, 0, time.UTC)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-15 23:47:07 +08:00
|
|
|
type message struct {
|
|
|
|
from string
|
|
|
|
to []string
|
|
|
|
content string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessage(t *testing.T) {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage()
|
|
|
|
m.SetAddressHeader("From", "from@example.com", "Señor From")
|
|
|
|
m.SetHeader("To", m.FormatAddress("to@example.com", "Señor To"), "tobis@example.com")
|
|
|
|
m.SetAddressHeader("Cc", "cc@example.com", "A, B")
|
|
|
|
m.SetAddressHeader("X-To", "ccbis@example.com", "à, b")
|
|
|
|
m.SetDateHeader("X-Date", now())
|
|
|
|
m.SetHeader("X-Date-2", m.FormatDate(now()))
|
|
|
|
m.SetHeader("Subject", "¡Hola, señor!")
|
|
|
|
m.SetHeaders(map[string][]string{
|
2014-10-15 23:47:07 +08:00
|
|
|
"X-Headers": {"Test", "Café"},
|
|
|
|
})
|
2015-07-15 05:34:21 +08:00
|
|
|
m.SetBody("text/plain", "¡Hola, señor!")
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
want := &message{
|
2014-10-24 00:46:32 +08:00
|
|
|
from: "from@example.com",
|
2014-10-15 23:47:07 +08:00
|
|
|
to: []string{
|
2014-10-24 00:46:32 +08:00
|
|
|
"to@example.com",
|
2014-10-15 23:47:07 +08:00
|
|
|
"tobis@example.com",
|
2015-03-15 01:41:58 +08:00
|
|
|
"cc@example.com",
|
2014-10-15 23:47:07 +08:00
|
|
|
},
|
2015-06-27 23:20:04 +08:00
|
|
|
content: "From: =?UTF-8?q?Se=C3=B1or_From?= <from@example.com>\r\n" +
|
|
|
|
"To: =?UTF-8?q?Se=C3=B1or_To?= <to@example.com>, tobis@example.com\r\n" +
|
2015-03-15 01:41:58 +08:00
|
|
|
"Cc: \"A, B\" <cc@example.com>\r\n" +
|
2015-06-27 23:20:04 +08:00
|
|
|
"X-To: =?UTF-8?b?w6AsIGI=?= <ccbis@example.com>\r\n" +
|
2015-02-28 23:46:44 +08:00
|
|
|
"X-Date: Wed, 25 Jun 2014 17:46:00 +0000\r\n" +
|
|
|
|
"X-Date-2: Wed, 25 Jun 2014 17:46:00 +0000\r\n" +
|
2015-06-27 23:20:04 +08:00
|
|
|
"X-Headers: Test, =?UTF-8?q?Caf=C3=A9?=\r\n" +
|
|
|
|
"Subject: =?UTF-8?q?=C2=A1Hola,_se=C3=B1or!?=\r\n" +
|
2014-10-15 23:47:07 +08:00
|
|
|
"Content-Type: text/plain; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: quoted-printable\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"=C2=A1Hola, se=C3=B1or!",
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
testMessage(t, m, 0, want)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBodyWriter(t *testing.T) {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage()
|
|
|
|
m.SetHeader("From", "from@example.com")
|
|
|
|
m.SetHeader("To", "to@example.com")
|
|
|
|
m.AddAlternativeWriter("text/plain", func(w io.Writer) error {
|
2015-07-08 06:26:19 +08:00
|
|
|
_, err := w.Write([]byte("Test message"))
|
|
|
|
return err
|
|
|
|
})
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
want := &message{
|
2014-10-15 23:47:07 +08:00
|
|
|
from: "from@example.com",
|
|
|
|
to: []string{"to@example.com"},
|
|
|
|
content: "From: from@example.com\r\n" +
|
|
|
|
"To: to@example.com\r\n" +
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: quoted-printable\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"Test message",
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
testMessage(t, m, 0, want)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCustomMessage(t *testing.T) {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage(SetCharset("ISO-8859-1"), SetEncoding(Base64))
|
|
|
|
m.SetHeaders(map[string][]string{
|
2014-10-15 23:47:07 +08:00
|
|
|
"From": {"from@example.com"},
|
|
|
|
"To": {"to@example.com"},
|
|
|
|
"Subject": {"Café"},
|
|
|
|
})
|
2015-07-15 05:34:21 +08:00
|
|
|
m.SetBody("text/html", "¡Hola, señor!")
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
want := &message{
|
2014-10-15 23:47:07 +08:00
|
|
|
from: "from@example.com",
|
|
|
|
to: []string{"to@example.com"},
|
|
|
|
content: "From: from@example.com\r\n" +
|
|
|
|
"To: to@example.com\r\n" +
|
2015-06-27 23:20:04 +08:00
|
|
|
"Subject: =?ISO-8859-1?b?Q2Fmw6k=?=\r\n" +
|
2014-10-15 23:47:07 +08:00
|
|
|
"Content-Type: text/html; charset=ISO-8859-1\r\n" +
|
|
|
|
"Content-Transfer-Encoding: base64\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"wqFIb2xhLCBzZcOxb3Ih",
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
testMessage(t, m, 0, want)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2014-10-28 15:35:50 +08:00
|
|
|
func TestUnencodedMessage(t *testing.T) {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage(SetEncoding(Unencoded))
|
|
|
|
m.SetHeaders(map[string][]string{
|
2014-10-28 15:35:50 +08:00
|
|
|
"From": {"from@example.com"},
|
|
|
|
"To": {"to@example.com"},
|
|
|
|
"Subject": {"Café"},
|
|
|
|
})
|
2015-07-15 05:34:21 +08:00
|
|
|
m.SetBody("text/html", "¡Hola, señor!")
|
2014-10-28 15:35:50 +08:00
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
want := &message{
|
2014-10-28 15:35:50 +08:00
|
|
|
from: "from@example.com",
|
|
|
|
to: []string{"to@example.com"},
|
|
|
|
content: "From: from@example.com\r\n" +
|
|
|
|
"To: to@example.com\r\n" +
|
2015-06-27 23:20:04 +08:00
|
|
|
"Subject: =?UTF-8?q?Caf=C3=A9?=\r\n" +
|
2014-10-28 15:35:50 +08:00
|
|
|
"Content-Type: text/html; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: 8bit\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"¡Hola, señor!",
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
testMessage(t, m, 0, want)
|
2014-10-28 15:35:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-15 23:47:07 +08:00
|
|
|
func TestRecipients(t *testing.T) {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage()
|
|
|
|
m.SetHeaders(map[string][]string{
|
2014-10-15 23:47:07 +08:00
|
|
|
"From": {"from@example.com"},
|
|
|
|
"To": {"to@example.com"},
|
|
|
|
"Cc": {"cc@example.com"},
|
|
|
|
"Bcc": {"bcc1@example.com", "bcc2@example.com"},
|
|
|
|
"Subject": {"Hello!"},
|
|
|
|
})
|
2015-07-15 05:34:21 +08:00
|
|
|
m.SetBody("text/plain", "Test message")
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
want := &message{
|
2014-10-15 23:47:07 +08:00
|
|
|
from: "from@example.com",
|
2015-07-03 03:10:45 +08:00
|
|
|
to: []string{"to@example.com", "cc@example.com", "bcc1@example.com", "bcc2@example.com"},
|
2014-10-15 23:47:07 +08:00
|
|
|
content: "From: from@example.com\r\n" +
|
|
|
|
"To: to@example.com\r\n" +
|
|
|
|
"Cc: cc@example.com\r\n" +
|
|
|
|
"Subject: Hello!\r\n" +
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: quoted-printable\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"Test message",
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
testMessage(t, m, 0, want)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAlternative(t *testing.T) {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage()
|
|
|
|
m.SetHeader("From", "from@example.com")
|
|
|
|
m.SetHeader("To", "to@example.com")
|
|
|
|
m.SetBody("text/plain", "¡Hola, señor!")
|
|
|
|
m.AddAlternative("text/html", "¡<b>Hola</b>, <i>señor</i>!</h1>")
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
want := &message{
|
2014-10-15 23:47:07 +08:00
|
|
|
from: "from@example.com",
|
|
|
|
to: []string{"to@example.com"},
|
|
|
|
content: "From: from@example.com\r\n" +
|
|
|
|
"To: to@example.com\r\n" +
|
|
|
|
"Content-Type: multipart/alternative; boundary=_BOUNDARY_1_\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"--_BOUNDARY_1_\r\n" +
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: quoted-printable\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"=C2=A1Hola, se=C3=B1or!\r\n" +
|
|
|
|
"--_BOUNDARY_1_\r\n" +
|
|
|
|
"Content-Type: text/html; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: quoted-printable\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"=C2=A1<b>Hola</b>, <i>se=C3=B1or</i>!</h1>\r\n" +
|
|
|
|
"--_BOUNDARY_1_--\r\n",
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
testMessage(t, m, 1, want)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttachmentOnly(t *testing.T) {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage()
|
|
|
|
m.SetHeader("From", "from@example.com")
|
|
|
|
m.SetHeader("To", "to@example.com")
|
|
|
|
m.Attach(testFile("/tmp/test.pdf"))
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
want := &message{
|
2014-10-15 23:47:07 +08:00
|
|
|
from: "from@example.com",
|
|
|
|
to: []string{"to@example.com"},
|
|
|
|
content: "From: from@example.com\r\n" +
|
|
|
|
"To: to@example.com\r\n" +
|
|
|
|
"Content-Type: application/pdf; name=\"test.pdf\"\r\n" +
|
|
|
|
"Content-Disposition: attachment; filename=\"test.pdf\"\r\n" +
|
|
|
|
"Content-Transfer-Encoding: base64\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
base64.StdEncoding.EncodeToString([]byte("Content of test.pdf")),
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
testMessage(t, m, 0, want)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttachment(t *testing.T) {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage()
|
|
|
|
m.SetHeader("From", "from@example.com")
|
|
|
|
m.SetHeader("To", "to@example.com")
|
|
|
|
m.SetBody("text/plain", "Test")
|
|
|
|
m.Attach(testFile("/tmp/test.pdf"))
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
want := &message{
|
2014-10-15 23:47:07 +08:00
|
|
|
from: "from@example.com",
|
|
|
|
to: []string{"to@example.com"},
|
|
|
|
content: "From: from@example.com\r\n" +
|
|
|
|
"To: to@example.com\r\n" +
|
|
|
|
"Content-Type: multipart/mixed; boundary=_BOUNDARY_1_\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"--_BOUNDARY_1_\r\n" +
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: quoted-printable\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"Test\r\n" +
|
|
|
|
"--_BOUNDARY_1_\r\n" +
|
|
|
|
"Content-Type: application/pdf; name=\"test.pdf\"\r\n" +
|
|
|
|
"Content-Disposition: attachment; filename=\"test.pdf\"\r\n" +
|
|
|
|
"Content-Transfer-Encoding: base64\r\n" +
|
|
|
|
"\r\n" +
|
2015-07-09 01:53:11 +08:00
|
|
|
base64.StdEncoding.EncodeToString([]byte("Content of test.pdf")) + "\r\n" +
|
2014-10-15 23:47:07 +08:00
|
|
|
"--_BOUNDARY_1_--\r\n",
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
testMessage(t, m, 1, want)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttachmentsOnly(t *testing.T) {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage()
|
|
|
|
m.SetHeader("From", "from@example.com")
|
|
|
|
m.SetHeader("To", "to@example.com")
|
|
|
|
m.Attach(testFile("/tmp/test.pdf"))
|
|
|
|
m.Attach(testFile("/tmp/test.zip"))
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
want := &message{
|
2014-10-15 23:47:07 +08:00
|
|
|
from: "from@example.com",
|
|
|
|
to: []string{"to@example.com"},
|
|
|
|
content: "From: from@example.com\r\n" +
|
|
|
|
"To: to@example.com\r\n" +
|
|
|
|
"Content-Type: multipart/mixed; boundary=_BOUNDARY_1_\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"--_BOUNDARY_1_\r\n" +
|
|
|
|
"Content-Type: application/pdf; name=\"test.pdf\"\r\n" +
|
|
|
|
"Content-Disposition: attachment; filename=\"test.pdf\"\r\n" +
|
|
|
|
"Content-Transfer-Encoding: base64\r\n" +
|
|
|
|
"\r\n" +
|
2015-07-09 01:53:11 +08:00
|
|
|
base64.StdEncoding.EncodeToString([]byte("Content of test.pdf")) + "\r\n" +
|
2014-10-15 23:47:07 +08:00
|
|
|
"--_BOUNDARY_1_\r\n" +
|
|
|
|
"Content-Type: application/zip; name=\"test.zip\"\r\n" +
|
|
|
|
"Content-Disposition: attachment; filename=\"test.zip\"\r\n" +
|
|
|
|
"Content-Transfer-Encoding: base64\r\n" +
|
|
|
|
"\r\n" +
|
2015-07-09 01:53:11 +08:00
|
|
|
base64.StdEncoding.EncodeToString([]byte("Content of test.zip")) + "\r\n" +
|
2014-10-15 23:47:07 +08:00
|
|
|
"--_BOUNDARY_1_--\r\n",
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
testMessage(t, m, 1, want)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAttachments(t *testing.T) {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage()
|
|
|
|
m.SetHeader("From", "from@example.com")
|
|
|
|
m.SetHeader("To", "to@example.com")
|
|
|
|
m.SetBody("text/plain", "Test")
|
|
|
|
m.Attach(testFile("/tmp/test.pdf"))
|
|
|
|
m.Attach(testFile("/tmp/test.zip"))
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
want := &message{
|
2014-10-15 23:47:07 +08:00
|
|
|
from: "from@example.com",
|
|
|
|
to: []string{"to@example.com"},
|
|
|
|
content: "From: from@example.com\r\n" +
|
|
|
|
"To: to@example.com\r\n" +
|
|
|
|
"Content-Type: multipart/mixed; boundary=_BOUNDARY_1_\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"--_BOUNDARY_1_\r\n" +
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: quoted-printable\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"Test\r\n" +
|
|
|
|
"--_BOUNDARY_1_\r\n" +
|
|
|
|
"Content-Type: application/pdf; name=\"test.pdf\"\r\n" +
|
|
|
|
"Content-Disposition: attachment; filename=\"test.pdf\"\r\n" +
|
|
|
|
"Content-Transfer-Encoding: base64\r\n" +
|
|
|
|
"\r\n" +
|
2015-07-09 01:53:11 +08:00
|
|
|
base64.StdEncoding.EncodeToString([]byte("Content of test.pdf")) + "\r\n" +
|
2014-10-15 23:47:07 +08:00
|
|
|
"--_BOUNDARY_1_\r\n" +
|
|
|
|
"Content-Type: application/zip; name=\"test.zip\"\r\n" +
|
|
|
|
"Content-Disposition: attachment; filename=\"test.zip\"\r\n" +
|
|
|
|
"Content-Transfer-Encoding: base64\r\n" +
|
|
|
|
"\r\n" +
|
2015-07-09 01:53:11 +08:00
|
|
|
base64.StdEncoding.EncodeToString([]byte("Content of test.zip")) + "\r\n" +
|
2014-10-15 23:47:07 +08:00
|
|
|
"--_BOUNDARY_1_--\r\n",
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
testMessage(t, m, 1, want)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmbedded(t *testing.T) {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage()
|
|
|
|
m.SetHeader("From", "from@example.com")
|
|
|
|
m.SetHeader("To", "to@example.com")
|
2015-07-09 01:53:11 +08:00
|
|
|
f := testFile("image1.jpg")
|
|
|
|
f.Header["Content-ID"] = []string{"<test-content-id>"}
|
2015-07-15 05:34:21 +08:00
|
|
|
m.Embed(f)
|
|
|
|
m.Embed(testFile("image2.jpg"))
|
|
|
|
m.SetBody("text/plain", "Test")
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
want := &message{
|
2014-10-15 23:47:07 +08:00
|
|
|
from: "from@example.com",
|
|
|
|
to: []string{"to@example.com"},
|
|
|
|
content: "From: from@example.com\r\n" +
|
|
|
|
"To: to@example.com\r\n" +
|
|
|
|
"Content-Type: multipart/related; boundary=_BOUNDARY_1_\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"--_BOUNDARY_1_\r\n" +
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: quoted-printable\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"Test\r\n" +
|
|
|
|
"--_BOUNDARY_1_\r\n" +
|
2014-12-23 18:23:01 +08:00
|
|
|
"Content-Type: image/jpeg; name=\"image1.jpg\"\r\n" +
|
|
|
|
"Content-Disposition: inline; filename=\"image1.jpg\"\r\n" +
|
2015-02-19 03:46:05 +08:00
|
|
|
"Content-ID: <test-content-id>\r\n" +
|
2014-10-15 23:47:07 +08:00
|
|
|
"Content-Transfer-Encoding: base64\r\n" +
|
|
|
|
"\r\n" +
|
2015-07-09 01:53:11 +08:00
|
|
|
base64.StdEncoding.EncodeToString([]byte("Content of image1.jpg")) + "\r\n" +
|
2014-12-23 18:23:01 +08:00
|
|
|
"--_BOUNDARY_1_\r\n" +
|
|
|
|
"Content-Type: image/jpeg; name=\"image2.jpg\"\r\n" +
|
|
|
|
"Content-Disposition: inline; filename=\"image2.jpg\"\r\n" +
|
|
|
|
"Content-ID: <image2.jpg>\r\n" +
|
|
|
|
"Content-Transfer-Encoding: base64\r\n" +
|
|
|
|
"\r\n" +
|
2015-07-09 01:53:11 +08:00
|
|
|
base64.StdEncoding.EncodeToString([]byte("Content of image2.jpg")) + "\r\n" +
|
2014-10-15 23:47:07 +08:00
|
|
|
"--_BOUNDARY_1_--\r\n",
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
testMessage(t, m, 1, want)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFullMessage(t *testing.T) {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage()
|
|
|
|
m.SetHeader("From", "from@example.com")
|
|
|
|
m.SetHeader("To", "to@example.com")
|
|
|
|
m.SetBody("text/plain", "¡Hola, señor!")
|
|
|
|
m.AddAlternative("text/html", "¡<b>Hola</b>, <i>señor</i>!</h1>")
|
|
|
|
m.Attach(testFile("test.pdf"))
|
|
|
|
m.Embed(testFile("image.jpg"))
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
want := &message{
|
2014-10-15 23:47:07 +08:00
|
|
|
from: "from@example.com",
|
|
|
|
to: []string{"to@example.com"},
|
|
|
|
content: "From: from@example.com\r\n" +
|
|
|
|
"To: to@example.com\r\n" +
|
|
|
|
"Content-Type: multipart/mixed; boundary=_BOUNDARY_1_\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"--_BOUNDARY_1_\r\n" +
|
|
|
|
"Content-Type: multipart/related; boundary=_BOUNDARY_2_\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"--_BOUNDARY_2_\r\n" +
|
|
|
|
"Content-Type: multipart/alternative; boundary=_BOUNDARY_3_\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"--_BOUNDARY_3_\r\n" +
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: quoted-printable\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"=C2=A1Hola, se=C3=B1or!\r\n" +
|
|
|
|
"--_BOUNDARY_3_\r\n" +
|
|
|
|
"Content-Type: text/html; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: quoted-printable\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"=C2=A1<b>Hola</b>, <i>se=C3=B1or</i>!</h1>\r\n" +
|
|
|
|
"--_BOUNDARY_3_--\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"--_BOUNDARY_2_\r\n" +
|
|
|
|
"Content-Type: image/jpeg; name=\"image.jpg\"\r\n" +
|
|
|
|
"Content-Disposition: inline; filename=\"image.jpg\"\r\n" +
|
|
|
|
"Content-ID: <image.jpg>\r\n" +
|
|
|
|
"Content-Transfer-Encoding: base64\r\n" +
|
|
|
|
"\r\n" +
|
2015-07-09 01:53:11 +08:00
|
|
|
base64.StdEncoding.EncodeToString([]byte("Content of image.jpg")) + "\r\n" +
|
2014-10-15 23:47:07 +08:00
|
|
|
"--_BOUNDARY_2_--\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"--_BOUNDARY_1_\r\n" +
|
|
|
|
"Content-Type: application/pdf; name=\"test.pdf\"\r\n" +
|
|
|
|
"Content-Disposition: attachment; filename=\"test.pdf\"\r\n" +
|
|
|
|
"Content-Transfer-Encoding: base64\r\n" +
|
|
|
|
"\r\n" +
|
2015-07-09 01:53:11 +08:00
|
|
|
base64.StdEncoding.EncodeToString([]byte("Content of test.pdf")) + "\r\n" +
|
2014-10-15 23:47:07 +08:00
|
|
|
"--_BOUNDARY_1_--\r\n",
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
testMessage(t, m, 3, want)
|
2015-07-15 04:26:22 +08:00
|
|
|
|
|
|
|
want = &message{
|
|
|
|
from: "from@example.com",
|
|
|
|
to: []string{"to@example.com"},
|
|
|
|
content: "From: from@example.com\r\n" +
|
|
|
|
"To: to@example.com\r\n" +
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: quoted-printable\r\n" +
|
|
|
|
"\r\n" +
|
|
|
|
"Test reset",
|
|
|
|
}
|
2015-07-15 05:34:21 +08:00
|
|
|
m.Reset()
|
|
|
|
m.SetHeader("From", "from@example.com")
|
|
|
|
m.SetHeader("To", "to@example.com")
|
|
|
|
m.SetBody("text/plain", "Test reset")
|
|
|
|
testMessage(t, m, 0, want)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestQpLineLength(t *testing.T) {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage()
|
|
|
|
m.SetHeader("From", "from@example.com")
|
|
|
|
m.SetHeader("To", "to@example.com")
|
|
|
|
m.SetBody("text/plain",
|
2015-06-27 23:20:04 +08:00
|
|
|
strings.Repeat("0", 76)+"\r\n"+
|
2014-10-15 23:47:07 +08:00
|
|
|
strings.Repeat("0", 75)+"à\r\n"+
|
2015-01-20 20:15:52 +08:00
|
|
|
strings.Repeat("0", 74)+"à\r\n"+
|
|
|
|
strings.Repeat("0", 73)+"à\r\n"+
|
2015-06-27 23:20:04 +08:00
|
|
|
strings.Repeat("0", 72)+"à\r\n"+
|
|
|
|
strings.Repeat("0", 75)+"\r\n"+
|
|
|
|
strings.Repeat("0", 76)+"\n")
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
want := &message{
|
2014-10-15 23:47:07 +08:00
|
|
|
from: "from@example.com",
|
|
|
|
to: []string{"to@example.com"},
|
|
|
|
content: "From: from@example.com\r\n" +
|
|
|
|
"To: to@example.com\r\n" +
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: quoted-printable\r\n" +
|
|
|
|
"\r\n" +
|
2015-06-27 23:20:04 +08:00
|
|
|
strings.Repeat("0", 75) + "=\r\n0\r\n" +
|
2015-01-20 20:15:52 +08:00
|
|
|
strings.Repeat("0", 75) + "=\r\n=C3=A0\r\n" +
|
|
|
|
strings.Repeat("0", 74) + "=\r\n=C3=A0\r\n" +
|
2015-06-27 23:20:04 +08:00
|
|
|
strings.Repeat("0", 73) + "=\r\n=C3=A0\r\n" +
|
|
|
|
strings.Repeat("0", 72) + "=C3=\r\n=A0\r\n" +
|
|
|
|
strings.Repeat("0", 75) + "\r\n" +
|
|
|
|
strings.Repeat("0", 75) + "=\r\n0\r\n",
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
testMessage(t, m, 0, want)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBase64LineLength(t *testing.T) {
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage(SetCharset("UTF-8"), SetEncoding(Base64))
|
|
|
|
m.SetHeader("From", "from@example.com")
|
|
|
|
m.SetHeader("To", "to@example.com")
|
|
|
|
m.SetBody("text/plain", strings.Repeat("0", 58))
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
want := &message{
|
2014-10-15 23:47:07 +08:00
|
|
|
from: "from@example.com",
|
|
|
|
to: []string{"to@example.com"},
|
|
|
|
content: "From: from@example.com\r\n" +
|
|
|
|
"To: to@example.com\r\n" +
|
|
|
|
"Content-Type: text/plain; charset=UTF-8\r\n" +
|
|
|
|
"Content-Transfer-Encoding: base64\r\n" +
|
|
|
|
"\r\n" +
|
2015-01-20 20:15:52 +08:00
|
|
|
strings.Repeat("MDAw", 19) + "\r\nMA==",
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
testMessage(t, m, 0, want)
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
func testMessage(t *testing.T, m *Message, bCount int, want *message) {
|
|
|
|
err := Send(stubSendMail(t, bCount, want), m)
|
2014-10-15 23:47:07 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
func stubSendMail(t *testing.T, bCount int, want *message) SendFunc {
|
2015-07-15 05:34:21 +08:00
|
|
|
return func(from string, to []string, m io.WriterTo) error {
|
2014-10-15 23:47:07 +08:00
|
|
|
if from != want.from {
|
|
|
|
t.Fatalf("Invalid from, got %q, want %q", from, want.from)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(to) != len(want.to) {
|
|
|
|
t.Fatalf("Invalid recipient count, \ngot %d: %q\nwant %d: %q",
|
|
|
|
len(to), to,
|
|
|
|
len(want.to), want.to,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
for i := range want.to {
|
|
|
|
if to[i] != want.to[i] {
|
|
|
|
t.Fatalf("Invalid recipient, got %q, want %q",
|
|
|
|
to[i], want.to[i],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 05:08:18 +08:00
|
|
|
buf := new(bytes.Buffer)
|
2015-07-15 05:34:21 +08:00
|
|
|
_, err := m.WriteTo(buf)
|
2015-06-30 17:55:03 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2015-07-03 05:08:18 +08:00
|
|
|
got := buf.String()
|
2014-10-15 23:47:07 +08:00
|
|
|
wantMsg := string("Mime-Version: 1.0\r\n" +
|
2015-02-28 23:46:44 +08:00
|
|
|
"Date: Wed, 25 Jun 2014 17:46:00 +0000\r\n" +
|
2014-10-15 23:47:07 +08:00
|
|
|
want.content)
|
|
|
|
if bCount > 0 {
|
|
|
|
boundaries := getBoundaries(t, bCount, got)
|
|
|
|
for i, b := range boundaries {
|
|
|
|
wantMsg = strings.Replace(wantMsg, "_BOUNDARY_"+strconv.Itoa(i+1)+"_", b, -1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
compareBodies(t, got, wantMsg)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareBodies(t *testing.T, got, want string) {
|
|
|
|
// We cannot do a simple comparison since the ordering of headers' fields
|
|
|
|
// is random.
|
|
|
|
gotLines := strings.Split(got, "\r\n")
|
|
|
|
wantLines := strings.Split(want, "\r\n")
|
|
|
|
|
|
|
|
// We only test for too many lines, missing lines are tested after
|
|
|
|
if len(gotLines) > len(wantLines) {
|
|
|
|
t.Fatalf("Message has too many lines, \ngot %d:\n%s\nwant %d:\n%s", len(gotLines), got, len(wantLines), want)
|
|
|
|
}
|
|
|
|
|
|
|
|
isInHeader := true
|
|
|
|
headerStart := 0
|
|
|
|
for i, line := range wantLines {
|
|
|
|
if line == gotLines[i] {
|
|
|
|
if line == "" {
|
|
|
|
isInHeader = false
|
|
|
|
} else if !isInHeader && len(line) > 2 && line[:2] == "--" {
|
|
|
|
isInHeader = true
|
|
|
|
headerStart = i + 1
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isInHeader {
|
|
|
|
missingLine(t, line, got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
isMissing := true
|
|
|
|
for j := headerStart; j < len(gotLines); j++ {
|
|
|
|
if gotLines[j] == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if gotLines[j] == line {
|
|
|
|
isMissing = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isMissing {
|
|
|
|
missingLine(t, line, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func missingLine(t *testing.T, line, got, want string) {
|
|
|
|
t.Fatalf("Missing line %q\ngot:\n%s\nwant:\n%s", line, got, want)
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
func getBoundaries(t *testing.T, count int, m string) []string {
|
|
|
|
if matches := boundaryRegExp.FindAllStringSubmatch(m, count); matches != nil {
|
2014-10-15 23:47:07 +08:00
|
|
|
boundaries := make([]string, count)
|
|
|
|
for i, match := range matches {
|
|
|
|
boundaries[i] = match[1]
|
|
|
|
}
|
|
|
|
return boundaries
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Fatal("Boundary not found in body")
|
|
|
|
return []string{""}
|
|
|
|
}
|
|
|
|
|
|
|
|
var boundaryRegExp = regexp.MustCompile("boundary=(\\w+)")
|
|
|
|
|
2015-07-09 01:53:11 +08:00
|
|
|
func testFile(name string) *File {
|
|
|
|
f := NewFile(name)
|
|
|
|
f.Copier = func(w io.Writer) error {
|
|
|
|
_, err := w.Write([]byte("Content of " + filepath.Base(f.Name)))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2014-10-15 23:47:07 +08:00
|
|
|
func BenchmarkFull(b *testing.B) {
|
2015-07-15 04:26:22 +08:00
|
|
|
buf := new(bytes.Buffer)
|
2015-07-15 05:34:21 +08:00
|
|
|
emptyFunc := func(from string, to []string, m io.WriterTo) error {
|
|
|
|
m.WriteTo(buf)
|
2015-07-15 04:26:22 +08:00
|
|
|
buf.Reset()
|
2014-10-15 23:47:07 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
m := NewMessage()
|
2015-07-15 04:26:22 +08:00
|
|
|
b.ResetTimer()
|
2014-10-15 23:47:07 +08:00
|
|
|
for n := 0; n < b.N; n++ {
|
2015-07-15 05:34:21 +08:00
|
|
|
m.SetAddressHeader("From", "from@example.com", "Señor From")
|
|
|
|
m.SetHeaders(map[string][]string{
|
2014-10-15 23:47:07 +08:00
|
|
|
"To": {"to@example.com"},
|
|
|
|
"Cc": {"cc@example.com"},
|
|
|
|
"Bcc": {"bcc1@example.com", "bcc2@example.com"},
|
|
|
|
"Subject": {"¡Hola, señor!"},
|
|
|
|
})
|
2015-07-15 05:34:21 +08:00
|
|
|
m.SetBody("text/plain", "¡Hola, señor!")
|
|
|
|
m.AddAlternative("text/html", "<p>¡Hola, señor!</p>")
|
|
|
|
m.Attach(testFile("benchmark.txt"))
|
|
|
|
m.Embed(testFile("benchmark.jpg"))
|
2014-10-15 23:47:07 +08:00
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
if err := Send(SendFunc(emptyFunc), m); err != nil {
|
2014-10-15 23:47:07 +08:00
|
|
|
panic(err)
|
|
|
|
}
|
2015-07-15 05:34:21 +08:00
|
|
|
m.Reset()
|
2014-10-15 23:47:07 +08:00
|
|
|
}
|
|
|
|
}
|