dtl19910708 2015-10-22 12:36
浏览 16

使用Go发送附件到邮戳

I was trying to implement a file uploader using DropzoneJS and writing the backend using Go (first attempt at programming with the language). I'm trying to use https://github.com/hjr265/postmark.go which appears to be Postmarks recommendation for Go libraries.

The app consists of 3 different handlers, one provides a token (ApplicationID in the code below) which is used in the headers.

The second handler handles the individual file uploads to the server. The files are grouped together inside a directory with the same name as the ApplicationID.

Up to here everything is working well.

The final handler loops over the files in a given directory and adds them to a slice of Attachments.

What I thought I'd be able to do is create an Attachment and add it to a slice of attachments and pass that in with the message however whenever I test this I send an email with everything except the attachments. Here is my code for the submission handler:

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
", res)
})

And this is an example of the result I'm getting back from 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>"}

I'm hoping that I'm making a rookie mistake with how I'm working with the attachments and fundamentally missing something easy in Go?

Minor Note: I reverted the commit in the repo that added templating as I not using templates and I wasn't sure how to send without a template being specified

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探