package server import ( "bufio" "github.com/emersion/go-smtp" "github.com/stretchr/testify/require" "io" "net" "net/http" "strings" "testing" "time" ) func TestSmtpBackend_Multipart(t *testing.T) { email := `EHLO example.com MAIL FROM: phil@example.com RCPT TO: ntfy-mytopic@ntfy.sh DATA MIME-Version: 1.0 Date: Tue, 28 Dec 2021 00:30:10 +0100 Message-ID: Subject: and one more From: Phil To: ntfy-mytopic@ntfy.sh Content-Type: multipart/alternative; boundary="000000000000f3320b05d42915c9" --000000000000f3320b05d42915c9 Content-Type: text/plain; charset="UTF-8" what's up --000000000000f3320b05d42915c9 Content-Type: text/html; charset="UTF-8"
what's up

--000000000000f3320b05d42915c9-- . ` s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { require.Equal(t, "/mytopic", r.URL.Path) require.Equal(t, "and one more", r.Header.Get("Title")) require.Equal(t, "what's up", readAll(t, r.Body)) }) defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued") } func TestSmtpBackend_MultipartNoBody(t *testing.T) { email := `EHLO example.com MAIL FROM: phil@example.com RCPT TO: ntfy-emailtest@ntfy.sh DATA 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-- . ` s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { require.Equal(t, "/emailtest", r.URL.Path) require.Equal(t, "", r.Header.Get("Title")) // We flipped message and body require.Equal(t, "This email has a subject but no body", readAll(t, r.Body)) }) defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued") } func TestSmtpBackend_Plaintext(t *testing.T) { email := `EHLO example.com MAIL FROM: phil@example.com RCPT TO: mytopic@ntfy.sh DATA Date: Tue, 28 Dec 2021 00:30:10 +0100 Message-ID: Subject: and one more From: Phil To: mytopic@ntfy.sh Content-Type: text/plain; charset="UTF-8" what's up . ` s, c, conf, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { require.Equal(t, "/mytopic", r.URL.Path) require.Equal(t, "and one more", r.Header.Get("Title")) require.Equal(t, "what's up", readAll(t, r.Body)) }) conf.SMTPServerAddrPrefix = "" defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued") } func TestSmtpBackend_Plaintext_No_ContentType(t *testing.T) { email := `EHLO example.com MAIL FROM: phil@example.com RCPT TO: mytopic@ntfy.sh DATA Subject: Very short mail what's up . ` s, c, conf, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { require.Equal(t, "/mytopic", r.URL.Path) require.Equal(t, "Very short mail", r.Header.Get("Title")) require.Equal(t, "what's up", readAll(t, r.Body)) }) conf.SMTPServerAddrPrefix = "" defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued") } func TestSmtpBackend_Plaintext_EncodedSubject(t *testing.T) { email := `EHLO example.com MAIL FROM: phil@example.com RCPT TO: ntfy-mytopic@ntfy.sh DATA Date: Tue, 28 Dec 2021 00:30:10 +0100 Subject: =?UTF-8?B?VGhyZWUgc2FudGFzIPCfjoXwn46F8J+OhQ==?= From: Phil To: ntfy-mytopic@ntfy.sh Content-Type: text/plain; charset="UTF-8" what's up . ` s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { require.Equal(t, "Three santas 🎅🎅🎅", r.Header.Get("Title")) }) defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued") } func TestSmtpBackend_Plaintext_TooLongTruncate(t *testing.T) { email := `EHLO example.com MAIL FROM: phil@example.com RCPT TO: mytopic@ntfy.sh DATA Date: Tue, 28 Dec 2021 00:30:10 +0100 Message-ID: Subject: and one more From: Phil To: mytopic@ntfy.sh Content-Type: text/plain; charset="UTF-8" you know this is a string. it's a long string. it's supposed to be longer than the max message length which is 4096 bytes, it used to be 512 bytes, but I increased that for the UnifiedPush support the 512 bytes was a little short, some people said but it kinda makes sense when you look at what it looks like one a phone heck this wasn't even half of it so far. so i'm gonna fill the rest of this with AAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp and with BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB that should do it . ` s, c, conf, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { expected := `you know this is a string. it's a long string. it's supposed to be longer than the max message length which is 4096 bytes, it used to be 512 bytes, but I increased that for the UnifiedPush support the 512 bytes was a little short, some people said but it kinda makes sense when you look at what it looks like one a phone heck this wasn't even half of it so far. so i'm gonna fill the rest of this with AAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp and with BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBB` require.Equal(t, 4096, len(expected)) // Sanity check require.Equal(t, expected, readAll(t, r.Body)) }) defer s.Close() defer c.Close() conf.SMTPServerAddrPrefix = "" writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued") } func TestSmtpBackend_Plaintext_QuotedPrintable(t *testing.T) { email := `EHLO example.com MAIL FROM: phil@example.com RCPT TO: mytopic@ntfy.sh DATA Date: Tue, 28 Dec 2021 00:30:10 +0100 Message-ID: Subject: and one more From: Phil To: mytopic@ntfy.sh Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable what's =C3=A0&=C3=A9"'(-=C3=A8_=C3=A7=C3=A0) =3D=3D=3D=3D=3D up . ` s, c, conf, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { require.Equal(t, "/mytopic", r.URL.Path) require.Equal(t, "and one more", r.Header.Get("Title")) require.Equal(t, `what's à&é"'(-è_çà) ===== up`, readAll(t, r.Body)) }) conf.SMTPServerAddrPrefix = "" defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued") } func TestSmtpBackend_Unsupported(t *testing.T) { email := `EHLO example.com MAIL FROM: phil@example.com RCPT TO: ntfy-mytopic@ntfy.sh DATA Date: Tue, 28 Dec 2021 00:30:10 +0100 Message-ID: Subject: and one more From: Phil To: mytopic@ntfy.sh Content-Type: text/SOMETHINGELSE what's up . ` s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { t.Fatal("This should not be called") }) defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "554 5.0.0 Error: transaction failed, blame it on the weather: unsupported content type") } func TestSmtpBackend_InvalidAddress(t *testing.T) { email := `EHLO example.com MAIL FROM: phil@example.com RCPT TO: unsupported@ntfy.sh DATA Date: Tue, 28 Dec 2021 00:30:10 +0100 Subject: and one more From: Phil To: mytopic@ntfy.sh Content-Type: text/plain what's up . ` s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { t.Fatal("This should not be called") }) defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "451 4.0.0 invalid address") } func TestSmtpBackend_Base64Body(t *testing.T) { email := `EHLO example.com MAIL FROM: test@mydomain.me RCPT TO: ntfy-mytopic@ntfy.sh DATA Content-Type: multipart/mixed; boundary="===============2138658284696597373==" MIME-Version: 1.0 Subject: TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local From: =?utf-8?q?Robbie?= To: test@mydomain.me Date: Thu, 16 Feb 2023 01:04:00 -0000 Message-ID: This is a multi-part message in MIME format. --===============2138658284696597373== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 VGhpcyBpcyBhIHRlc3QgbWVzc2FnZSBmcm9tIFRydWVOQVMgQ09SRS4= --===============2138658284696597373== Content-Type: text/html; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv L0VOIj4KClRoaXMgaXMgYSB0ZXN0IG1lc3NhZ2UgZnJvbSBUcnVlTkFTIENPUkUuCg== --===============2138658284696597373==-- . ` s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { require.Equal(t, "/mytopic", r.URL.Path) require.Equal(t, "TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local", r.Header.Get("Title")) require.Equal(t, "This is a test message from TrueNAS CORE.", readAll(t, r.Body)) }) defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued") } func TestSmtpBackend_MultipartQuotedPrintable(t *testing.T) { email := `EHLO example.com MAIL FROM: phil@example.com RCPT TO: ntfy-mytopic@ntfy.sh DATA MIME-Version: 1.0 Date: Tue, 28 Dec 2021 00:30:10 +0100 Message-ID: Subject: and one more From: Phil To: ntfy-mytopic@ntfy.sh Content-Type: multipart/alternative; boundary="000000000000f3320b05d42915c9" --000000000000f3320b05d42915c9 Content-Type: text/html; charset="UTF-8" html, ignore me --000000000000f3320b05d42915c9 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable what's =C3=A0&=C3=A9"'(-=C3=A8_=C3=A7=C3=A0) =3D=3D=3D=3D=3D up --000000000000f3320b05d42915c9-- . ` s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { require.Equal(t, "/mytopic", r.URL.Path) require.Equal(t, "and one more", r.Header.Get("Title")) require.Equal(t, `what's à&é"'(-è_çà) ===== up`, readAll(t, r.Body)) }) defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued") } func TestSmtpBackend_NestedMultipartBase64(t *testing.T) { email := `EHLO example.com MAIL FROM: test@mydomain.me RCPT TO: ntfy-mytopic@ntfy.sh DATA Content-Type: multipart/mixed; boundary="===============2138658284696597373==" MIME-Version: 1.0 Subject: TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local From: =?utf-8?q?Robbie?= To: test@mydomain.me Date: Thu, 16 Feb 2023 01:04:00 -0000 Message-ID: This is a multi-part message in MIME format. --===============2138658284696597373== Content-Type: multipart/alternative; boundary="===============2233989480071754745==" MIME-Version: 1.0 --===============2233989480071754745== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 VGhpcyBpcyBhIHRlc3QgbWVzc2FnZSBmcm9tIFRydWVOQVMgQ09SRS4= --===============2233989480071754745== Content-Type: text/html; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv L0VOIj4KClRoaXMgaXMgYSB0ZXN0IG1lc3NhZ2UgZnJvbSBUcnVlTkFTIENPUkUuCg== --===============2233989480071754745==-- --===============2138658284696597373==-- . ` s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { require.Equal(t, "/mytopic", r.URL.Path) require.Equal(t, "TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local", r.Header.Get("Title")) require.Equal(t, "This is a test message from TrueNAS CORE.", readAll(t, r.Body)) }) defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued") } func TestSmtpBackend_NestedMultipartTooDeep(t *testing.T) { email := `EHLO example.com MAIL FROM: test@mydomain.me RCPT TO: ntfy-mytopic@ntfy.sh DATA Content-Type: multipart/mixed; boundary="===============1==" MIME-Version: 1.0 Subject: TrueNAS truenas.local: TrueNAS Test Message hostname: truenas.local From: =?utf-8?q?Robbie?= To: test@mydomain.me Date: Thu, 16 Feb 2023 01:04:00 -0000 Message-ID: This is a multi-part message in MIME format. --===============1== Content-Type: multipart/alternative; boundary="===============2==" MIME-Version: 1.0 --===============2== Content-Type: multipart/alternative; boundary="===============3==" MIME-Version: 1.0 --===============3== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 VGhpcyBpcyBhIHRlc3QgbWVzc2FnZSBmcm9tIFRydWVOQVMgQ09SRS4= --===============3== Content-Type: text/html; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 PCFET0NUWVBFIEhUTUwgUFVCTElDICItLy9XM0MvL0RURCBIVE1MIDQuMCBUcmFuc2l0aW9uYWwv L0VOIj4KClRoaXMgaXMgYSB0ZXN0IG1lc3NhZ2UgZnJvbSBUcnVlTkFTIENPUkUuCg== --===============3==-- --===============2==-- --===============1==-- . ` s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { t.Fatal("This should not be called") }) defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "554 5.0.0 Error: transaction failed, blame it on the weather: multipart message nested too deep") } func TestSmtpBackend_HTMLEmail(t *testing.T) { email := `EHLO example.com MAIL FROM: test@mydomain.me RCPT TO: ntfy-mytopic@ntfy.sh DATA Message-Id: <51610934ss4.mmailer@fritz.box> From: To: , Date: Thu, 30 Mar 2023 02:56:53 +0000 Subject: A HTML email Mime-Version: 1.0 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: quoted-printable <=21DOCTYPE html> Alerttitle
headertext of table
" Very important information about a change in your home automation setup Now the light is on

If you don't want to receive this message anymore, stop the push services in your FRITZ=21Box=2E
Here you can see the active push services: "System > Push Service"=2E
This mail has ben sent by your FRITZ=21Box automatically=2E
. ` s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { require.Equal(t, "/mytopic", r.URL.Path) require.Equal(t, "A HTML email", r.Header.Get("Title")) expected := `headertext of table " Very important information about a change in your home automation setup Now the light is on If you don't want to receive this message anymore, stop the push services in your FRITZ!Box . Here you can see the active push services: "System > Push Service". This mail has ben sent by your FRITZ!Box automatically.` require.Equal(t, expected, readAll(t, r.Body)) }) defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued") } const spamEmail = ` EHLO example.com MAIL FROM: test@mydomain.me RCPT TO: ntfy-mytopic@ntfy.sh DATA Delivered-To: somebody@gmail.com Received: by 2002:a05:651c:1248:b0:2bf:c263:285 with SMTP id h8csp1096496ljh; Mon, 30 Oct 2023 06:23:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFsB3WqbwbeefbeefbeefbeefbeefiXRNDHnIy2xBeaYHZCM3EC8DfPv55qDtgq9djTeBCF X-Received: by 2002:a05:6808:147:b0:3af:66e5:5d3c with SMTP id h7-20020a056808014700b003af66e55d3cmr11662458oie.26.1698672188132; Mon, 30 Oct 2023 06:23:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698672188; cv=none; d=google.com; s=arc-20160816; b=XM96KvnTbr4h6bqrTPTuuDNXmFCr9Be/HvVhu+UsSQjP9RxPk0wDTPUPZ/HWIJs52y beeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef BUmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe-post:list-unsubscribe:mime-version:subject:to :reply-to:from:date:message-id:dkim-signature:dkim-signature; bh=BERwBIp6fBgrZePFKQjyNMmgPkcnq1Zy1jPO8M0T4Ok=; fh=+kTCcNpX22TOI/SVSLygnrDqWeUt4zW7QKiv0TOVSGs=; b=lyIBRuOxPOTY2s36OqP7M7awlBKd4t5PX9mJOEJB0eTnTZqML+cplrXUIg2ZTlAAi9 beeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeef tgVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@spamspam.com header.s=2020294246 header.b=G8y6xmtK; dkim=pass header.i=@auth.ccsend.com header.s=1000073432 header.b=ht8IksVK; spf=pass (google.com: domain of aigxeklyirlg+dvwkrmsgua==_1133104752381_suqcukvbeeynm/owplvdba==@in.constantcontact.com designates 208.75.123.226 as permitted sender) smtp.mailfrom="AigXeKlyIRLG+DvWkRMsGUA==_1133104752381_sUQcUKVBEeynm/oWPlvDBA==@in.constantcontact.com"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=spamspam.com Return-Path: Received: from ccm30.constantcontact.com (ccm30.constantcontact.com. [208.75.123.226]) by mx.google.com with ESMTPS id h2-20020a05620a21c200b0076eeed38118si5450962qka.131.2023.10.30.06.23.07 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Oct 2023 06:23:08 -0700 (PDT) Received-SPF: pass (google.com: domain of aigxeklyirlg+dvwkrmsgua==_1133104752381_suqcukvbeeynm/owplvdba==@in.constantcontact.com designates 208.75.123.226 as permitted sender) client-ip=208.75.123.226; Authentication-Results: mx.google.com; dkim=pass header.i=@spamspam.com header.s=2020294246 header.b=G8y6xmtK; dkim=pass header.i=@auth.ccsend.com header.s=1000073432 header.b=ht8IksVK; spf=pass (google.com: domain of aigxeklyirlg+dvwkrmsgua==_1133104752381_suqcukvbeeynm/owplvdba==@in.constantcontact.com designates 208.75.123.226 as permitted sender) smtp.mailfrom="AigXeKlyIRLG+DvWkRMsGUA==_1133104752381_sUQcUKVBEeynm/oWPlvDBA==@in.constantcontact.com"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=spamspam.com Return-Path: Received: from [10.252.0.3] ([10.252.0.3:53254] helo=p2-jbemailsyndicator12.ctct.net) by 10.249.225.20 (envelope-from ) (ecelerity 4.3.1.999 r(:)) with ESMTP id A4/82-60517-B3EAF356; Mon, 30 Oct 2023 09:23:07 -0400 DKIM-Signature: v=1; q=dns/txt; a=rsa-sha256; c=relaxed/relaxed; s=2020294246; d=spamspam.com; h=date:mime-version:subject:X-Feedback-ID:X-250ok-CID:message-id:from:reply-to:list-unsubscribe:list-unsubscribe-post:to; bh=BERwBIp6fBgrZePFKQjyNMmgPkcnq1Zy1jPO8M0T4Ok=; b=G8y6xmtKv8asfEXA9o8dP+6foQjclo6j5sFREYVIJBbj5YJ5tqoiv5B04/qoRkoTBFDhmjt+BUua7AqDgPSnwbP2iPSA4fTJehnHhut1PyVUp/9vqSYlhxQehfdhma8tPg8ArKfYIKmfKJwKRaQBU0JHCaB1m+5LNQQX3UjkxAg= DKIM-Signature: v=1; q=dns/txt; a=rsa-sha256; c=relaxed/relaxed; s=1000073432; d=auth.ccsend.com; h=date:mime-version:subject:X-Feedback-ID:X-250ok-CID:message-id:from:reply-to:list-unsubscribe:list-unsubscribe-post:to; bh=BERwBIp6fBgrZePFKQjyNMmgPkcnq1Zy1jPO8M0T4Ok=; b=ht8IksVKYY/Kb3dUERWoeW4eVdYjKL6F4PEoIZOhfFXor6XAIbPnd3A/CPmbmoqFZjnKh5OdcUy1N5qEoj8w1Q3TmN8/ySQkqrlrmSDSZIHZMY7Qp9/TJrqUe4RMFOO1KKIN6Y0vGP1+dWe98msMAHwvi2qMjG9aEKLfFr2JUTQ= Message-ID: <1140728754828.1133104752381.1941549819.0.260913JL.2002@synd.ccsend.com> Date: Mon, 30 Oct 2023 09:23:07 -0400 (EDT) From: spamspam Loan Servicing Reply-To: marklake@spamspam.com To: somebody@gmail.com Subject: Buying a home? You deserve the confidence of Pre-Approval MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_75055660_144854819.1698672187348" List-Unsubscribe: List-Unsubscribe-Post: List-Unsubscribe=One-Click X-Campaign-Activity-ID: 8a05de2a-5c88-44b1-be0e-f5a444cb0650 X-250ok-CID: 8a05de2a-5c88-44b1-be0e-f5a444cb0650 X-Channel-ID: b1441c50-a541-11ec-a79b-fa163e5bc304 X-Return-Path-Hint: AbeefbeefbeefbeefbeefUA==_1133104752381_sUQcUKVBEeynm/oWPlvDBA==@in.constantcontact.com X-Roving-Campaignid: 1140728754811 X-Roving-Id: 1133104752381.1111111111 X-Feedback-ID: b1441c50-a541-11ec-beef-beefbeefbeefbeef5de2a-5c88-44b1-be0e-f5a444cb0650:1133104752381:CTCT X-CTCT-ID: b13a9586-a541-11ec-beef-beefbeefbeef ------=_Part_75055660_144854819.1698672187348 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable When you're buying a home, Pre-Approval gives you confidence you're in the = right price range and shows sellers you mean business. xxxxxxxxx SELLING or= BUYING? Call: 844-590-2275 Get Your Homebuying PRE-APPROVAL IN 24-HOURS* G= et Pre-Approved When you're buying a home, Pre-Approval gives you confidenc= e you're in the right price range and shows sellers you mean business. xxx= xxxxxxGet Pre-Approved today! Click or Call to Get Pre-Approved 844-590-227= 5 Get Pre-Approved nmlsconsumeraccess.org/ *The 24 hour timeframe is for mo= st approvals, however if additional information is needed or a request is o= n a holiday, the time for preapproval may be greater than 24 hours. This em= ail is for informational purposes only and is not an offer, loan approval o= r loan commitment. Mortgage rates are subject to change without notice. Som= e terms and restrictions may apply to certain loan programs. Refinancing ex= isting loans may result in total finance charges being higher over the life= of the loan, reduction in payments may partially reflect a longer loan ter= m. This information is provided as guidance and illustrative purposes only = and does not constitute legal or financial advice. We are not liable or bou= nd legally for any answers provided to any user for our process or position= on an issue. This information may change from time to time and at any time= without notification. The most current information will be updated periodi= cally and posted in the online forum. spamspam Loan Servicing, LLC. NMLS#39= 1521. nmlsconsumeraccess.org. You are receiving this information as a curre= nt loan customer with spamspam Loan Servicing, LLC. Not licensed for lendin= g activities in any of the U.S. territories. Not authorized to originate lo= ans in the State of New York. Licensed by the Dept. of Financial Protection= and Innovation under the California Residential Mortgage .Lending Act #413= 1216. This email was sent to somebody@gmail.com Version 103023PCHPrAp= 9 xxxxxxxxx spamspam Loan Servicing | 4425 Ponce de Leon Blvd 5-251, Coral = Gables, FL 33146-1837 Unsubscribe somebody@gmail.com Update Profile |= Our Privacy Policy | Constant Contact Data Notice Sent by marklake@spamspa= m.com ------=_Part_75055660_144854819.1698672187348 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
When you're buying a home, Pre-Approval = gives you confidence you're in the right price range and shows sellers= you mean business.
3D""
=
3D""
=
3D""

SELLING or BUYING?

Call: 844-590-2275

Get Your Homebuying

PRE-APPROVAL IN 24-HOURS*

= 3D"" =
Get Pre-Approved
=


When you're buying= a home, Pre-Approval gives you confidence you're in the right price range = and shows sellers you mean business.

Get Pre-Ap= proved today!


Click or Call to Get Pre-Approved

844-590-2275=

Get Pre-Approved
3D""
<= tr>


nmlsconsumeraccess.org/

*The 24 hour timeframe is for= most approvals, however if additional information is needed or a request i= s on a holiday, the time for preapproval may be greater than 24 hours.

This email is for informational purposes only and is not an offer,= loan approval or loan commitment. Mortgage rates are subject to change wit= hout notice. Some terms and restrictions may apply to certain loan programs= . Refinancing existing loans may result in total finance charges being high= er over the life of the loan, reduction in payments may partially reflect a= longer loan term. This information is provided as guidance and illustrativ= e purposes only and does not constitute legal or financial advice. We are n= ot liable or bound legally for any answers provided to any user for our pro= cess or position on an issue. This information may change from time to time= and at any time without notification. The most current information will be= updated periodically and posted in the online forum.

spamspam Loan Servicing, LLC. NMLS#391521. nmlsconsumeraccess.org.= You are receiving this information as a current loan customer with spamspa= m Loan Servicing, LLC. Not licensed for lending activities in any of the U.= S. territories. Not authorized to originate loans in the State of New York.= Licensed by the Dept. of Financial Protection and Innovation under the Cal= ifornia Residential Mortgage .Lending Act #4131216.


This email was sent to somebody@gmail.com

Version 103023PCHPrAp9



=
3D""
=
=
spamspam Loan Servicing | 4425 Ponce de= Leon Blvd 5-251, Coral Gables, FL 33146-1837
Uns= ubscribe somebody@gmail.com
Upd= ate Profile | Our Privacy Policy | Constant Contact Data Noti= ce
Sent by marklake@spamspam.com
=
------=_Part_75055660_144854819.1698672187348-- . ` func TestSmtpBackend_Spam_Text(t *testing.T) { email := spamEmail s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { require.Equal(t, "/mytopic", r.URL.Path) require.Equal(t, "Buying a home? You deserve the confidence of Pre-Approval", r.Header.Get("Title")) actual := readAll(t, r.Body) expected := "When you're buying a home, Pre-Approval gives you confidence you're in the right price range and shows sellers you mean business. xxxxxxxxx SELLING or BUYING? Call: 844-590-2275 Get Your Homebuying PRE-APPROVAL IN 24-HOURS* Get Pre-Approved When you're buying a home, Pre-Approval gives you confidence you're in the right price range and shows sellers you mean business. xxxxxxxxxGet Pre-Approved today! Click or Call to Get Pre-Approved 844-590-2275 Get Pre-Approved nmlsconsumeraccess.org/ *The 24 hour timeframe is for most approvals, however if additional information is needed or a request is on a holiday, the time for preapproval may be greater than 24 hours. This email is for informational purposes only and is not an offer, loan approval or loan commitment. Mortgage rates are subject to change without notice. Some terms and restrictions may apply to certain loan programs. Refinancing existing loans may result in total finance charges being higher over the life of the loan, reduction in payments may partially reflect a longer loan term. This information is provided as guidance and illustrative purposes only and does not constitute legal or financial advice. We are not liable or bound legally for any answers provided to any user for our process or position on an issue. This information may change from time to time and at any time without notification. The most current information will be updated periodically and posted in the online forum. spamspam Loan Servicing, LLC. NMLS#391521. nmlsconsumeraccess.org. You are receiving this information as a current loan customer with spamspam Loan Servicing, LLC. Not licensed for lending activities in any of the U.S. territories. Not authorized to originate loans in the State of New York. Licensed by the Dept. of Financial Protection and Innovation under the California Residential Mortgage .Lending Act #4131216. This email was sent to somebody@gmail.com Version 103023PCHPrAp9 xxxxxxxxx spamspam Loan Servicing | 4425 Ponce de Leon Blvd 5-251, Coral Gables, FL 33146-1837 Unsubscribe somebody@gmail.com Update Profile | Our Privacy Policy | Constant Contact Data Notice Sent by marklake@spamspam.com" require.Equal(t, expected, actual) }) defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued") } func TestSmtpBackend_Spam_HTML(t *testing.T) { email := strings.ReplaceAll(spamEmail, "text/plain", "text/not-plain-anymore") // We artificially force HTML parsing here s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { require.Equal(t, "/mytopic", r.URL.Path) require.Equal(t, "Buying a home? You deserve the confidence of Pre-Approval", r.Header.Get("Title")) actual := readAll(t, r.Body) expected := `When you're buying a home, Pre-Approval gives you confidence you're in the right price range and shows sellers you mean business. ` + "\u200a" + ` SELLING or BUYING? Call: 844-590-2275 Get Your Homebuying PRE-APPROVAL IN 24-HOURS * Get Pre-Approved When you're buying a home, Pre-Approval gives you confidence you're in the right price range and shows sellers you mean business. ` + "\ufeff" + `Get Pre-Approved today! Click or Call to Get Pre-Approved 844-590-2275 Get Pre-Approved nmlsconsumeraccess.org/ *The 24 hour timeframe is for most approvals, however if additional information is needed or a request is on a holiday, the time for preapproval may be greater than 24 hours. This email is for informational purposes only and is not an offer, loan approval or loan commitment. Mortgage rates are subject to change without notice. Some terms and restrictions may apply to certain loan programs Refinancing existing loans may result in total finance charges being higher over the life of the loan, reduction in payments may partially reflect a longer loan term. This information is provided as guidance and illustrative purposes only and does not constitute legal or financial advice. We are not liable or bound legally for any answers provided to any user for our process or position on an issue. This information may change from time to time and at any time without notification. The most current information will be updated periodically and posted in the online forum. spamspam Loan Servicing, LLC. NMLS#391521. nmlsconsumeraccess.org. You are receiving this information as a current loan customer with spamspam Loan Servicing, LLC. Not licensed for lending activities in any of the U.S. territories. Not authorized to originate loans in the State of New York. Licensed by the Dept. of Financial Protection and Innovation under the California Residential Mortgage .Lending Act #4131216. This email was sent to somebody@gmail.com Version 103023PCHPrAp9 ` + "\ufeff" + ` spamspam Loan Servicing | 4425 Ponce de Leon Blvd 5-251 , Coral Gables, FL 33146-1837 Unsubscribe somebody@gmail.com Update Profile | Our Privacy Policy | Constant Contact Data Notice Sent by marklake@spamspam.com` require.Equal(t, expected, actual) }) defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued") } func TestSmtpBackend_PlaintextWithToken(t *testing.T) { email := `EHLO example.com MAIL FROM: phil@example.com RCPT TO: ntfy-mytopic+tk_KLORUqSqvNRLpY11DfkHVbHu9NGG2@ntfy.sh DATA Subject: Very short mail what's up . ` s, c, _, scanner := newTestSMTPServer(t, func(w http.ResponseWriter, r *http.Request) { require.Equal(t, "/mytopic", r.URL.Path) require.Equal(t, "Very short mail", r.Header.Get("Title")) require.Equal(t, "Bearer tk_KLORUqSqvNRLpY11DfkHVbHu9NGG2", r.Header.Get("Authorization")) require.Equal(t, "what's up", readAll(t, r.Body)) }) defer s.Close() defer c.Close() writeAndReadUntilLine(t, email, c, scanner, "250 2.0.0 OK: queued") } type smtpHandlerFunc func(http.ResponseWriter, *http.Request) func newTestSMTPServer(t *testing.T, handler smtpHandlerFunc) (s *smtp.Server, c net.Conn, conf *Config, scanner *bufio.Scanner) { conf = newTestConfig(t) conf.SMTPServerListen = ":25" conf.SMTPServerDomain = "ntfy.sh" conf.SMTPServerAddrPrefix = "ntfy-" backend := newMailBackend(conf, handler) l, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) } s = smtp.NewServer(backend) s.Domain = conf.SMTPServerDomain s.AllowInsecureAuth = true go func() { require.Nil(t, s.Serve(l)) }() c, err = net.Dial("tcp", l.Addr().String()) if err != nil { t.Fatal(err) } scanner = bufio.NewScanner(c) return } func writeAndReadUntilLine(t *testing.T, email string, conn net.Conn, scanner *bufio.Scanner, expectedLine string) { _, err := io.WriteString(conn, email) require.Nil(t, err) readUntilLine(t, conn, scanner, expectedLine) } func readUntilLine(t *testing.T, conn net.Conn, scanner *bufio.Scanner, expectedLine string) { cancelChan := make(chan bool) go func() { select { case <-cancelChan: case <-time.After(3 * time.Second): conn.Close() t.Error("Failed waiting for expected output") } }() var output string for scanner.Scan() { text := scanner.Text() if strings.TrimSpace(text) == expectedLine { cancelChan <- true return } output += text + "\n" } t.Fatalf("Expected line '%s' not found in output:\n%s", expectedLine, output) }