From d3a80b69f0fb90efe9cb8690a48bc5364e12f4f6 Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Sun, 17 Oct 2021 17:45:07 +0200 Subject: [PATCH] minify email html before sending --- internal/email/confirm.go | 8 +++++++- internal/email/{sender_test.go => email_test.go} | 0 internal/email/noopsender.go | 14 ++++++++++++-- internal/email/reset.go | 8 +++++++- internal/email/util_test.go | 4 ++-- internal/text/common.go | 2 +- internal/text/minify.go | 4 ++-- 7 files changed, 31 insertions(+), 9 deletions(-) rename internal/email/{sender_test.go => email_test.go} (100%) diff --git a/internal/email/confirm.go b/internal/email/confirm.go index 78f661fe1..363cdb8b0 100644 --- a/internal/email/confirm.go +++ b/internal/email/confirm.go @@ -21,6 +21,8 @@ package email import ( "bytes" "net/smtp" + + "github.com/superseriousbusiness/gotosocial/internal/text" ) const ( @@ -33,7 +35,11 @@ func (s *sender) SendConfirmEmail(toAddress string, data ConfirmData) error { if err := s.template.ExecuteTemplate(buf, confirmTemplate, data); err != nil { return err } - confirmBody := buf.String() + + confirmBody, err := text.MinifyHTML(buf.String()) + if err != nil { + return err + } msg := assembleMessage(confirmSubject, confirmBody, toAddress, s.from) return smtp.SendMail(s.hostAddress, s.auth, s.from, []string{toAddress}, msg) diff --git a/internal/email/sender_test.go b/internal/email/email_test.go similarity index 100% rename from internal/email/sender_test.go rename to internal/email/email_test.go diff --git a/internal/email/noopsender.go b/internal/email/noopsender.go index a572dcc73..d8a7b4db7 100644 --- a/internal/email/noopsender.go +++ b/internal/email/noopsender.go @@ -21,6 +21,8 @@ package email import ( "bytes" "html/template" + + "github.com/superseriousbusiness/gotosocial/internal/text" ) // NewNoopSender returns a no-op email sender that will just execute the given sendCallback @@ -50,7 +52,11 @@ func (s *noopSender) SendConfirmEmail(toAddress string, data ConfirmData) error if err := s.template.ExecuteTemplate(buf, confirmTemplate, data); err != nil { return err } - confirmBody := buf.String() + + confirmBody, err := text.MinifyHTML(buf.String()) + if err != nil { + return err + } msg := assembleMessage(confirmSubject, confirmBody, toAddress, "test@example.org") s.sendCallback(toAddress, string(msg)) @@ -64,7 +70,11 @@ func (s *noopSender) SendResetEmail(toAddress string, data ResetData) error { if err := s.template.ExecuteTemplate(buf, resetTemplate, data); err != nil { return err } - resetBody := buf.String() + + resetBody, err := text.MinifyHTML(buf.String()) + if err != nil { + return err + } msg := assembleMessage(resetSubject, resetBody, toAddress, "test@example.org") s.sendCallback(toAddress, string(msg)) diff --git a/internal/email/reset.go b/internal/email/reset.go index 786777f1b..6fb72084f 100644 --- a/internal/email/reset.go +++ b/internal/email/reset.go @@ -21,6 +21,8 @@ package email import ( "bytes" "net/smtp" + + "github.com/superseriousbusiness/gotosocial/internal/text" ) const ( @@ -33,7 +35,11 @@ func (s *sender) SendResetEmail(toAddress string, data ResetData) error { if err := s.template.ExecuteTemplate(buf, resetTemplate, data); err != nil { return err } - resetBody := buf.String() + + resetBody, err := text.MinifyHTML(buf.String()) + if err != nil { + return err + } msg := assembleMessage(resetSubject, resetBody, toAddress, s.from) return smtp.SendMail(s.hostAddress, s.auth, s.from, []string{toAddress}, msg) diff --git a/internal/email/util_test.go b/internal/email/util_test.go index 201bcd92d..8a16d093d 100644 --- a/internal/email/util_test.go +++ b/internal/email/util_test.go @@ -39,7 +39,7 @@ func (suite *UtilTestSuite) TestTemplateConfirm() { suite.sender.SendConfirmEmail("user@example.org", confirmData) suite.Len(suite.sentEmails, 1) - suite.Equal("Subject: GoToSocial Email Confirmation\r\nFrom: GoToSocial \r\nTo: user@example.org\r\nMIME-version: 1.0;\nContent-Type: text/html;\r\n\n\n \n \n
\n

\n Hello test!\n

\n
\n
\n

\n You are receiving this mail because you've requested an account on Test Instance.\n

\n

\n We just need to confirm that this is your email address. To confirm your email, click here or paste the following in your browser's address bar:\n

\n

\n \n https://example.org/confirm_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\n \n

\n
\n
\n

\n If you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of Test Instance.\n

\n
\n \n\r\n", suite.sentEmails["user@example.org"]) + suite.Equal("Subject: GoToSocial Email Confirmation\r\nFrom: GoToSocial \r\nTo: user@example.org\r\nMIME-version: 1.0;\nContent-Type: text/html;\r\n

Hello test!

You are receiving this mail because you've requested an account on Test Instance.

We just need to confirm that this is your email address. To confirm your email, click here or paste the following in your browser's address bar:

https://example.org/confirm_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa

If you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of Test Instance.

\r\n", suite.sentEmails["user@example.org"]) } func (suite *UtilTestSuite) TestTemplateReset() { @@ -52,7 +52,7 @@ func (suite *UtilTestSuite) TestTemplateReset() { suite.sender.SendResetEmail("user@example.org", resetData) suite.Len(suite.sentEmails, 1) - suite.Equal("Subject: GoToSocial Password Reset\r\nFrom: GoToSocial \r\nTo: user@example.org\r\nMIME-version: 1.0;\nContent-Type: text/html;\r\n\n\n \n \n
\n

\n Hello test!\n

\n
\n
\n

\n You are receiving this mail because a password reset has been requested for your account on Test Instance.\n

\n

\n To reset your password, click here or paste the following in your browser's address bar:\n

\n

\n \n https://example.org/reset_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\n \n

\n
\n
\n

\n If you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of Test Instance.\n

\n
\n \n\r\n", suite.sentEmails["user@example.org"]) + suite.Equal("Subject: GoToSocial Password Reset\r\nFrom: GoToSocial \r\nTo: user@example.org\r\nMIME-version: 1.0;\nContent-Type: text/html;\r\n

Hello test!

You are receiving this mail because a password reset has been requested for your account on Test Instance.

To reset your password, click here or paste the following in your browser's address bar:

https://example.org/reset_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa

If you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of Test Instance.

\r\n", suite.sentEmails["user@example.org"]) } func TestUtilTestSuite(t *testing.T) { diff --git a/internal/text/common.go b/internal/text/common.go index 1c7d52905..0173b1d01 100644 --- a/internal/text/common.go +++ b/internal/text/common.go @@ -52,7 +52,7 @@ func postformat(in string) string { s = html.UnescapeString(s) // 3. minify html to remove any trailing newlines, spaces, unnecessary elements, etc etc - mini, err := minifyHTML(s) + mini, err := MinifyHTML(s) if err != nil { // if the minify failed, just return what we have return s diff --git a/internal/text/minify.go b/internal/text/minify.go index c6d7b9bc1..1c7c8ce24 100644 --- a/internal/text/minify.go +++ b/internal/text/minify.go @@ -25,8 +25,8 @@ import ( var m *minify.M -// minifyHTML runs html through a minifier, reducing it in size. -func minifyHTML(in string) (string, error) { +// MinifyHTML runs html through a minifier, reducing it in size. +func MinifyHTML(in string) (string, error) { if m == nil { m = minify.New() m.Add("text/html", &html.Minifier{