Prechádzať zdrojové kódy

Added ability to import emails with base64 encoded mime parts. Fixes #38

Jordan Wright 7 rokov pred
rodič
commit
4e83f79356
2 zmenil súbory, kde vykonal 57 pridanie a 1 odobranie
  1. 7 1
      email.go
  2. 50 0
      email_test.go

+ 7 - 1
email.go

@@ -174,9 +174,15 @@ func parseMIMEParts(hs textproto.MIMEHeader, b io.Reader) ([]*part, error) {
 				}
 				ps = append(ps, sps...)
 			} else {
+				var reader io.Reader
+				reader = p
+				const cte = "Content-Transfer-Encoding"
+				if p.Header.Get(cte) == "base64" {
+					reader = base64.NewDecoder(base64.StdEncoding, reader)
+				}
 				// Otherwise, just append the part to the list
 				// Copy the part data into the buffer
-				if _, err := io.Copy(&buf, p); err != nil {
+				if _, err := io.Copy(&buf, reader); err != nil {
 					return ps, err
 				}
 				ps = append(ps, &part{body: buf.Bytes(), header: p.Header})

+ 50 - 0
email_test.go

@@ -326,6 +326,56 @@ This is a test message!`)
 	}
 }
 
+func TestBase64EmailFromReader(t *testing.T) {
+	ex := &Email{
+		Subject: "Test Subject",
+		To:      []string{"Jordan Wright <jmwright798@gmail.com>"},
+		From:    "Jordan Wright <jmwright798@gmail.com>",
+		Text:    []byte("This is a test email with HTML Formatting. It also has very long lines so that the content must be wrapped if using quoted-printable decoding."),
+		HTML:    []byte("<div dir=\"ltr\">This is a test email with <b>HTML Formatting.</b>\u00a0It also has very long lines so that the content must be wrapped if using quoted-printable decoding.</div>\n"),
+	}
+	raw := []byte(`
+		MIME-Version: 1.0
+Subject: Test Subject
+From: Jordan Wright <jmwright798@gmail.com>
+To: Jordan Wright <jmwright798@gmail.com>
+Content-Type: multipart/alternative; boundary=001a114fb3fc42fd6b051f834280
+
+--001a114fb3fc42fd6b051f834280
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: base64
+
+VGhpcyBpcyBhIHRlc3QgZW1haWwgd2l0aCBIVE1MIEZvcm1hdHRpbmcuIEl0IGFsc28gaGFzIHZl
+cnkgbG9uZyBsaW5lcyBzbyB0aGF0IHRoZSBjb250ZW50IG11c3QgYmUgd3JhcHBlZCBpZiB1c2lu
+ZyBxdW90ZWQtcHJpbnRhYmxlIGRlY29kaW5nLg==
+
+--001a114fb3fc42fd6b051f834280
+Content-Type: text/html; charset=UTF-8
+Content-Transfer-Encoding: quoted-printable
+
+<div dir=3D"ltr">This is a test email with <b>HTML Formatting.</b>=C2=A0It =
+also has very long lines so that the content must be wrapped if using quote=
+d-printable decoding.</div>
+
+--001a114fb3fc42fd6b051f834280--`)
+	e, err := NewEmailFromReader(bytes.NewReader(raw))
+	if err != nil {
+		t.Fatalf("Error creating email %s", err.Error())
+	}
+	if e.Subject != ex.Subject {
+		t.Fatalf("Incorrect subject. %#q != %#q", e.Subject, ex.Subject)
+	}
+	if !bytes.Equal(e.Text, ex.Text) {
+		t.Fatalf("Incorrect text: %#q != %#q", e.Text, ex.Text)
+	}
+	if !bytes.Equal(e.HTML, ex.HTML) {
+		t.Fatalf("Incorrect HTML: %#q != %#q", e.HTML, ex.HTML)
+	}
+	if e.From != ex.From {
+		t.Fatalf("Incorrect \"From\": %#q != %#q", e.From, ex.From)
+	}
+}
+
 func ExampleGmail() {
 	e := NewEmail()
 	e.From = "Jordan Wright <test@gmail.com>"