mirror of
https://github.com/ivabus/pop
synced 2024-11-10 02:25:19 +03:00
feat: attachments
This commit is contained in:
parent
f7b87a514c
commit
ec14061785
34
email.go
34
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)
|
||||
|
|
Loading…
Reference in a new issue