Save email body to tmp file when error (#64)

* store email body when send fails

* increase error display duration

* feat: save email body

---------

Co-authored-by: Allan Martinez <martinezallan123@gmail.com>
This commit is contained in:
Maas Lalani
2024-04-25 10:23:52 -04:00
committed by GitHub
parent 7240e0dbd9
commit ef84570048
4 changed files with 23 additions and 26 deletions

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
@@ -48,6 +49,10 @@ func (m Model) sendEmailCmd() tea.Cmd {
err = errors.New("[ERROR]: unknown delivery method")
}
if err != nil {
path, storeErr := saveTmp(m.Body.Value())
if storeErr == nil {
err = fmt.Errorf("%w\nEmail saved to: %s", err, path)
}
return sendEmailFailureMsg(err)
}
return sendEmailSuccessMsg{}
@@ -183,3 +188,20 @@ func makeAttachments(paths []string) []resend.Attachment {
return attachments
}
// saveTmp is a helper function that stores a string in a temporary file.
// It returns the path of the file created.
func saveTmp(s string) (string, error) {
f, err := os.CreateTemp("", fmt.Sprintf("pop-%s-*.txt", time.Now().Format("2006-01-02")))
if err != nil {
return "", fmt.Errorf("creating temp file: %w", err)
}
defer f.Close()
_, err = f.WriteString(s)
if err != nil {
return "", fmt.Errorf("error writing to %s: %w", f.Name(), err)
}
return f.Name(), nil
}