tidy up the email formatting

This commit is contained in:
tsmethurst 2021-10-16 18:41:36 +02:00
commit 2eda7f4846
5 changed files with 20 additions and 13 deletions

View file

@ -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)
}

View file

@ -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.

View file

@ -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)
}

View file

@ -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")

View file

@ -40,8 +40,8 @@ func (suite *UtilTestSuite) TestTemplateConfirm() {
suite.NoError(err)
suite.Equal("<!DOCTYPE html>\n<html>\n </head>\n <body>\n <div>\n <h1>\n Hello test!\n </h1>\n </div>\n <div>\n <p>\n You are receiving this mail because you've requested an account on <a href=\"https://example.org\">Test Instance</a>.\n </p>\n <p>\n We just need to confirm that this is your email address. To confirm your email, <a href=\"https://example.org/confirm_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\">click here</a> or paste the following in your browser's address bar:\n </p>\n <p>\n <code>\n https://example.org/confirm_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\n </code>\n </p>\n </div>\n <div>\n <p>\n If you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of <a href=\"https://example.org\">Test Instance</a>.\n </p>\n </div>\n </body>\n</html>", 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<!DOCTYPE html>\n<html>\n </head>\n <body>\n <div>\n <h1>\n Hello test!\n </h1>\n </div>\n <div>\n <p>\n You are receiving this mail because you've requested an account on <a href=\"https://example.org\">Test Instance</a>.\n </p>\n <p>\n We just need to confirm that this is your email address. To confirm your email, <a href=\"https://example.org/confirm_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\">click here</a> or paste the following in your browser's address bar:\n </p>\n <p>\n <code>\n https://example.org/confirm_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\n </code>\n </p>\n </div>\n <div>\n <p>\n If you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of <a href=\"https://example.org\">Test Instance</a>.\n </p>\n </div>\n </body>\n</html>\r\n", string(message))
message := suite.sender.AssembleMessage("Subject: something", mailBody, "user@example.org")
suite.Equal("Subject: something\r\nFrom: GoToSocial <GoToSocial>\r\nMIME-version: 1.0;\nContent-Type: text/html;\r\n<!DOCTYPE html>\n<html>\n </head>\n <body>\n <div>\n <h1>\n Hello test!\n </h1>\n </div>\n <div>\n <p>\n You are receiving this mail because you've requested an account on <a href=\"https://example.org\">Test Instance</a>.\n </p>\n <p>\n We just need to confirm that this is your email address. To confirm your email, <a href=\"https://example.org/confirm_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\">click here</a> or paste the following in your browser's address bar:\n </p>\n <p>\n <code>\n https://example.org/confirm_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\n </code>\n </p>\n </div>\n <div>\n <p>\n If you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of <a href=\"https://example.org\">Test Instance</a>.\n </p>\n </div>\n </body>\n</html>\r\n", string(message))
}
func (suite *UtilTestSuite) TestTemplateReset() {
@ -55,8 +55,8 @@ func (suite *UtilTestSuite) TestTemplateReset() {
suite.NoError(err)
suite.Equal("<!DOCTYPE html>\n<html>\n </head>\n <body>\n <div>\n <h1>\n Hello test!\n </h1>\n </div>\n <div>\n <p>\n You are receiving this mail because a password reset has been requested for your account on <a href=\"https://example.org\">Test Instance</a>.\n </p>\n <p>\n To reset your password, <a href=\"https://example.org/reset_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\">click here</a> or paste the following in your browser's address bar:\n </p>\n <p>\n <code>\n https://example.org/reset_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\n </code>\n </p>\n </div>\n <div>\n <p>\n If you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of <a href=\"https://example.org\">Test Instance</a>.\n </p>\n </div>\n </body>\n</html>", 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<!DOCTYPE html>\n<html>\n </head>\n <body>\n <div>\n <h1>\n Hello test!\n </h1>\n </div>\n <div>\n <p>\n You are receiving this mail because a password reset has been requested for your account on <a href=\"https://example.org\">Test Instance</a>.\n </p>\n <p>\n To reset your password, <a href=\"https://example.org/reset_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\">click here</a> or paste the following in your browser's address bar:\n </p>\n <p>\n <code>\n https://example.org/reset_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\n </code>\n </p>\n </div>\n <div>\n <p>\n If you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of <a href=\"https://example.org\">Test Instance</a>.\n </p>\n </div>\n </body>\n</html>\r\n", string(message))
message := suite.sender.AssembleMessage("Subject: something", mailBody, "user@example.org")
suite.Equal("Subject: something\r\nFrom: GoToSocial <GoToSocial>\r\nMIME-version: 1.0;\nContent-Type: text/html;\r\n<!DOCTYPE html>\n<html>\n </head>\n <body>\n <div>\n <h1>\n Hello test!\n </h1>\n </div>\n <div>\n <p>\n You are receiving this mail because a password reset has been requested for your account on <a href=\"https://example.org\">Test Instance</a>.\n </p>\n <p>\n To reset your password, <a href=\"https://example.org/reset_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\">click here</a> or paste the following in your browser's address bar:\n </p>\n <p>\n <code>\n https://example.org/reset_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa\n </code>\n </p>\n </div>\n <div>\n <p>\n If you believe you've been sent this email in error, feel free to ignore it, or contact the administrator of <a href=\"https://example.org\">Test Instance</a>.\n </p>\n </div>\n </body>\n</html>\r\n", string(message))
}
func TestUtilTestSuite(t *testing.T) {