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

Example in documentation for parse helper doesn't work #399

Open
phillipbroberts opened this issue Jul 2, 2020 · 4 comments
Open

Example in documentation for parse helper doesn't work #399

phillipbroberts opened this issue Jul 2, 2020 · 4 comments
Labels
status: help wanted requesting help from the community type: bug bug in the library

Comments

@phillipbroberts
Copy link

Issue Summary

The example in the readme for helper/inbound does not work.

Steps to Reproduce

  1. Cut and Paste Example
  2. Run Code
  3. Point a parser domain to it
  4. Send email to [email protected]

Code Snippet

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/sendgrid/sendgrid-go/helpers/inbound"
)


func inboundHandler(response http.ResponseWriter, request *http.Request) {
	parsedEmail := inbound.Parse(request)

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

	for filename, contents := range parsedEmail.Attachments {
		// Do something with an attachment
		//handleAttachment(filename, contents)
		fmt.Print(filename, contents)
	}

	for section, body := range parsedEmail.Body {
		// Do something with the email body
		//handleEmail(body)
		fmt.Print(section, body)
	}

	// Twilio SendGrid needs a 200 OK response to stop POSTing
	response.WriteHeader(http.StatusOK)
}

func main() {
	http.HandleFunc("/inbound", inboundHandler)
	if err := http.ListenAndServe(":8000", nil); err != nil {
		log.Fatalln("Error")
	}
}

Exception/Log

2020/07/02 16:51:40 http: panic serving 10.244.0.50:40980: runtime error: index out of range [1] with length 1
goroutine 21 [running]:
net/http.(*conn).serve.func1(0xc000072f00)
	/usr/local/go/src/net/http/server.go:1772 +0x139
panic(0x6d76a0, 0xc0001d8780)
	/usr/local/go/src/runtime/panic.go:975 +0x3e3
github.com/sendgrid/sendgrid-go/helpers/inbound.(*ParsedEmail).parseHeaders(0xc0000cb630, 0xc000143c00, 0xb14)
	/go/src/github.com/sendgrid/sendgrid-go/helpers/inbound/inbound.go:100 +0x19c
github.com/sendgrid/sendgrid-go/helpers/inbound.(*ParsedEmail).parse(0xc0000cb630)
	/go/src/github.com/sendgrid/sendgrid-go/helpers/inbound/inbound.go:40 +0x180
github.com/sendgrid/sendgrid-go/helpers/inbound.Parse(...)
	/go/src/github.com/sendgrid/sendgrid-go/helpers/inbound/inbound.go:28
main.inboundHandler(0x7637c0, 0xc0000a4700, 0xc0000c0900)
	/go/src/app/main.go:20 +0x1b1
net/http.HandlerFunc.ServeHTTP(0x70fca0, 0x7637c0, 0xc0000a4700, 0xc0000c0900)
	/usr/local/go/src/net/http/server.go:2012 +0x44
net/http.(*ServeMux).ServeHTTP(0x93e300, 0x7637c0, 0xc0000a4700, 0xc0000c0900)
	/usr/local/go/src/net/http/server.go:2387 +0x1a5
net/http.serverHandler.ServeHTTP(0xc0000a4000, 0x7637c0, 0xc0000a4700, 0xc0000c0900)
	/usr/local/go/src/net/http/server.go:2807 +0xa3
net/http.(*conn).serve(0xc000072f00, 0x763d80, 0xc00004cb80)
	/usr/local/go/src/net/http/server.go:1895 +0x86c
created by net/http.(*Server).Serve
	/usr/local/go/src/net/http/server.go:2933 +0x35c

Technical details:

  • sendgrid-go version: Latest
  • go version: 1.14
@eshanholtz
Copy link
Contributor

@phillipbroberts

Looks like it's having an issue parsing the email header portion. Can you please include an example of the inbound email request body so I can run further tests?

@eshanholtz eshanholtz added status: waiting for feedback waiting for feedback from the submitter type: bug bug in the library labels Jul 6, 2020
@phillipbroberts
Copy link
Author

phillipbroberts commented Jul 8, 2020

I didn't have the service up anymore - the best I could do is send an email from the address I was using to send test emails to one of my other email addresses - hope this is helpful.

Date: Wed, 08 Jul 2020 09:40:47 -0400
Subject: test
From: Phillip Roberts <[email protected]>
To: Phillip Roberts <[email protected]>
Message-ID: <[email protected]>
Thread-Topic: test
Thread-Index: AUJERDQ2q08BPjBZqFLNp1WxpBNfMg==
Mime-version: 1.0
Content-type: multipart/alternative;
	boundary="B_3677046411_2029731371"

> This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--B_3677046411_2029731371
Content-type: text/plain;
	charset="UTF-8"
Content-transfer-encoding: 7bit

test


--B_3677046411_2029731371
Content-type: text/html;
	charset="UTF-8"
Content-transfer-encoding: quoted-printable

<html><head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8"><meta co=
ntent=3D"text/html; charset=3Dutf-8"><meta name=3D"Generator" content=3D"Microsoft W=
ord 15 (filtered medium)"><style>
<!--
@font-face
	{font-family:"Cambria Math"}
@font-face
	{font-family:Calibri}
p.MsoNormal, li.MsoNormal, div.MsoNormal
	{margin:0in;
	margin-bottom:.0001pt;
	font-size:12.0pt;
	font-family:"Calibri",sans-serif}
span.EmailStyle17
	{font-family:"Calibri",sans-serif;
	color:windowtext}
.MsoChpDefault
	{font-size:12.0pt;
	font-family:"Calibri",sans-serif}
@page WordSection1
	{margin:1.0in 1.0in 1.0in 1.0in}
div.WordSection1
	{}
-->
</style></head><body lang=3D"EN-US" link=3D"#0563C1" vlink=3D"#954F72"><div class=
=3D"WordSection1"><p class=3D"MsoNormal"><span style=3D"font-size:11.0pt">test</sp=
an></p></div></body></html>

--B_3677046411_2029731371--

@eshanholtz
Copy link
Contributor

I was able to recreate this bug. This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.

@childish-sambino childish-sambino added status: help wanted requesting help from the community and removed status: waiting for feedback waiting for feedback from the submitter labels Jul 14, 2020
@kwshi
Copy link

kwshi commented Dec 21, 2020

For whoever gets around to working on this (I may try to open a PR later if I'm free), I dug around a little and think I figured out the cause of the issue (or at least the cause of an issue):

emails := email.rawRequest.MultipartForm.Value["email"]

I wrote a quick program that logs the keys of the req.MultipartForm.Value map, producing the following:

2020/12/21 00:50:20 value key: "dkim"
2020/12/21 00:50:20 value key: "attachments"
2020/12/21 00:50:20 value key: "sender_ip"
2020/12/21 00:50:20 value key: "charsets"
2020/12/21 00:50:20 value key: "content-ids"
2020/12/21 00:50:20 value key: "html"
2020/12/21 00:50:20 value key: "envelope"
2020/12/21 00:50:20 value key: "attachment-info"
2020/12/21 00:50:20 value key: "from"
2020/12/21 00:50:20 value key: "text"
2020/12/21 00:50:20 value key: "subject"
2020/12/21 00:50:20 value key: "SPF"
2020/12/21 00:50:20 value key: "headers"
2020/12/21 00:50:20 value key: "to"

Point being, there is no "email" field. Instead, it appears that much of the information parseRawEmail tries to extract, e.g. attachments, text content, etc., are already pre-extracted by the inbound parse hook and provided in the "attachments", "attachment-info", "text", etc. keys.

That also means that the documentation (specifically the "sample payload" snippet) here should be updated to reflect the actual payload shape.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community type: bug bug in the library
Projects
None yet
Development

No branches or pull requests

4 participants