diff --git a/internal/email/confirm.go b/internal/email/confirm.go index 06b5bc1c6..d6b3a2407 100644 --- a/internal/email/confirm.go +++ b/internal/email/confirm.go @@ -33,8 +33,7 @@ func (s *sender) SendConfirmEmail(toAddress string, data ConfirmData) error { return err } - msg := AssembleMessage(confirmSubject, confirmBody) - + msg := s.AssembleMessage(confirmSubject, confirmBody, toAddress) return smtp.SendMail(s.hostAddress, s.auth, s.from, []string{toAddress}, msg) } diff --git a/internal/email/email.go b/internal/email/email.go index 56e5635e2..69015e252 100644 --- a/internal/email/email.go +++ b/internal/email/email.go @@ -39,6 +39,9 @@ type Sender interface { // ExecuteTemplate returns templated HTML using the given templateName and data. Mostly you won't need to call this, // and can just call one of the 'Send' functions instead (which calls this under the hood anyway). ExecuteTemplate(templateName string, data interface{}) (string, error) + // AssembleMessage concacenates the mailSubject, the mime header, mailTo, mailFrom and the mailBody in + // an appropriate format for sending via net/smtp. Mostly you won't need to call this, but it's provided just in case. + AssembleMessage(mailSubject string, mailBody string, mailTo string) []byte } // NewSender returns a new email Sender interface with the given configuration, or an error if something goes wrong. diff --git a/internal/email/reset.go b/internal/email/reset.go index 473223867..a0cd377f6 100644 --- a/internal/email/reset.go +++ b/internal/email/reset.go @@ -33,8 +33,7 @@ func (s *sender) SendResetEmail(toAddress string, data ResetData) error { return err } - msg := AssembleMessage(resetSubject, resetBody) - + msg := s.AssembleMessage(resetSubject, resetBody, toAddress) return smtp.SendMail(s.hostAddress, s.auth, s.from, []string{toAddress}, msg) } diff --git a/internal/email/util.go b/internal/email/util.go index 460a27f9e..87933b130 100644 --- a/internal/email/util.go +++ b/internal/email/util.go @@ -18,11 +18,14 @@ package email -import "bytes" +import ( + "bytes" + "fmt" +) const ( mime = `MIME-version: 1.0; -Content-Type: text/plain; charset="UTF-8";` +Content-Type: text/html;` ) func (s *sender) ExecuteTemplate(templateName string, data interface{}) (string, error) { @@ -33,11 +36,14 @@ func (s *sender) ExecuteTemplate(templateName string, data interface{}) (string, return buf.String(), nil } -// AssembleMessage concacenates the mailSubject, the mime header, and the mailBody in -// an appropriate format for sending via net/smtp. -func AssembleMessage(mailSubject string, mailBody string) []byte { +func (s *sender) AssembleMessage(mailSubject string, mailBody string, mailTo string) []byte { + from := fmt.Sprintf("From: GoToSocial <%s>", s.from) + to := fmt.Sprintf("To: %s", mailTo) + msg := []byte( mailSubject + "\r\n" + + from + "\r\n" + + to + "\r\n" + mime + "\r\n" + mailBody + "\r\n") diff --git a/internal/email/util_test.go b/internal/email/util_test.go index 820d4073e..aacb02655 100644 --- a/internal/email/util_test.go +++ b/internal/email/util_test.go @@ -40,8 +40,8 @@ func (suite *UtilTestSuite) TestTemplateConfirm() { suite.NoError(err) suite.Equal("\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", mailBody) - message := email.AssembleMessage("Subject: something", mailBody) - suite.Equal("Subject: something\r\nMIME-version: 1.0;\nContent-Type: text/plain; charset=\"UTF-8\";\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", string(message)) + message := suite.sender.AssembleMessage("Subject: something", mailBody, "user@example.org") + suite.Equal("Subject: something\r\nFrom: GoToSocial \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", string(message)) } func (suite *UtilTestSuite) TestTemplateReset() { @@ -55,8 +55,8 @@ func (suite *UtilTestSuite) TestTemplateReset() { suite.NoError(err) suite.Equal("\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", mailBody) - message := email.AssembleMessage("Subject: something", mailBody) - suite.Equal("Subject: something\r\nMIME-version: 1.0;\nContent-Type: text/plain; charset=\"UTF-8\";\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", string(message)) + message := suite.sender.AssembleMessage("Subject: something", mailBody, "user@example.org") + suite.Equal("Subject: something\r\nFrom: GoToSocial \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", string(message)) } func TestUtilTestSuite(t *testing.T) {