From 04719f8dee03e27544932ddc76c8bcc37d30a08b Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Tue, 28 Dec 2021 01:41:00 +0100 Subject: [PATCH] Flip title and message if message is empty --- server/smtp_server.go | 7 ++++++- server/smtp_server_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/server/smtp_server.go b/server/smtp_server.go index 19fcca44..689deaf3 100644 --- a/server/smtp_server.go +++ b/server/smtp_server.go @@ -110,11 +110,12 @@ func (s *smtpSession) Data(r io.Reader) error { if err != nil { return err } + body = strings.TrimSpace(body) if len(body) > conf.MessageLimit { body = body[:conf.MessageLimit] } m := newDefaultMessage(s.topic, body) - subject := msg.Header.Get("Subject") + subject := strings.TrimSpace(msg.Header.Get("Subject")) if subject != "" { dec := mime.WordDecoder{} subject, err := dec.DecodeHeader(subject) @@ -123,6 +124,10 @@ func (s *smtpSession) Data(r io.Reader) error { } m.Title = subject } + if m.Title != "" && m.Message == "" { + m.Message = m.Title // Flip them, this makes more sense + m.Title = "" + } if err := s.backend.sub(m); err != nil { return err } diff --git a/server/smtp_server_test.go b/server/smtp_server_test.go index c842365a..ac639990 100644 --- a/server/smtp_server_test.go +++ b/server/smtp_server_test.go @@ -39,6 +39,38 @@ Content-Type: text/html; charset="UTF-8" require.Nil(t, session.Data(strings.NewReader(email))) } +func TestSmtpBackend_MultipartNoBody(t *testing.T) { + email := `MIME-Version: 1.0 +Date: Tue, 28 Dec 2021 01:33:34 +0100 +Message-ID: +Subject: This email has a subject but no body +From: Phil +To: ntfy-emailtest@ntfy.sh +Content-Type: multipart/alternative; boundary="000000000000bcf4a405d429f8d4" + +--000000000000bcf4a405d429f8d4 +Content-Type: text/plain; charset="UTF-8" + + + +--000000000000bcf4a405d429f8d4 +Content-Type: text/html; charset="UTF-8" + +

+ +--000000000000bcf4a405d429f8d4--` + _, backend := newTestBackend(t, func(m *message) error { + require.Equal(t, "emailtest", m.Topic) + require.Equal(t, "", m.Title) // We flipped message and body + require.Equal(t, "This email has a subject but no body", m.Message) + return nil + }) + session, _ := backend.AnonymousLogin(nil) + require.Nil(t, session.Mail("phil@example.com", smtp.MailOptions{})) + require.Nil(t, session.Rcpt("ntfy-emailtest@ntfy.sh")) + require.Nil(t, session.Data(strings.NewReader(email))) +} + func TestSmtpBackend_Plaintext(t *testing.T) { email := `Date: Tue, 28 Dec 2021 00:30:10 +0100 Message-ID: