Skip to content

Commit

Permalink
feat: support for ContentType attr (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
drish authored Jul 4, 2024
1 parent e0e459c commit b9dac9d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
18 changes: 12 additions & 6 deletions emails.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions emails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -87,8 +87,9 @@ func TestSendEmailWithAttachment(t *testing.T) {
To: []string{"[email protected]"},
Attachments: []*Attachment{
{
Content: []byte("hello"),
Filename: "hello.txt",
Content: []byte("hello"),
Filename: "hello.txt",
ContentType: "text/plain",
},
},
}
Expand Down
10 changes: 6 additions & 4 deletions examples/with_attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion resend.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const (
version = "2.9.0"
version = "2.10.0"
userAgent = "resend-go/" + version
contentType = "application/json"
)
Expand Down

0 comments on commit b9dac9d

Please sign in to comment.