inbound

package
v3.10.5+incompatible Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2022 License: MIT Imports: 6 Imported by: 1

README

This helper is a stand-alone module to help get you started consuming and processing Inbound Parse data.

Table of Contents

Example Usage

package main

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

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

func inboundHandler(response http.ResponseWriter, request *http.Request) {
	parsedEmail, err := Parse(request)
	if err != nil {
		log.Fatal(err)
	}
    
	fmt.Print(parsedEmail.Headers["From"])
	
	for filename, contents := range parsedEmail.Attachments {
		// Do something with an attachment
		handleAttachment(filename, contents)
	}
    
	for section, body := range parsedEmail.Body {
		// Do something with the email body
		handleEmail(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")
	}
}

Testing the Source Code

Tests are located in the helpers/inbound folder:

Learn about testing this code here.

Contributing

If you would like to contribute to this project, please see our contributing guide. Thanks!

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ParsedEmail

type ParsedEmail struct {
	Headers     map[string]string
	Body        map[string]string
	Attachments map[string][]byte
	// contains filtered or unexported fields
}
Example (ParseHeaders)
headers := `
Foo: foo
Bar: baz
`
email := ParsedEmail{
	Headers:     make(map[string]string),
	Body:        make(map[string]string),
	Attachments: make(map[string][]byte),
	rawRequest:  nil,
}
email.parseHeaders(headers)
fmt.Println(email.Headers["Foo"])
fmt.Println(email.Headers["Bar"])
Output:

foo
baz
Example (ParseRawEmail)
rawEmail := `
To: test@example.com
From: example@example.com
Subject: Test Email
Content-Type: multipart/mixed; boundary=TwiLIo

--TwiLIo
Content-Type: text/plain; charset=UTF-8

Hello Twilio SendGrid!
--TwiLIo
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html><body><strong>Hello Twilio SendGrid!</body></html>
--TwiLIo--
`
email := ParsedEmail{
	Headers:     make(map[string]string),
	Body:        make(map[string]string),
	Attachments: make(map[string][]byte),
	rawRequest:  nil,
}

if err := email.parseRawEmail(rawEmail); err != nil {
	log.Fatal(err)
}

for key, value := range email.Headers {
	fmt.Println(key, value)
}
fmt.Println(email.Body["text/plain; charset=UTF-8"])
Output:

To test@example.com
From example@example.com
Subject Test Email
Content-Type multipart/mixed; boundary=TwiLIo
Hello Twilio SendGrid!

func Parse

func Parse(request *http.Request) (*ParsedEmail, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL