Skip to content

Commit

Permalink
jordan-wright#139 - Missing MIMe headers with NewEmailFromReader
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarmo Randma committed Feb 16, 2021
1 parent 06c045f commit b42033a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
11 changes: 9 additions & 2 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func NewEmailFromReader(r io.Reader) (*Email, error) {
}
filename, filenameDefined := params["filename"]
if cd == "attachment" || (cd == "inline" && filenameDefined) {
_, err = e.Attach(bytes.NewReader(p.body), filename, ct)
_, err = e.AttachWithHeaders(bytes.NewReader(p.body), filename, ct, p.header)
if err != nil {
return e, err
}
Expand Down Expand Up @@ -262,14 +262,21 @@ func parseMIMEParts(hs textproto.MIMEHeader, b io.Reader) ([]*part, error) {
// Required parameters include an io.Reader, the desired filename for the attachment, and the Content-Type
// The function will return the created Attachment for reference, as well as nil for the error, if successful.
func (e *Email) Attach(r io.Reader, filename string, c string) (a *Attachment, err error) {
return e.AttachWithHeaders(r, filename, c, textproto.MIMEHeader{})
}

// AttachWithHeaders is used to attach content from an io.Reader to the email. Required parameters include an io.Reader,
// the desired filename for the attachment, the Content-Type and the original MIME headers.
// The function will return the created Attachment for reference, as well as nil for the error, if successful.
func (e *Email) AttachWithHeaders(r io.Reader, filename string, c string, headers textproto.MIMEHeader) (a *Attachment, err error) {
var buffer bytes.Buffer
if _, err = io.Copy(&buffer, r); err != nil {
return
}
at := &Attachment{
Filename: filename,
ContentType: c,
Header: textproto.MIMEHeader{},
Header: headers,
Content: buffer.Bytes(),
}
e.Attachments = append(e.Attachments, at)
Expand Down
14 changes: 12 additions & 2 deletions email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ Content-Type: text/html; charset=UTF-8
--35d10c2224bd787fe700c2c6f4769ddc936eb8a0b58e9c8717e406c5abb7
Content-Disposition: attachment;
filename="cat.jpeg"
Content-Id: <cat.jpeg>
Content-Id: <cat.content-id>
Content-Transfer-Encoding: base64
Content-Type: image/jpeg
Expand All @@ -688,7 +688,7 @@ TGV0J3MganVzdCBwcmV0ZW5kIHRoaXMgaXMgcmF3IEpQRUcgZGF0YS4=
--35d10c2224bd787fe700c2c6f4769ddc936eb8a0b58e9c8717e406c5abb7
Content-Disposition: inline;
filename="cat-inline.jpeg"
Content-Id: <cat-inline.jpeg>
Content-Id: <cat-inline.content-id>
Content-Transfer-Encoding: base64
Content-Type: image/jpeg
Expand Down Expand Up @@ -720,12 +720,22 @@ TGV0J3MganVzdCBwcmV0ZW5kIHRoaXMgaXMgcmF3IEpQRUcgZGF0YS4=
if !bytes.Equal(e.Attachments[0].Content, a.Content) {
t.Fatalf("Incorrect attachment content %#q != %#q", e.Attachments[0].Content, a.Content)
}
if e.Attachments[0].Header != nil {
if e.Attachments[0].Header.Get("Content-Id") != "<cat.content-id>" {
t.Fatalf("Incorrect attachment header Content-Id %s != %s", e.Attachments[0].Header.Get("Content-Id"), "<cat.content-id>")
}
}
if e.Attachments[1].Filename != b.Filename {
t.Fatalf("Incorrect attachment filename %s != %s", e.Attachments[1].Filename, b.Filename)
}
if !bytes.Equal(e.Attachments[1].Content, b.Content) {
t.Fatalf("Incorrect attachment content %#q != %#q", e.Attachments[1].Content, b.Content)
}
if e.Attachments[1].Header != nil {
if e.Attachments[1].Header.Get("Content-Id") != "<cat-inline.content-id>" {
t.Fatalf("Incorrect attachment header Content-Id %s != %s", e.Attachments[1].Header.Get("Content-Id"), "<cat-inline.content-id>")
}
}
}

func ExampleGmail() {
Expand Down

0 comments on commit b42033a

Please sign in to comment.