123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- package email
- import (
- "testing"
- "bytes"
- "crypto/rand"
- "io"
- "io/ioutil"
- "mime"
- "mime/multipart"
- "mime/quotedprintable"
- "net/mail"
- "net/smtp"
- )
- func TestEmailTextHtmlAttachment(t *testing.T) {
- e := NewEmail()
- e.From = "Jordan Wright <test@example.com>"
- e.To = []string{"test@example.com"}
- e.Bcc = []string{"test_bcc@example.com"}
- e.Cc = []string{"test_cc@example.com"}
- e.Subject = "Awesome Subject"
- e.Text = []byte("Text Body is, of course, supported!\n")
- e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
- e.Attach(bytes.NewBufferString("Rad attachement"), "rad.txt", "text/plain; charset=utf-8")
- raw, err := e.Bytes()
- if err != nil {
- t.Fatal("Failed to render message: ", e)
- }
- msg, err := mail.ReadMessage(bytes.NewBuffer(raw))
- if err != nil {
- t.Fatal("Could not parse rendered message: ", err)
- }
- expectedHeaders := map[string]string{
- "To": "test@example.com",
- "From": "Jordan Wright <test@example.com>",
- "Cc": "test_cc@example.com",
- "Subject": "Awesome Subject",
- }
- for header, expected := range expectedHeaders {
- if val := msg.Header.Get(header); val != expected {
- t.Errorf("Wrong value for message header %s: %v != %v", header, expected, val)
- }
- }
- // Were the right headers set?
- ct := msg.Header.Get("Content-type")
- mt, params, err := mime.ParseMediaType(ct)
- if err != nil {
- t.Fatal("Content-type header is invalid: ", ct)
- } else if mt != "multipart/mixed" {
- t.Fatalf("Content-type expected \"multipart/mixed\", not %v", mt)
- }
- b := params["boundary"]
- if b == "" {
- t.Fatalf("Invalid or missing boundary parameter: ", b)
- }
- if len(params) != 1 {
- t.Fatal("Unexpected content-type parameters")
- }
- // Is the generated message parsable?
- mixed := multipart.NewReader(msg.Body, params["boundary"])
- text, err := mixed.NextPart()
- if err != nil {
- t.Fatalf("Could not find text component of email: ", err)
- }
- // Does the text portion match what we expect?
- mt, params, err = mime.ParseMediaType(text.Header.Get("Content-type"))
- if err != nil {
- t.Fatal("Could not parse message's Content-Type")
- } else if mt != "multipart/alternative" {
- t.Fatal("Message missing multipart/alternative")
- }
- mpReader := multipart.NewReader(text, params["boundary"])
- part, err := mpReader.NextPart()
- if err != nil {
- t.Fatal("Could not read plain text component of message: ", err)
- }
- plainText, err := ioutil.ReadAll(part)
- if err != nil {
- t.Fatal("Could not read plain text component of message: ", err)
- }
- if !bytes.Equal(plainText, []byte("Text Body is, of course, supported!\r\n")) {
- t.Fatalf("Plain text is broken: %#q", plainText)
- }
- // Check attachments.
- _, err = mixed.NextPart()
- if err != nil {
- t.Fatalf("Could not find attachemnt compoenent of email: ", err)
- }
- if _, err = mixed.NextPart(); err != io.EOF {
- t.Error("Expected only text and one attachement!")
- }
- }
- func ExampleGmail() {
- e := NewEmail()
- e.From = "Jordan Wright <test@gmail.com>"
- e.To = []string{"test@example.com"}
- e.Bcc = []string{"test_bcc@example.com"}
- e.Cc = []string{"test_cc@example.com"}
- e.Subject = "Awesome Subject"
- e.Text = []byte("Text Body is, of course, supported!\n")
- e.HTML = []byte("<h1>Fancy Html is supported, too!</h1>\n")
- e.Send("smtp.gmail.com:587", smtp.PlainAuth("", e.From, "password123", "smtp.gmail.com"))
- }
- func ExampleAttach() {
- e := NewEmail()
- e.AttachFile("test.txt")
- }
- func Test_base64Wrap(t *testing.T) {
- file := "I'm a file long enough to force the function to wrap a\n" +
- "couple of lines, but I stop short of the end of one line and\n" +
- "have some padding dangling at the end."
- encoded := "SSdtIGEgZmlsZSBsb25nIGVub3VnaCB0byBmb3JjZSB0aGUgZnVuY3Rpb24gdG8gd3JhcCBhCmNv\r\n" +
- "dXBsZSBvZiBsaW5lcywgYnV0IEkgc3RvcCBzaG9ydCBvZiB0aGUgZW5kIG9mIG9uZSBsaW5lIGFu\r\n" +
- "ZApoYXZlIHNvbWUgcGFkZGluZyBkYW5nbGluZyBhdCB0aGUgZW5kLg==\r\n"
- var buf bytes.Buffer
- base64Wrap(&buf, []byte(file))
- if !bytes.Equal(buf.Bytes(), []byte(encoded)) {
- t.Fatalf("Encoded file does not match expected: %#q != %#q", string(buf.Bytes()), encoded)
- }
- }
- // *Since the mime library in use by ```email``` is now in the stdlib, this test is deprecated
- func Test_quotedPrintEncode(t *testing.T) {
- var buf bytes.Buffer
- text := []byte("Dear reader!\n\n" +
- "This is a test email to try and capture some of the corner cases that exist within\n" +
- "the quoted-printable encoding.\n" +
- "There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
- "it can come out a little weird. Also, we need to support unicode so here's a fish: 🐟\n")
- expected := []byte("Dear reader!\r\n\r\n" +
- "This is a test email to try and capture some of the corner cases that exist=\r\n" +
- " within\r\n" +
- "the quoted-printable encoding.\r\n" +
- "There are some wacky parts like =3D, and this input assumes UNIX line break=\r\n" +
- "s so\r\n" +
- "it can come out a little weird. Also, we need to support unicode so here's=\r\n" +
- " a fish: =F0=9F=90=9F\r\n")
- qp := quotedprintable.NewWriter(&buf)
- if _, err := qp.Write(text); err != nil {
- t.Fatal("quotePrintEncode: ", err)
- }
- if err := qp.Close(); err != nil {
- t.Fatal("Error closing writer", err)
- }
- if b := buf.Bytes(); !bytes.Equal(b, expected) {
- t.Errorf("quotedPrintEncode generated incorrect results: %#q != %#q", b, expected)
- }
- }
- // *Since the mime library in use by ```email``` is now in the stdlib, this test is deprecated
- func Test_quotedPrintDecode(t *testing.T) {
- text := []byte("Dear reader!\r\n\r\n" +
- "This is a test email to try and capture some of the corner cases that exist=\r\n" +
- " within\r\n" +
- "the quoted-printable encoding.\r\n" +
- "There are some wacky parts like =3D, and this input assumes UNIX line break=\r\n" +
- "s so\r\n" +
- "it can come out a little weird. Also, we need to support unicode so here's=\r\n" +
- " a fish: =F0=9F=90=9F\r\n")
- expected := []byte("Dear reader!\r\n\r\n" +
- "This is a test email to try and capture some of the corner cases that exist within\r\n" +
- "the quoted-printable encoding.\r\n" +
- "There are some wacky parts like =, and this input assumes UNIX line breaks so\r\n" +
- "it can come out a little weird. Also, we need to support unicode so here's a fish: 🐟\r\n")
- qp := quotedprintable.NewReader(bytes.NewReader(text))
- got, err := ioutil.ReadAll(qp)
- if err != nil {
- t.Fatal("quotePrintDecode: ", err)
- }
- if !bytes.Equal(got, expected) {
- t.Errorf("quotedPrintDecode generated incorrect results: %#q != %#q", got, expected)
- }
- }
- func Benchmark_base64Wrap(b *testing.B) {
- // Reasonable base case; 128K random bytes
- file := make([]byte, 128*1024)
- if _, err := rand.Read(file); err != nil {
- panic(err)
- }
- for i := 0; i <= b.N; i++ {
- base64Wrap(ioutil.Discard, file)
- }
- }
- /*
- func Test_encodeHeader(t *testing.T) {
- // Plain ASCII (unchanged).
- subject := "Plain ASCII email subject, !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
- expected := []byte("Plain ASCII email subject, !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~")
- b := encodeHeader("Subject", subject)
- if !bytes.Equal(b, expected) {
- t.Errorf("encodeHeader generated incorrect results: %#q != %#q", b, expected)
- }
- // UTF-8 ('q' encoded).
- subject = "UTF-8 email subject. It can contain é, ñ, or £. Long subject headers will be split in multiple lines!"
- expected = []byte("=?UTF-8?Q?UTF-8_email_subject._It_c?=\r\n" +
- " =?UTF-8?Q?an_contain_=C3=A9,_=C3=B1,_or_=C2=A3._Lo?=\r\n" +
- " =?UTF-8?Q?ng_subject_headers_will_be_split_in_multiple_lines!?=")
- b = encodeHeader("Subject", subject)
- if !bytes.Equal(b, expected) {
- t.Errorf("encodeHeader generated incorrect results: %#q != %#q", b, expected)
- }
- }
- */
|