2

我正在尝试使用 DropzoneJS 实现文件上传器并使用 Go 编写后端(第一次尝试使用该语言进行编程)。我正在尝试使用https://github.com/hjr265/postmark.go这似乎是 Go 库的 Postmarks 推荐。

该应用程序由 3 个不同的处理程序组成,一个提供在标头中使用的令牌(下面代码中的 ApplicationID)。

第二个处理程序处理单个文件上传到服务器。这些文件在与 ApplicationID 同名的目录中组合在一起。

到目前为止,一切都运行良好。

最终处理程序循环遍历给定目录中的文件并将它们添加到附件切片中。

我认为我能够做的是创建一个附件并将其添加到一个附件中,然后将其与消息一起传递,但是每当我测试这个时,我都会发送一封电子邮件,其中包含除附件之外的所有内容。这是我的提交处理程序代码:

http.HandleFunc("/submission", func(w http.ResponseWriter, r *http.Request){

    client := postmark.Client{
        ApiKey: "SOME-POSTMARK-API-KEY",
        Secure: true,
    }
    fmt.Println(client)

    attachments := []postmark.Attachment{}

    switch r.Method {
        case "POST":
            appId := r.Header.Get("ApplicationID")

            files, _ := ioutil.ReadDir("/tmp/uploader/" + appId)
            for _, f := range files {
                fullpath := "/tmp/uploader/" + appId + "/" + string(f.Name())
                extension := filepath.Ext(fullpath)
                mimeType := mime.TypeByExtension(extension)
                upload, _ := os.Open(fullpath)
                attachment := postmark.Attachment{
                    Name: f.Name(),
                    Content: upload,
                    ContentType: mimeType,
                }
                attachments = append(attachments, attachment)
            }
    }

    res, err := client.Send(&postmark.Message{
        From: &mail.Address{
            Name:    "SENDER-NAME",
            Address: "sender@example.com",
        },
        To: []*mail.Address{
            {
                Name:    "RECIPIENT-NAME",
                Address: "recipient@example.com",
            },
        },
        Subject:  "Hooking up Postmark for sending email",
        TextBody: strings.NewReader("MESSAGE-BODY-AS-TEXT"),
        Attachments: attachments,
    })

    if err != nil {
        panic(err)
    }

    fmt.Printf("%#v\n", res)
})

这是我从 Postmark 得到的结果的一个例子:

 &postmark.Result{ErrorCode:0, Message:"OK", MessageID:"29fdf3da-ae5d-40c8-af92-c13e93fa6b69", SubmittedAt:"2015-10-22T07:39:17.2297478-04:00", To:"\"RECIPIENT-NAME\" <recipient@example.com>"}

我希望我在处理附件的方式上犯了一个新手错误,并且从根本上错过了 Go 中简单的东西?

次要注意:我恢复了 repo 中添加模板的提交,因为我没有使用模板,并且我不确定如何在没有指定模板的情况下发送

4

0 回答 0