From ec140617855374ea76d1536c1c41746414bfae27 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Thu, 15 Jun 2023 12:02:09 -0400 Subject: [PATCH] feat: attachments --- email.go | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/email.go b/email.go index aa74a28..53409c0 100644 --- a/email.go +++ b/email.go @@ -3,6 +3,7 @@ package main import ( "bytes" "os" + "path/filepath" "strings" tea "github.com/charmbracelet/bubbletea" @@ -39,7 +40,7 @@ func (m Model) sendEmailCmd() tea.Cmd { } } -func sendEmail(to []string, from, subject, body string, attachments []string) error { +func sendEmail(to []string, from, subject, body string, paths []string) error { client := resend.NewClient(os.Getenv(RESEND_API_KEY)) var html, text = bytes.NewBufferString(""), bytes.NewBufferString("") @@ -48,12 +49,33 @@ func sendEmail(to []string, from, subject, body string, attachments []string) er text.WriteString(body) } + attachments := make([]resend.Attachment, len(paths)) + for i, a := range paths { + abs, err := filepath.Abs(a) + if err != nil { + continue + } + content, err := os.ReadFile(abs) + if err != nil { + continue + } + attachments[i] = resend.Attachment{ + Content: string(content), + Filename: filepath.Base(a), + } + } + + if len(attachments) == 0 { + attachments = nil + } + request := &resend.SendEmailRequest{ - From: from, - To: to, - Subject: subject, - Html: html.String(), - Text: text.String(), + From: from, + To: to, + Subject: subject, + Html: html.String(), + Text: text.String(), + Attachments: attachments, } _, err = client.Emails.Send(request)