Created
March 22, 2019 09:50
-
-
Save bsiegert/af6bf8483af4c3f879874d7a613a08da to your computer and use it in GitHub Desktop.
Parse cloudmailin emails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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