Skip to content
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

Added parsing attachments in non-multipart emails #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/DusanKasan/parsemail
module github.com/get-net/parsemail

go 1.12
32 changes: 30 additions & 2 deletions parsemail.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const contentTypeMultipartAlternative = "multipart/alternative"
const contentTypeMultipartRelated = "multipart/related"
const contentTypeTextHtml = "text/html"
const contentTypeTextPlain = "text/plain"
const contentTypeOctetStream = "application/octet-stream"

// Parse an email message read from io.Reader into parsemail.Email struct
func Parse(r io.Reader) (email Email, err error) {
Expand Down Expand Up @@ -50,6 +51,8 @@ func Parse(r io.Reader) (email Email, err error) {
case contentTypeTextHtml:
message, _ := ioutil.ReadAll(msg.Body)
email.HTMLBody = strings.TrimSuffix(string(message[:]), "\n")
case contentTypeOctetStream:
email.Attachments, err = parseAttachmentOnlyEmail(msg.Body, msg.Header)
default:
email.Content, err = decodeContent(msg.Body, msg.Header.Get("Content-Transfer-Encoding"))
}
Expand Down Expand Up @@ -103,6 +106,31 @@ func parseContentType(contentTypeHeader string) (contentType string, params map[
return mime.ParseMediaType(contentTypeHeader)
}

func parseAttachmentOnlyEmail(body io.Reader, header mail.Header) (attachments []Attachment, err error) {
contentDisposition := header.Get("Content-Disposition")

if len(contentDisposition) > 0 && strings.Contains(contentDisposition, "attachment;") {

attachmentData, err := decodeContent(body, header.Get("Content-Transfer-Encoding"))
if err != nil {
return attachments, err
}

fileName := strings.Replace(contentDisposition, "attachment; filename=\"", "", -1)
fileName = strings.TrimRight(fileName, "\"")

at := Attachment{
Filename: fileName,
ContentType: "application/octet-stream",
Data: attachmentData,
}

attachments = append(attachments, at)
}

return attachments, nil
}

func parseMultipartRelated(msg io.Reader, boundary string) (textBody, htmlBody string, embeddedFiles []EmbeddedFile, err error) {
pmr := multipart.NewReader(msg, boundary)
for {
Expand Down Expand Up @@ -483,11 +511,11 @@ type Email struct {
ResentMessageID string

ContentType string
Content io.Reader
Content io.Reader

HTMLBody string
TextBody string

Attachments []Attachment
EmbeddedFiles []EmbeddedFile
}
}