Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store email body on failed send #50

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions email.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down