1 回答
TA贡献1719条经验 获得超6个赞
您可以让 Mailgun 在您域的路由设置中发送回调请求示例: https: //app.mailgun.com/app/routes。要快速概览,请在http://bin.mailgun.net上创建一个垃圾箱并输入该 URL。
您将看到“转发”操作的请求包含 multipart/form-data 主体,因此您使用http.Request.FormFile访问附件:
http.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
// r.FormFile and r.FormValue will call ParseMultipartForm
// automatically if necessary, but they ignore any errors. For
// robustness we do it ourselves.
if err := r.ParseMultipartForm(10 << 20); err != nil {
http.Error(w, err.Error(), 500)
return
}
// The "attachment-count" field reports how many attachments there are.
n, _ := strconv.Atoi(r.FormValue("attachment-count"))
// The file fields are then named "attachment-1", "attachment-2", ..., "attachment-n".
for i := 1; i <= n; i++ {
fieldName := fmt.Sprintf("attachment-%d", i)
file, header, err := r.FormFile(fieldName)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
fmt.Printf("%s (%d bytes)\n", header.Filename, header.Size)
var _ = file // call file.Read() to read the file contents
}
})
对于 Mailgun 的测试负载,输出将是:
crabby.gif (2785 bytes)
attached_файл.txt (32 bytes)
- 1 回答
- 0 关注
- 105 浏览
添加回答
举报