Skip to content

Commit

Permalink
Merge pull request #269 from mailgun/115-route-forward
Browse files Browse the repository at this point in the history
Add a struct and function to extract route forward payload
  • Loading branch information
tsidei authored Nov 3, 2021
2 parents 25ac2da + e3c6b7a commit 3806ecd
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package mailgun

import (
"context"
"encoding/json"
"net/url"
"strconv"
"time"
)

// A Route structure contains information on a configured or to-be-configured route.
Expand All @@ -28,6 +31,68 @@ type Route struct {
Id string `json:"id,omitempty"`
}

// ForwardedMessage represents the payload the server will get on match
// You can use ExtractForwardRoute() to extract PostForm into the struct, or you can use only the struct and parse the form manually
type ForwardedMessage struct {
BodyPlain string // body-plain
From string // from
MessageHeaders map[string]string // message-headers
Recipient string // recipient
Sender string // sender
Signature string // signature
StrippedHTML string // stripped-html
StrippedText string // stripped-text
Subject string // subject
Timestamp time.Time // timestamp
Token string // token
}

// ExtractForwardedMessage extracts the forward route payload values from a parsed PostForm
// Example usage:
// func Handler(w http.ResponseWriter, r *http.Request) {
// err := r.ParseForm()
// if err != nil {
// log.Fatal(err)
// }
// forwardRoute := mailgun.ExtractForwardedMessage(r.PostForm)
// fmt.Printf("Forwarded message: %#v", forwardRoute)
//}
func ExtractForwardedMessage(formValues url.Values) ForwardedMessage {
forwardedMessage := ForwardedMessage{}
forwardedMessage.BodyPlain = formValues.Get("body-plain")
forwardedMessage.From = formValues.Get("from")
forwardedMessage.Recipient = formValues.Get("recipient")
forwardedMessage.Sender = formValues.Get("sender")
forwardedMessage.Signature = formValues.Get("signature")
forwardedMessage.StrippedHTML = formValues.Get("stripped-html")
forwardedMessage.StrippedText = formValues.Get("stripped-text")
forwardedMessage.Subject = formValues.Get("subject")
forwardedMessage.Token = formValues.Get("token")

timestampStr := formValues.Get("timestamp")
timeInt, err := strconv.Atoi(timestampStr)
if err != nil {
timeInt = 0
}
forwardedMessage.Timestamp = time.Unix(int64(timeInt), 0)

headersStr := formValues.Get("message-headers")
headersParsed := make([][]string, 0)
messageHeaders := make(map[string]string)
err = json.Unmarshal([]byte(headersStr), &headersParsed)
if err == nil {
for _, header := range headersParsed {
if len(header) < 2 {
continue
}
messageHeaders[header[0]] = header[1]
}
}
forwardedMessage.MessageHeaders = messageHeaders

return forwardedMessage
}

type routesListResponse struct {
// is -1 if Next() or First() have not been called
TotalCount int `json:"total_count"`
Expand Down

0 comments on commit 3806ecd

Please sign in to comment.