From 26f309937bdd591eab6eba6f2351c59c348a3d07 Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Wed, 25 Jun 2025 13:22:10 -0400 Subject: [PATCH] feat: add FileAttachment type and functions for creating attachments from file content and file path --- client/attachment.go | 33 +++++++++++++++++++++++++++++++++ client/types.go | 16 +++++++++++----- 2 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 client/attachment.go diff --git a/client/attachment.go b/client/attachment.go new file mode 100644 index 0000000..21d4bd2 --- /dev/null +++ b/client/attachment.go @@ -0,0 +1,33 @@ +package client + +import ( + "encoding/base64" + "io" + "os" +) + +func NewAttachmentFromFileContent(fileName string, f []byte) (*FileAttachment, error) { + base64.StdEncoding.Encode(f, f) + + return &FileAttachment{ + Filename: fileName, + Content: string(f), + }, nil +} + +func NewAttachmentFromFilePath(filePath string) (*FileAttachment, error) { + f, err := os.Open(filePath) + if err != nil { + return nil, err + } + defer func(f *os.File) { + _ = f.Close() + }(f) + + content, err := io.ReadAll(f) + if err != nil { + return nil, err + } + + return NewAttachmentFromFileContent(filePath, content) +} diff --git a/client/types.go b/client/types.go index 9110200..554764a 100755 --- a/client/types.go +++ b/client/types.go @@ -24,12 +24,18 @@ type Destination struct { BccAddresses Addresses `json:"BccAddresses,omitempty"` } +type FileAttachment struct { + Filename string `json:"Filename"` + Content string `json:"Content"` +} + type EmailRequest struct { - Source string `json:"Source"` - Destination Destination `json:"Destination"` - Message Message `json:"Message"` - ScheduledTime time.Time `json:"ScheduledTime,omitempty"` - Catch bool `json:"Catch,omitempty"` + Attachments []FileAttachment `json:"Attachments,omitempty"` + Source string `json:"Source"` + Destination Destination `json:"Destination"` + Message Message `json:"Message"` + ScheduledTime time.Time `json:"ScheduledTime,omitempty"` + Catch bool `json:"Catch,omitempty"` } type Email struct {