2023-06-15 16:45:14 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-06-15 17:30:20 +03:00
|
|
|
"bytes"
|
2023-06-15 16:45:14 +03:00
|
|
|
"os"
|
2023-06-15 19:02:09 +03:00
|
|
|
"path/filepath"
|
2023-06-15 17:30:20 +03:00
|
|
|
"strings"
|
2023-06-15 16:45:14 +03:00
|
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/resendlabs/resend-go"
|
2023-06-15 17:30:20 +03:00
|
|
|
"github.com/yuin/goldmark"
|
2023-06-15 16:45:14 +03:00
|
|
|
)
|
|
|
|
|
2023-06-15 17:30:20 +03:00
|
|
|
const TO_SEPARATOR = ","
|
|
|
|
|
2023-06-15 16:45:14 +03:00
|
|
|
// sendEmailSuccessMsg is the tea.Msg handled by Bubble Tea when the email has
|
|
|
|
// been sent successfully.
|
|
|
|
type sendEmailSuccessMsg struct{}
|
|
|
|
|
|
|
|
// sendEmailFailureMsg is the tea.Msg handled by Bubble Tea when the email has
|
|
|
|
// failed to send.
|
|
|
|
type sendEmailFailureMsg error
|
|
|
|
|
|
|
|
// sendEmailCmd returns a tea.Cmd that sends the email.
|
|
|
|
func (m Model) sendEmailCmd() tea.Cmd {
|
|
|
|
return func() tea.Msg {
|
|
|
|
attachments := make([]string, len(m.Attachments.Items()))
|
|
|
|
for i, a := range m.Attachments.Items() {
|
2023-06-15 19:05:07 +03:00
|
|
|
attachments[i] = a.FilterValue()
|
2023-06-15 16:45:14 +03:00
|
|
|
}
|
2023-06-15 17:30:20 +03:00
|
|
|
err := sendEmail(strings.Split(m.To.Value(), TO_SEPARATOR), m.From.Value(), m.Subject.Value(), m.Body.Value(), attachments)
|
2023-06-15 16:45:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return sendEmailFailureMsg(err)
|
|
|
|
}
|
|
|
|
return sendEmailSuccessMsg{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 19:02:09 +03:00
|
|
|
func sendEmail(to []string, from, subject, body string, paths []string) error {
|
2023-06-15 16:45:14 +03:00
|
|
|
client := resend.NewClient(os.Getenv(RESEND_API_KEY))
|
|
|
|
|
2023-06-21 21:44:39 +03:00
|
|
|
html := bytes.NewBufferString("")
|
|
|
|
// If the conversion fails, we'll simply send the plain-text body.
|
|
|
|
_ = goldmark.Convert([]byte(body), html)
|
2023-06-15 17:30:20 +03:00
|
|
|
|
2023-06-15 16:45:14 +03:00
|
|
|
request := &resend.SendEmailRequest{
|
2023-06-15 19:02:09 +03:00
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Subject: subject,
|
|
|
|
Html: html.String(),
|
2023-06-21 21:44:39 +03:00
|
|
|
Text: body,
|
2023-06-27 21:34:28 +03:00
|
|
|
Attachments: makeAttachments(paths),
|
2023-06-15 16:45:14 +03:00
|
|
|
}
|
|
|
|
|
2023-06-21 21:44:39 +03:00
|
|
|
_, err := client.Emails.Send(request)
|
2023-06-15 16:45:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-06-27 21:34:28 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|