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

Integrate all previous PRs #44

Closed
wants to merge 10 commits into from
Closed
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
8 changes: 5 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Golang CircleCI 2.0 configuration file
# Golang CircleCI 2.1 configuration file
#
# Check https://circleci.com/docs/2.0/language-go/ for more details
version: 2
version: 2.1
jobs:
build:
docker:
- image: circleci/golang:1.14
working_directory: /go/src/github.com/{{ORG_NAME}}/{{REPO_NAME}}
steps:
- checkout
- run: go test -v ./...
- run: GO111MODULE=off go get github.com/mattn/goveralls
- run: go test -v -cover -race -coverprofile=coverage.out ./...
- run: $GOPATH/bin/goveralls -coverprofile=coverage.out -service=circle-ci -repotoken=$COVERALLS_TOKEN
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ Every change merges to master. No development is done in other branches.
- Wait for CI to verify you didn't break anything
- If you did, rewrite it
- If CI passes, wait for manual review by repo's owner
- Your pull request will be merged into master
- Your pull request will be merged into master
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Parsemail - simple email parsing Go library

[![Build Status](https://circleci.com/gh/DusanKasan/parsemail.svg?style=shield&circle-token=:circle-token)](https://circleci.com/gh/DusanKasan/parsemail) [![Coverage Status](https://coveralls.io/repos/github/DusanKasan/Parsemail/badge.svg?branch=master)](https://coveralls.io/github/DusanKasan/Parsemail?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/DusanKasan/parsemail)](https://goreportcard.com/report/github.com/DusanKasan/parsemail)
[![Build Status](https://circleci.com/gh/k3a/parsemail.svg?style=shield)](https://circleci.com/gh/k3a/parsemail) [![Coverage Status](https://coveralls.io/repos/github/k3a/parsemail/badge.svg?branch=master)](https://coveralls.io/github/k3a/parsemail?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/k3a/parsemail)](https://goreportcard.com/report/github.com/k3a/parsemail)

> The [original repo](https://github.com/DusanKasan/parsemail) is unmaintained. This is an active fork replacing the original repo. Contributions are welcome.

This library allows for parsing an email message into a more convenient form than the `net/mail` provides. Where the `net/mail` just gives you a map of header fields and a `io.Reader` of its body, Parsemail allows access to all the standard header fields set in [RFC5322](https://tools.ietf.org/html/rfc5322), html/text body as well as attachements/embedded content as binary streams with metadata.

Expand Down Expand Up @@ -55,4 +57,4 @@ for _, a := range(email.EmbeddedFiles) {
fmt.Println(a.ContentType)
//and read a.Data
}
```
```
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/DusanKasan/parsemail
module github.com/k3a/parsemail

go 1.12
go 1.12
152 changes: 127 additions & 25 deletions parsemail.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io/ioutil"
"mime"
"mime/multipart"
"mime/quotedprintable"
"net/mail"
"strings"
"time"
Expand All @@ -18,6 +19,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 All @@ -37,6 +39,8 @@ func Parse(r io.Reader) (email Email, err error) {
return
}

cte := msg.Header.Get("Content-Transfer-Encoding")

switch contentType {
case contentTypeMultipartMixed:
email.TextBody, email.HTMLBody, email.Attachments, email.EmbeddedFiles, err = parseMultipartMixed(msg.Body, params["boundary"])
Expand All @@ -46,12 +50,36 @@ func Parse(r io.Reader) (email Email, err error) {
email.TextBody, email.HTMLBody, email.EmbeddedFiles, err = parseMultipartRelated(msg.Body, params["boundary"])
case contentTypeTextPlain:
message, _ := ioutil.ReadAll(msg.Body)
var reader io.Reader
reader, err = decodeContent(strings.NewReader(string(message[:])), cte)
if err != nil {
return
}

message, err = ioutil.ReadAll(reader)
if err != nil {
return
}

email.TextBody = strings.TrimSuffix(string(message[:]), "\n")
case contentTypeTextHtml:
message, _ := ioutil.ReadAll(msg.Body)
var reader io.Reader
reader, err = decodeContent(strings.NewReader(string(message[:])), cte)
if err != nil {
return
}

message, err = ioutil.ReadAll(reader)
if err != nil {
return
}

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"))
email.Content, err = decodeContent(msg.Body, cte)
}

return
Expand Down Expand Up @@ -103,32 +131,67 @@ 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 {
part, err := pmr.NextPart()
part, err := pmr.NextRawPart()

if err == io.EOF {
break
} else if err != nil {
return textBody, htmlBody, embeddedFiles, err
}

cte := part.Header.Get("Content-Transfer-Encoding")

contentType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type"))
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}

switch contentType {
case contentTypeTextPlain:
ppContent, err := ioutil.ReadAll(part)
decoded, err := decodeContent(part, cte)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
ppContent, err := ioutil.ReadAll(decoded)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}

textBody += strings.TrimSuffix(string(ppContent[:]), "\n")
case contentTypeTextHtml:
ppContent, err := ioutil.ReadAll(part)
decoded, err := decodeContent(part, cte)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
ppContent, err := ioutil.ReadAll(decoded)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
Expand Down Expand Up @@ -163,29 +226,39 @@ func parseMultipartRelated(msg io.Reader, boundary string) (textBody, htmlBody s
func parseMultipartAlternative(msg io.Reader, boundary string) (textBody, htmlBody string, embeddedFiles []EmbeddedFile, err error) {
pmr := multipart.NewReader(msg, boundary)
for {
part, err := pmr.NextPart()
part, err := pmr.NextRawPart()

if err == io.EOF {
break
} else if err != nil {
return textBody, htmlBody, embeddedFiles, err
}

cte := part.Header.Get("Content-Transfer-Encoding")

contentType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type"))
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}

switch contentType {
case contentTypeTextPlain:
ppContent, err := ioutil.ReadAll(part)
decoded, err := decodeContent(part, cte)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
ppContent, err := ioutil.ReadAll(decoded)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}

textBody += strings.TrimSuffix(string(ppContent[:]), "\n")
case contentTypeTextHtml:
ppContent, err := ioutil.ReadAll(part)
decoded, err := decodeContent(part, cte)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
ppContent, err := ioutil.ReadAll(decoded)
if err != nil {
return textBody, htmlBody, embeddedFiles, err
}
Expand Down Expand Up @@ -220,18 +293,38 @@ func parseMultipartAlternative(msg io.Reader, boundary string) (textBody, htmlBo
func parseMultipartMixed(msg io.Reader, boundary string) (textBody, htmlBody string, attachments []Attachment, embeddedFiles []EmbeddedFile, err error) {
mr := multipart.NewReader(msg, boundary)
for {
part, err := mr.NextPart()
part, err := mr.NextRawPart()
if err == io.EOF {
break
} else if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, err
}

if isAttachment(part) {
at, err := decodeAttachment(part)
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, err
}

attachments = append(attachments, at)
continue
}

cte := part.Header.Get("Content-Transfer-Encoding")

contentType, params, err := mime.ParseMediaType(part.Header.Get("Content-Type"))
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, err
}

if isAttachment(part) {
at, err := decodeAttachment(part)
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, err
}
attachments = append(attachments, at)
}

if contentType == contentTypeMultipartAlternative {
textBody, htmlBody, embeddedFiles, err = parseMultipartAlternative(part, params["boundary"])
if err != nil {
Expand All @@ -243,26 +336,27 @@ func parseMultipartMixed(msg io.Reader, boundary string) (textBody, htmlBody str
return textBody, htmlBody, attachments, embeddedFiles, err
}
} else if contentType == contentTypeTextPlain {
ppContent, err := ioutil.ReadAll(part)
decoded, err := decodeContent(part, cte)
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, err
}
ppContent, err := ioutil.ReadAll(decoded)
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, err
}

textBody += strings.TrimSuffix(string(ppContent[:]), "\n")
} else if contentType == contentTypeTextHtml {
ppContent, err := ioutil.ReadAll(part)
decoded, err := decodeContent(part, cte)
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, err
}

htmlBody += strings.TrimSuffix(string(ppContent[:]), "\n")
} else if isAttachment(part) {
at, err := decodeAttachment(part)
ppContent, err := ioutil.ReadAll(decoded)
if err != nil {
return textBody, htmlBody, attachments, embeddedFiles, err
}

attachments = append(attachments, at)
htmlBody += strings.TrimSuffix(string(ppContent[:]), "\n")
} else {
return textBody, htmlBody, attachments, embeddedFiles, fmt.Errorf("Unknown multipart/mixed nested mime type: %s", contentType)
}
Expand Down Expand Up @@ -345,24 +439,32 @@ func decodeAttachment(part *multipart.Part) (at Attachment, err error) {
}

func decodeContent(content io.Reader, encoding string) (io.Reader, error) {
switch encoding {
switch strings.ToLower(encoding) {
case "base64":
decoded := base64.NewDecoder(base64.StdEncoding, content)
b, err := ioutil.ReadAll(decoded)
if err != nil {
return nil, err
}

return bytes.NewReader(b), nil
case "7bit":
dd, err := ioutil.ReadAll(content)
case "quoted-printable":
decoded := quotedprintable.NewReader(content)
b, err := ioutil.ReadAll(decoded)
if err != nil {
return nil, err
}

return bytes.NewReader(dd), nil
case "":
return content, nil
return bytes.NewReader(b), nil
// The values "8bit", "7bit", and "binary" all imply that NO encoding has been performed and data need to be read as bytes.
// "7bit" means that the data is all represented as short lines of US-ASCII data.
// "8bit" means that the lines are short, but there may be non-ASCII characters (octets with the high-order bit set).
// "Binary" means that not only may non-ASCII characters be present, but also that the lines are not necessarily short enough for SMTP transport.
case "", "7bit", "8bit", "binary":
decoded := quotedprintable.NewReader(content)
b, err := ioutil.ReadAll(decoded)
if err != nil {
return nil, err
}
return bytes.NewReader(b), nil
default:
return nil, fmt.Errorf("unknown encoding: %s", encoding)
}
Expand Down Expand Up @@ -483,11 +585,11 @@ type Email struct {
ResentMessageID string

ContentType string
Content io.Reader
Content io.Reader

HTMLBody string
TextBody string

Attachments []Attachment
EmbeddedFiles []EmbeddedFile
}
}
Loading