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 關注
- 136 瀏覽
添加回答
舉報