feat: attachments
This commit is contained in:
34
email.go
34
email.go
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
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))
|
client := resend.NewClient(os.Getenv(RESEND_API_KEY))
|
||||||
|
|
||||||
var html, text = bytes.NewBufferString(""), bytes.NewBufferString("")
|
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)
|
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{
|
request := &resend.SendEmailRequest{
|
||||||
From: from,
|
From: from,
|
||||||
To: to,
|
To: to,
|
||||||
Subject: subject,
|
Subject: subject,
|
||||||
Html: html.String(),
|
Html: html.String(),
|
||||||
Text: text.String(),
|
Text: text.String(),
|
||||||
|
Attachments: attachments,
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = client.Emails.Send(request)
|
_, err = client.Emails.Send(request)
|
||||||
|
|||||||
Reference in New Issue
Block a user