Skip to content

Commit

Permalink
Merge pull request #2 from wneessen/empty-subject-fix
Browse files Browse the repository at this point in the history
Fix index out of range error when no subject is set
  • Loading branch information
wneessen authored Oct 3, 2022
2 parents 6b51bd7 + e65ff62 commit 1565172
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions subject_capitalize/subject_capitalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func New(l language.Tag) *Middleware {
// Handle is the handler method that satisfies the mail.Middleware interface
func (c Middleware) Handle(m *mail.Msg) *mail.Msg {
cs := m.GetGenHeader(mail.HeaderSubject)
if len(cs) <= 0 {
return m
}
cp := cases.Title(c.l)
m.Subject(cp.String(cs[0]))
return m
Expand Down
8 changes: 8 additions & 0 deletions subject_capitalize/subject_capitalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ func TestMiddleware_Handle(t *testing.T) {
t.Errorf("middleware failed. Expected: %q in subject, got: %q", "This Is A Test", buf.String())
}
}

func TestMiddleware_HandleEmpty(t *testing.T) {
m := mail.NewMsg(mail.WithMiddleware(New(language.English)))
buf := bytes.Buffer{}
if _, err := m.WriteTo(&buf); err != nil {
t.Errorf("failed to write mail message to buffer: %s", err)
}
}

0 comments on commit 1565172

Please sign in to comment.