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 4 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
4 changes: 2 additions & 2 deletions helpers/inbound/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func inboundHandler(response http.ResponseWriter, request *http.Request) {

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

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

for section, body := range parsedEmail.Body {
Expand Down
22 changes: 19 additions & 3 deletions helpers/inbound/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,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 {
result := ParsedEmail{
Headers: make(map[string]string),
Body: make(map[string]string),
Attachments: make(map[string][]byte),
Attachments: []ParsedAttachment{},
rawRequest: request,
}
result.parse()
Expand Down Expand Up @@ -65,7 +72,16 @@ func (email *ParsedEmail) parseRawEmail(rawEmail string) {
}

} else if emailPart.FileName() != "" {
email.Attachments[emailPart.FileName()] = readBody(emailPart)
content := readBody(emailPart)

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

email.Attachments = append(email.Attachments, attachment)
} else {
header := emailPart.Header.Get("Content-Type")
email.Body[header] = string(readBody(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 @@ -49,6 +49,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 @@ -57,7 +65,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 @@ -89,7 +97,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,
}
email.parseRawEmail(rawEmail)
Expand All @@ -103,4 +111,4 @@ Content-Transfer-Encoding: quoted-printable
// Subject Test Email
// Content-Type multipart/mixed; boundary=TwiLIo
// Hello Twilio SendGrid!
}
}