Skip to content

Instantly share code, notes, and snippets.

@bsiegert
Created March 22, 2019 09:50
Show Gist options
  • Save bsiegert/af6bf8483af4c3f879874d7a613a08da to your computer and use it in GitHub Desktop.
Save bsiegert/af6bf8483af4c3f879874d7a613a08da to your computer and use it in GitHub Desktop.
Parse cloudmailin emails
package mailparser
import (
"bytes"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
)
const Boundary = "----cloudmailinboundry"
func LogMail(w http.ResponseWriter, req *http.Request) {
r := multipart.NewReader(req.Body, Boundary)
for i := 0; ; i++ {
part, err := r.NextPart()
if err == io.EOF {
w.WriteHeader(204)
return
}
if err != nil {
log.Println(err)
w.WriteHeader(500)
return
}
var b bytes.Buffer
fmt.Fprintf(&b, "Part %d (%v)", i, part.FormName())
if fn := part.FileName(); fn != "" {
fmt.Fprintf(&b, " filename %q", fn)
}
fmt.Fprintf(&b, "\n\n")
io.Copy(&b, part)
log.Println(b.String())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment