Skip to content

Commit cb42c17

Browse files
authored
fix: refactor and fix inbound email handling (#388)
* fix: refactor and fix inbound email handling
1 parent adab2f1 commit cb42c17

File tree

4 files changed

+197
-302
lines changed

4 files changed

+197
-302
lines changed

helpers/inbound/README.md

+39-105
Original file line numberDiff line numberDiff line change
@@ -2,116 +2,50 @@
22

33
## Table of Contents
44

5-
* [Quick Start for Local Testing with Sample Data](#quick_start_local_sample)
6-
* [Quick Start for Local Testing with Real Data](#quick_start_local_real)
7-
* [Deploy to Heroku](#heroku)
8-
* [Code Walkthrough](#code_walkthrough)
5+
* [Example Usage](#example-usage)
96
* [Testing the Source Code](#testing)
107
* [Contributing](#contributing)
118

12-
<a name="quick_start_local_sample"></a>
13-
# Quick Start for Local Testing with Sample Data
14-
15-
```bash
16-
git clone https://github.com/sendgrid/sendgrid-go.git
17-
cd sendgrid-go
18-
```
19-
20-
Run the Inbound Parse listener in your terminal:
21-
22-
```bash
23-
cd helpers/inbound/
24-
go run inbound.go
25-
```
26-
27-
In another terminal, run the test data sender:
28-
29-
```bash
30-
cd [path to sendgrid-go/helpers/inbound]
31-
go run inbound.go ./sample_data/default_data.txt http://127.0.0.1:8000/inbound
32-
```
33-
34-
More sample data can be found [here](https://github.com/sendgrid/sendgrid-go/tree/master/helpers/inbound/sample_data).
35-
36-
View the results in the first terminal.
37-
38-
<a name="quick_start_local_real"></a>
39-
# Quick Start for Local Testing with Real Data
40-
41-
[Setup your MX records.](https://sendgrid.com/docs/Classroom/Basics/Inbound_Parse_Webhook/setting_up_the_inbound_parse_webhook.html#-Setup) Depending on your domain name host, you may need to wait up to 48 hours for the settings to propagate.
42-
43-
Run the Inbound Parse listener in your terminal:
44-
45-
```bash
46-
git clone https://github.com/sendgrid/sendgrid-go.git
47-
go run inbound.go
48-
```
49-
50-
In another terminal, use [ngrok](https://ngrok.com/) to allow external access to your machine:
51-
```bash
52-
ngrok http 8000
9+
# Example Usage
10+
11+
```go
12+
package main
13+
14+
import (
15+
"fmt"
16+
"log"
17+
"net/http"
18+
19+
"github.com/sendgrid/sendgrid-go/helpers/inbound"
20+
)
21+
22+
func inboundHandler(response http.ResponseWriter, request *http.Request) {
23+
parsedEmail := Parse(request)
24+
25+
fmt.Print(parsedEmail.Headers["From"])
26+
27+
for filename, contents := range parsedEmail.Attachments {
28+
// Do something with an attachment
29+
handleAttachment(filename, contents)
30+
}
31+
32+
for section, body := range parsedEmail.Body {
33+
// Do something with the email body
34+
handleEmail(body)
35+
}
36+
37+
// Twilio SendGrid needs a 200 OK response to stop POSTing
38+
response.WriteHeader(http.StatusOK)
39+
}
40+
41+
func main() {
42+
http.HandleFunc("/inbound", inboundHandler)
43+
if err := http.ListenAndServe(":8000", nil); err != nil {
44+
log.Fatalln("Error")
45+
}
46+
}
5347
```
5448

55-
Update your Twilio SendGrid Incoming Parse settings: [Settings Page](https://app.sendgrid.com/settings/parse) | [Docs](https://sendgrid.com/docs/Classroom/Basics/Inbound_Parse_Webhook/setting_up_the_inbound_parse_webhook.html#-Pointing-to-a-Hostname-and-URL)
56-
57-
- For the HOSTNAME field, use the domain that you changed the MX records (e.g. inbound.yourdomain.com)
58-
- For the URL field, use the URL generated by ngrok + /inbound, e.g http://XXXXXXX.ngrok.io/inbound
59-
60-
Next, send an email to [anything]@inbound.yourdomain.com, then look at the terminal where you started the Inbound Parse listener.
61-
62-
<a name="heroku"></a>
63-
# Deploy to Heroku
64-
65-
Get a [Heroku](https://www.heroku.com) account.
66-
67-
[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/sendgrid/sendgrid-go/tree/heroku-deployment)
68-
69-
[Setup your MX records.](https://sendgrid.com/docs/Classroom/Basics/Inbound_Parse_Webhook/setting_up_the_inbound_parse_webhook.html#-Setup) Depending on your domain name host, you may need to wait up to 48 hours for the settings to propagate.
70-
71-
Update your Twilio SendGrid Incoming Parse settings: [Settings Page](https://app.sendgrid.com/settings/parse) | [Docs](https://sendgrid.com/docs/Classroom/Basics/Inbound_Parse_Webhook/setting_up_the_inbound_parse_webhook.html#-Pointing-to-a-Hostname-and-URL)
72-
73-
- For the HOSTNAME field, use the domain that you changed the MX records (e.g. inbound.yourdomain.com)
74-
- For the URL field, use the URL generated by Heroku + /inbound, e.g https://[name-of-your-app].herokuapp.com/inbound
75-
76-
Next, send an email to [anything]@inbound.yourdomain.com, then look at your Heroku logs: https://dashboard.heroku.com/apps/[name-of-your-app]/logs
77-
78-
While you are waiting for your MX records to propagate, you can test by using the test data sender:
79-
80-
```bash
81-
git clone https://github.com/sendgrid/sendgrid-go.git
82-
cd sendgrid-go
83-
go build
84-
./sendgrid-go ./helpers/inbound/sample_data/default_data.txt https://[name-of-your-app].herokuapp.com/inbound
85-
```
86-
87-
To make changes: clone, modify and push the changes. [Heroku CLI](https://devcenter.heroku.com/articles/heroku-command-line) is required.
88-
89-
For example:
90-
```bash
91-
git clone [repo url]
92-
git checkout heroku-deployment
93-
heroku git:remote -a [name-of-your-app]
94-
---make changes---
95-
git add -A
96-
git commit -m "updated configuration"
97-
git push heroku heroku-deployment:master
98-
```
99-
100-
<a name="code_walkthrough"></a>
101-
# Code Walkthrough
102-
103-
## inbound.go
104-
105-
This module runs a net/http server, that by default (you can change those settings [here](https://github.com/sendgrid/sendgrid-go/blob/master/helpers/inbound/conf.json), listens for POSTs on http://localhost:8000. When the server receives the POST, it parses and prints the key/value data.
106-
107-
## conf.json
108-
109-
This file contains application environment variables (located in [conf.json](https://github.com/sendgrid/sendgrid-go/blob/master/helpers/inbound/conf.json)).
110-
111-
## inbound.go & /sample_data
112-
113-
This module, in conjunction with the sample data, is also used to send sample test data. It is useful for testing and development, particularly while you wait for your MX records to propagate.
114-
11549
<a name="testing"></a>
11650
# Testing the Source Code
11751

helpers/inbound/conf.json

-4
This file was deleted.

0 commit comments

Comments
 (0)