From 62cb9e2fb1666c90be06d594e8c5ef9ad36810a8 Mon Sep 17 00:00:00 2001 From: Allan Martinez Date: Wed, 24 Jan 2024 18:31:16 -0800 Subject: [PATCH 1/2] store email body when send fails --- email.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/email.go b/email.go index 12fe121..97845e1 100644 --- a/email.go +++ b/email.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/tls" "errors" + "fmt" "os" "path/filepath" "strings" @@ -48,6 +49,11 @@ func (m Model) sendEmailCmd() tea.Cmd { err = errors.New("[ERROR]: unknown delivery method") } if err != nil { + // Store the body if the email fails to send + path, storeErr := storeEmailBody(m.Body.Value()) + if storeErr == nil { + err = fmt.Errorf("%w\nStored a copy of the email body at %s", err, path) + } return sendEmailFailureMsg(err) } return sendEmailSuccessMsg{} @@ -183,3 +189,39 @@ func makeAttachments(paths []string) []resend.Attachment { return attachments } + +// Store the email body in /tmp. Returns a path to the file created. +func storeEmailBody(body string) (string, error) { + dir := "/tmp/pop" + + err := os.MkdirAll(dir, 0700) + if err != nil { + return "", fmt.Errorf("creating %s: %w", dir, err) + } + + curr := time.Now() + fileName := fmt.Sprintf("%d-%d-%d-%d", curr.Year(), curr.Month(), curr.Day(), secondsSinceDayStart(curr)) + fullPath := filepath.Join(dir, fileName) + + file, err := os.Create(fullPath) + if err != nil { + return "", fmt.Errorf("creating %s: %w", fullPath, err) + } + defer file.Close() + + _, err = file.Write([]byte(body)) + if err != nil { + return "", fmt.Errorf("writing to %s: %w", fullPath, err) + } + + return fullPath, nil +} + +// Returns the seconds passed since the start of t. +// The start of t is determined as y:m:d:0:0:0:0 +func secondsSinceDayStart(t time.Time) int { + y, m, d := t.Date() + start := time.Date(y, m, d, 0, 0, 0, 0, t.Location()) + elapsed := time.Since(start) + return int(elapsed.Seconds()) +} From cc718d11a2816f67a5b22f40ec814b317e632ab8 Mon Sep 17 00:00:00 2001 From: Allan Martinez Date: Wed, 24 Jan 2024 18:31:31 -0800 Subject: [PATCH 2/2] increase error display duration --- model.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model.go b/model.go index a6cf30a..0f974d5 100644 --- a/model.go +++ b/model.go @@ -213,7 +213,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.state = editingFrom m.focusActiveInput() m.err = msg - return m, clearErrAfter(5 * time.Second) + return m, clearErrAfter(10 * time.Second) case clearErrMsg: m.err = nil case tea.KeyMsg: