Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Email 如何在Go中从Mailgun接收文件附件_Email_Go_Mailgun - Fatal编程技术网

Email 如何在Go中从Mailgun接收文件附件

Email 如何在Go中从Mailgun接收文件附件,email,go,mailgun,Email,Go,Mailgun,我正在尝试了解如何接收来自golang mailgun的电子邮件文件附件。它们仅提供python示例: 我应该如何处理Go中的此部分,或者此“文件”是什么类型 # attachments: for key in request.FILES: file = request.FILES[key] 您可以让Mailgun在域的路由设置中发送回调请求示例:。要快速查看,请在上创建一个bin并输入该URL 您将看到“转发”操作的请求包含多部分/表单数据体,因此

我正在尝试了解如何接收来自golang mailgun的电子邮件文件附件。它们仅提供python示例:

我应该如何处理Go中的此部分,或者此“文件”是什么类型

# attachments:
         for key in request.FILES:
             file = request.FILES[key]

您可以让Mailgun在域的路由设置中发送回调请求示例:。要快速查看,请在上创建一个bin并输入该URL

您将看到“转发”操作的请求包含多部分/表单数据体,因此您可以使用以下方式访问附件:

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
    }
})

在这行中得到了由ParseMultipartForm处理的http:multipart:
mr,err:=r.MultipartReader()
我用的是gin-gonic,它实际上是
mr,err:=c.Request.MultipartReader()
但是
c.Request
无论如何是
*http.Request
。我不熟悉gin,但从你可以调用的代码判断。我忘了Request.ParseMultipartForm。这实际上更简单,并且会更新答案。谢谢你的帮助,
c.MultipartForm()
做到了。顺便说一下,Gin还有c.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
    }
})
crabby.gif (2785 bytes)
attached_файл.txt (32 bytes)