Event Webhook Go Code Example
(information)
Info
We recommend using our official Go SDK, our client library with full documentation, when integrating with SendGrid's Inbound Parse Webhook.
In this example, we want to parse all emails at address@email.sendgrid.biz
and post the parsed email to http://sendgrid.biz/upload
Given this scenario, the following are the parameters you would set at the Parse API settings page:
Hostname: email.sendgrid.biz
URL: http://sendgrid.biz/upload
To test this scenario, we sent an email to example@example.com
and created the following code:
12package main34import (5"fmt"6"net/http"7"io/ioutil"8"log"9"github.com/sendgrid/sendgrid-go"1011)1213func Parse (w http.ResponseWriter, req *http.Request) {14//Get Email Values15to := req.FormValue("from")16subject := req.FormValue("subject")17body:= req.FormValue("text")1819//Get Uploaded File20file, handler, err := req.FormFile("attachment1")21if err != nil {22fmt.Println(err)23}24data, err := ioutil.ReadAll(file)25if err != nil {26fmt.Println(err)27}28err = ioutil.WriteFile(handler.Filename, data, 0777)29if err != nil {30fmt.Println(err)31}32}3334func main() {35http.HandleFunc("/upload", Parse)36err := http.ListenAndServe(":3000", nil)37if err != nil {38log.Fatal("ListenAndServe: ", err)39}40}41