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

Fix!: The ability to get Content-Type value from attachments (Inbound Parse) #416

Open
wants to merge 6 commits into
base: main
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
8 changes: 4 additions & 4 deletions helpers/inbound/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ func inboundHandler(response http.ResponseWriter, request *http.Request) {

fmt.Print(parsedEmail.Headers["From"])

for filename, contents := range parsedEmail.Attachments {
// Do something with an attachment
handleAttachment(filename, contents)
}
for _, file := range parsedEmail.Attachments {
// Do something with an attachment
handleAttachment(file.Filename, file.Content)
}

for section, body := range parsedEmail.Body {
// Do something with the email body
Expand Down
25 changes: 19 additions & 6 deletions helpers/inbound/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,29 @@ import (
"mime"
"mime/multipart"
"net/http"
"net/textproto"
"strings"
)

type ParsedAttachment struct {
Headers textproto.MIMEHeader
ContentType string
Filename string
Content []byte
}

type ParsedEmail struct {
Headers map[string]string
Body map[string]string
Attachments map[string][]byte
Attachments []ParsedAttachment
rawRequest *http.Request
}

func Parse(request *http.Request) (*ParsedEmail, error) {
result := ParsedEmail{
Headers: make(map[string]string),
Body: make(map[string]string),
Attachments: make(map[string][]byte),
Attachments: []ParsedAttachment{},
rawRequest: request,
}
err := result.parse()
Expand Down Expand Up @@ -76,11 +84,16 @@ func (email *ParsedEmail) parseRawEmail(rawEmail string) error {
}

} else if emailPart.FileName() != "" {
b, err := ioutil.ReadAll(emailPart)
if err != nil {
return err
content := readBody(emailPart)

attachment := ParsedAttachment{
Filename: emailPart.FileName(),
ContentType: strings.Split(emailPart.Header.Get("Content-Type"), ";")[0],
Content: content,
Headers: emailPart.Header,
}
email.Attachments[emailPart.FileName()] = b

email.Attachments = append(email.Attachments, attachment)
} else {
header := emailPart.Header.Get("Content-Type")
b, err := ioutil.ReadAll(emailPart)
Expand Down
14 changes: 11 additions & 3 deletions helpers/inbound/inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ func TestParse(t *testing.T) {
}
}

func TestAttachments(t *testing.T) {
req := createRequest("./sample_data/raw_data_with_attachments.txt")
email := Parse(req)
contentType := "image/jpeg"

assert.Equalf(t, contentType, email.Attachments[0].ContentType,"Expected From: %s, Got: %s", contentType, email.Attachments[0].ContentType)
}

func ExampleParsedEmail_parseHeaders() {
headers := `
Foo: foo
Expand All @@ -78,7 +86,7 @@ Bar: baz
email := ParsedEmail{
Headers: make(map[string]string),
Body: make(map[string]string),
Attachments: make(map[string][]byte),
Attachments: []ParsedAttachment{},
rawRequest: nil,
}
email.parseHeaders(headers)
Expand Down Expand Up @@ -110,7 +118,7 @@ Content-Transfer-Encoding: quoted-printable
email := ParsedEmail{
Headers: make(map[string]string),
Body: make(map[string]string),
Attachments: make(map[string][]byte),
Attachments: []ParsedAttachment{},
rawRequest: nil,
}

Expand All @@ -128,4 +136,4 @@ Content-Transfer-Encoding: quoted-printable
// Subject Test Email
// Content-Type multipart/mixed; boundary=TwiLIo
// Hello Twilio SendGrid!
}
}