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:
22
email.go
22
email.go
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user