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