Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
21 changes: 18 additions & 3 deletions helpers/inbound/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ import (
"strings"
)

type ParsedAttachment struct {
Headers map[string]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 +70,17 @@ func (email *ParsedEmail) parseRawEmail(rawEmail string) {
}

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

headers["Content-Type"] = emailPart.Header.Get("Content-Type")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just headers := emailPart.Header (would need to change type). Alternatively, instead of storing headers, make ContentType one of the properties of ParsedAttachment, and do some simple string parsing to extract the MIME type from the string returned by emailPart.Header.Get("Content-Type")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the ParsedAttachment.Headers to type MIMEHeader and stored the full attachment headers in there. Also added a string ContentType to take the content type (Without the name="***" part). Updated the tests for this too.

attachment := ParsedAttachment{
Filename: emailPart.FileName(),
Content: content,
Headers: headers,
}

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; name=\"TwilioSendGrid.jpg\""

assert.Equalf(t, contentType, email.Attachments[0].Headers["Content-Type"],"Expected From: %s, Got: %s", contentType, email.Attachments[0].Headers["Content-Type"])
Comment thread
thinkingserious marked this conversation as resolved.
Outdated
}

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!
}
}