feat: attachments

This commit is contained in:
Maas Lalani 2023-06-15 12:02:09 -04:00
parent f7b87a514c
commit ec14061785
No known key found for this signature in database
GPG key ID: 5A6ED5CBF1A0A000

View file

@ -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)