diff --git a/emails.go b/emails.go index d125e58..3372954 100644 --- a/emails.go +++ b/emails.go @@ -64,19 +64,25 @@ type Attachment struct { // Path where the attachment file is hosted instead of providing the // content directly. Path string + + // Content type for the attachment, if not set will be derived from + // the filename property + ContentType string } // MarshalJSON overrides the regular JSON Marshaller to ensure that the // attachment content is provided in the way Resend expects. func (a *Attachment) MarshalJSON() ([]byte, error) { na := struct { - Content []int `json:"content,omitempty"` - Filename string `json:"filename,omitempty"` - Path string `json:"path,omitempty"` + Content []int `json:"content,omitempty"` + Filename string `json:"filename,omitempty"` + Path string `json:"path,omitempty"` + ContentType string `json:"content_type,omitempty"` }{ - Filename: a.Filename, - Path: a.Path, - Content: BytesToIntArray(a.Content), + Filename: a.Filename, + Path: a.Path, + Content: BytesToIntArray(a.Content), + ContentType: a.ContentType, } return json.Marshal(na) } diff --git a/emails_test.go b/emails_test.go index 72d5723..f238668 100644 --- a/emails_test.go +++ b/emails_test.go @@ -70,7 +70,7 @@ func TestSendEmailWithAttachment(t *testing.T) { if err != nil { t.Errorf("failed to read request body: %v", err) } - exp := `"attachments":[{"content":[104,101,108,108,111],"filename":"hello.txt"}]` + exp := `"attachments":[{"content":[104,101,108,108,111],"filename":"hello.txt","content_type":"text/plain"}]` if !bytes.Contains(content, []byte(exp)) { t.Errorf("request body does not include attachment data") } @@ -87,8 +87,9 @@ func TestSendEmailWithAttachment(t *testing.T) { To: []string{"d@e.com"}, Attachments: []*Attachment{ { - Content: []byte("hello"), - Filename: "hello.txt", + Content: []byte("hello"), + Filename: "hello.txt", + ContentType: "text/plain", }, }, } diff --git a/examples/with_attachments.go b/examples/with_attachments.go index c225e8c..aced7e8 100644 --- a/examples/with_attachments.go +++ b/examples/with_attachments.go @@ -27,13 +27,15 @@ func withAttachments() { // Create attachments objects pdfAttachmentFromLocalFile := &resend.Attachment{ - Content: f, - Filename: "invoice1.pdf", + Content: f, + Filename: "invoice1.pdf", + ContentType: "application/pdf", } pdfAttachmentFromRemotePath := &resend.Attachment{ - Path: "https://github.com/resend/resend-go/raw/main/resources/invoice.pdf", - Filename: "invoice2.pdf", + Path: "https://github.com/resend/resend-go/raw/main/resources/invoice.pdf", + Filename: "invoice2.pdf", + ContentType: "application/pdf", } params := &resend.SendEmailRequest{ diff --git a/resend.go b/resend.go index a89f731..0ae999d 100644 --- a/resend.go +++ b/resend.go @@ -12,7 +12,7 @@ import ( ) const ( - version = "2.9.0" + version = "2.10.0" userAgent = "resend-go/" + version contentType = "application/json" )