sendgrid / sendgrid-go

The Official Twilio SendGrid Golang API Library
https://sendgrid.com
MIT License
984 stars 261 forks source link

runtime error index out of range [1] with length 1 #480

Open LeSqueed opened 3 months ago

LeSqueed commented 3 months ago

I keep getting the runtime error index out of range [1] with length 1 error when trying to use inbound parsing. For testing to make sure it wasn't something I was doing I simplified the code quite a bit to be basically the example code.

I added some extra code to log some extra data which I have also added below.

So far the best idea I have of why it is not working is due to the headers not being formatted in a way it can handle.

apiRouter.HandleFunc("/inbound-mail", func(w http.ResponseWriter, r *http.Request) {
        // Print the request headers to the console (key: value)
        for key, value := range r.Header {
            fmt.Println(key, ":", value)
        }

        // Parse the multipart form, if present
        if err := r.ParseMultipartForm(32 << 20); err != nil {
            log.Printf("Error parsing multipart form: %v", err)
            w.WriteHeader(http.StatusBadRequest)
            return
        }

        // Print the multipart form data
        if r.MultipartForm != nil {
            fmt.Println("Multipart Form Data:")
            for key, values := range r.MultipartForm.Value {
                for _, value := range values {
                    fmt.Printf("%s: %s\n", key, value)
                }
            }
        } else {
            fmt.Println("No multipart form data found")
        }

        // Parse the email with attachments
        parsedEmail, err := inbound.ParseWithAttachments(r)
        if err != nil {
            log.Fatalf("Error parsing email: %v", err)
            return
        }

        // Print the sender of the email
        fmt.Println("From:", parsedEmail.Envelope.From)

        // Print the email body
        for i, line := range strings.Split(parsedEmail.TextBody, "\n") {
            fmt.Printf("Line %d: %s\n", i, line)
        }

        // Respond 200 OK to indicate successful processing
        w.WriteHeader(http.StatusOK)
    }).Methods("POST")
2024/08/10 17:48:57 Starting server on port  9081
X-Forwarded-Proto  [https]
Content-Length  [3092]
Content-Type  [multipart/form-data; boundary=xYzZY]
Accept-Encoding  [gzip]
X-Real-Ip  [167.89.*.*]
User-Agent  [Sendlib/1.0]
X-Forwarded-For  [167.89.*.*]
Connection  [close]
Multipart Form Data:
charsets {"to":"UTF-8","from":"UTF-8","subject":"UTF-8"}
envelope {"to":["test@parse.domain.nl"],"from":"mailadress@here.com"}
sender_ip 81.169.*.*
to test@parse.domain.nl
dkim none
SPF softfail
from Pieter <mailadress@here.com>
email Received from mo4-p00-ob.smtp.rzone.de (mxd [81.169.*.*]) by mx.sendgrid.net with ESMTP id [REDACTED] for <test@parse.domain.nl>; Sat, 10 Aug 2024 17:39:55.803 +0000 (UTC)
ARC-Seal i=1; a=rsa-sha256; t=1723311594; cv=none;
    d=strato.com; s=strato-dkim-0002;
    b=[REDACTED]
ARC-Message-Signature i=1; a=rsa-sha256; c=relaxed/relaxed; t=1723311594;
    s=strato-dkim-0002; d=strato.com;
    h=Message-Id:To:Subject:From:Date:Cc:Date:From:Subject:Sender;
    bh=[REDACTED];
    b=[REDACTED]
ARC-Authentication-Results i=1; strato.com;
    arc=none;
    dkim=none
X-RZG-CLASS-ID mo00
X-RZG-AUTH ":[REDACTED]"
Received from [192.168.1.140]
    by smtp.strato.com (RZmta 51.1.0 AUTH)
    with ESMTPSA id [REDACTED]
        (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits))
        (Client did not present a certificate)
    for <test@parse.domain.nl>;
    Sat, 10 Aug 2024 19:39:54 +0200 (CEST)
Date Sat, 10 Aug 2024 19:39:48 +0200
From Pieter <mailadress@here.comt>
Subject Test
To test@parse.domain.nl
Message-Id <CQJ0IS.1Z5QJJRCHNWS3@domain.net>
X-Mailer geary/46.0
MIME-Version 1.0
Content-Type multipart/alternative; boundary="=-4eyqa2TrTXct6OapdPpZ"
Content-Transfer-Encoding 7bit

--=-4eyqa2TrTXct6OapdPpZ
Content-Type text/plain; charset=us-ascii; format=flowed

Test Message

--=-4eyqa2TrTXct6OapdPpZ
Content-Type text/html; charset=us-ascii

<div id="geary-body" dir="auto"><div>Test Message</div></div>
--=-4eyqa2TrTXct6OapdPpZ--

subject Test
2024/08/10 17:48:59 http panic serving 127.0.0.1:59604 runtime error index out of range [1] with length 1
goroutine 52 [running]:
net/http.(*conn).serve.func1()
        /usr/local/go/src/net/http/server.go:1850 +0xbf
panic({0x9203c0, 0xc0000262b8})
        /usr/local/go/src/runtime/panic.go:890 +0x262
github.com/sendgrid/sendgrid-go/helpers/inbound.(*ParsedEmail).parseRawEmail(0xc0005f0400, {0xc00027ea80?, 0x950a6f?})
        /go/pkg/mod/github.com/sendgrid/sendgrid-go@v3.15.0+incompatible/helpers/inbound/inbound.go:165 +0x4d4
github.com/sendgrid/sendgrid-go/helpers/inbound.(*ParsedEmail).parse(0xc0005f0400)
        /go/pkg/mod/github.com/sendgrid/sendgrid-go@v3.15.0+incompatible/helpers/inbound/inbound.go:121 +0x3d6
github.com/sendgrid/sendgrid-go/helpers/inbound.ParseWithAttachments(0xc0002c2200)
        /go/pkg/mod/github.com/sendgrid/sendgrid-go@v3.15.0+incompatible/helpers/inbound/inbound.go:79 +0x139
mailProcessor/routes.ApiRoutes.func1({0xa248d8, 0xc0005b4540}, 0xc0002c2200)
        /builds/LeSqueed/gomailprocessor/routes/api.go:51 +0x310
net/http.HandlerFunc.ServeHTTP(0xc0002c2100?, {0xa248d8?, 0xc0005b4540?}, 0x800?)
        /usr/local/go/src/net/http/server.go:2109 +0x2f
github.com/gorilla/mux.(*Router).ServeHTTP(0xc000508c00, {0xa248d8, 0xc0005b4540}, 0xc0002c2000)
        /go/pkg/mod/github.com/gorilla/mux@v1.8.1/mux.go:212 +0x1cf
net/http.serverHandler.ServeHTTP({0xa237a8?}, {0xa248d8, 0xc0005b4540}, 0xc0002c2000)
        /usr/local/go/src/net/http/server.go:2947 +0x30c
net/http.(*conn).serve(0xc0001a80a0, {0xa25120, 0xc000206480})
        /usr/local/go/src/net/http/server.go:1991 +0x607
created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:3102 +0x4db

I tried mutliple things, including downgrading to Go 1.19. Also tried sending mail from either the webmail or an gmail account, but non of them are getting processed correctly.

Dysar commented 1 month ago

Same problem here

Dysar commented 1 month ago

@LeSqueed were you able to resolve somehow?

LeSqueed commented 1 month ago

@Dysar sadly not. Gave up on using this implementation. For now sticking to my current parser in php that works. Till I get the time to perhaps write a custom one from scratch. Or till someone shows signs of life here.