Skip to content

Commit

Permalink
fix: seed attachments from command line
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Jun 27, 2023
1 parent bb54eb5 commit 50e40f8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
38 changes: 21 additions & 17 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,13 @@ func sendEmail(to []string, from, subject, body string, paths []string) error {
// If the conversion fails, we'll simply send the plain-text body.
_ = goldmark.Convert([]byte(body), html)

attachments := make([]resend.Attachment, len(paths))
for i, a := range paths {
f, err := os.ReadFile(a)
if err != nil {
continue
}
attachments[i] = resend.Attachment{
Content: string(f),
Filename: filepath.Base(a),
}
}

if len(attachments) == 0 {
attachments = nil
}

request := &resend.SendEmailRequest{
From: from,
To: to,
Subject: subject,
Html: html.String(),
Text: body,
Attachments: attachments,
Attachments: makeAttachments(paths),
}

_, err := client.Emails.Send(request)
Expand All @@ -75,3 +59,23 @@ func sendEmail(to []string, from, subject, body string, paths []string) error {

return nil
}

func makeAttachments(paths []string) []resend.Attachment {
if len(paths) == 0 {
return nil
}

attachments := make([]resend.Attachment, len(paths))
for i, a := range paths {
f, err := os.ReadFile(a)
if err != nil {
continue
}
attachments[i] = resend.Attachment{
Content: string(f),
Filename: filepath.Base(a),
}
}

return attachments
}
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ var rootCmd = &cobra.Command{
}

p := tea.NewProgram(NewModel(resend.SendEmailRequest{
From: from,
To: to,
Subject: subject,
Text: body,
From: from,
To: to,
Subject: subject,
Text: body,
Attachments: makeAttachments(attachments),
}))
m, err := p.Run()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ func NewModel(defaults resend.SendEmailRequest) Model {
attachments.SetStatusBarItemName("attachment", "attachments")
attachments.SetShowPagination(false)

for _, a := range defaults.Attachments {
attachments.InsertItem(0, attachment(a.Filename))
}

picker := filepicker.New()
picker.CurrentDirectory, _ = os.UserHomeDir()

Expand Down

0 comments on commit 50e40f8

Please sign in to comment.