Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for ContentType attr #37

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading