flesh out the email sender interface

This commit is contained in:
tsmethurst 2021-10-16 17:47:13 +02:00
commit c868cc3e65
6 changed files with 307 additions and 1 deletions

52
internal/email/confirm.go Normal file
View file

@ -0,0 +1,52 @@
/*
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package email
import (
"net/smtp"
)
const (
confirmTemplate = "email_confirm.tmpl"
confirmSubject = "Subject: GoToSocial Email Confirmation"
)
func (s *sender) SendConfirmEmail(toAddress string, data ConfirmData) error {
confirmBody, err := s.ExecuteTemplate(confirmTemplate, data)
if err != nil {
return err
}
msg := AssembleMessage(confirmSubject, confirmBody)
return smtp.SendMail(s.hostAddress, s.auth, s.from, []string{toAddress}, msg)
}
// ConfirmData represents data passed into the confirm email address template.
type ConfirmData struct {
// Username to be addressed.
Username string
// URL of the instance to present to the receiver.
InstanceURL string
// Name of the instance to present to the receiver.
InstanceName string
// Link to present to the receiver to click on and do the confirmation.
// Should be a full link with protocol eg., https://example.org/confirm_email?token=some-long-token
ConfirmLink string
}

View file

@ -16,5 +16,62 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Package email provides a service for interacting with an SMTP server
package email
import (
"fmt"
"html/template"
"net/smtp"
"os"
"path/filepath"
"github.com/superseriousbusiness/gotosocial/internal/config"
)
// Sender contains functions for sending emails to instance users/new signups.
type Sender interface {
// SendConfirmEmail sends a 'please confirm your email' style email to the given toAddress, with the given data.
SendConfirmEmail(toAddress string, data ConfirmData) error
// SendResetEmail sends a 'reset your password' style email to the given toAddress, with the given data.
SendResetEmail(toAddress string, data ResetData) error
// 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)
}
func NewSender(cfg *config.Config) (Sender, error) {
t, err := loadTemplates(cfg)
if err != nil {
return nil, err
}
auth := smtp.PlainAuth("", cfg.SMTPConfig.Username, cfg.SMTPConfig.Password, cfg.SMTPConfig.Host)
return &sender{
hostAddress: fmt.Sprintf("%s:%d", cfg.SMTPConfig.Host, cfg.SMTPConfig.Port),
from: cfg.SMTPConfig.From,
auth: auth,
template: t,
}, nil
}
type sender struct {
hostAddress string
from string
auth smtp.Auth
template *template.Template
}
// loadTemplates loads html templates for use in emails
func loadTemplates(cfg *config.Config) (*template.Template, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("error getting current working directory: %s", err)
}
// look for all templates that start with 'email_'
tmPath := filepath.Join(cwd, fmt.Sprintf("%semail_*", cfg.TemplateConfig.BaseDir))
return template.ParseGlob(tmPath)
}

View file

@ -0,0 +1,36 @@
/*
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package email_test
import (
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/email"
"github.com/superseriousbusiness/gotosocial/testrig"
)
type EmailTestSuite struct {
suite.Suite
sender email.Sender
}
func (suite *EmailTestSuite) SetupTest() {
testrig.InitTestLog()
suite.sender = testrig.NewEmailSender("../../web/template/")
}

52
internal/email/reset.go Normal file
View file

@ -0,0 +1,52 @@
/*
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package email
import (
"net/smtp"
)
const (
resetTemplate = "email_reset.tmpl"
resetSubject = "Subject: GoToSocial Password Reset"
)
func (s *sender) SendResetEmail(toAddress string, data ResetData) error {
resetBody, err := s.ExecuteTemplate(resetTemplate, data)
if err != nil {
return err
}
msg := AssembleMessage(resetSubject, resetBody)
return smtp.SendMail(s.hostAddress, s.auth, s.from, []string{toAddress}, msg)
}
// ResetData represents data passed into the reset email address template.
type ResetData struct {
// Username to be addressed.
Username string
// URL of the instance to present to the receiver.
InstanceURL string
// Name of the instance to present to the receiver.
InstanceName string
// Link to present to the receiver to click on and begin the reset process.
// Should be a full link with protocol eg., https://example.org/reset_password?token=some-reset-password-token
ResetLink string
}

45
internal/email/util.go Normal file
View file

@ -0,0 +1,45 @@
/*
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package email
import "bytes"
const (
mime = `MIME-version: 1.0;
Content-Type: text/plain; charset="UTF-8";`
)
func (s *sender) ExecuteTemplate(templateName string, data interface{}) (string, error) {
buf := &bytes.Buffer{}
if err := s.template.ExecuteTemplate(buf, templateName, data); err != nil {
return "", err
}
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 {
msg := []byte(
mailSubject + "\r\n" +
mime + "\r\n" +
mailBody + "\r\n")
return msg
}

View file

@ -0,0 +1,64 @@
/*
GoToSocial
Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package email_test
import (
"testing"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/email"
)
type UtilTestSuite struct {
EmailTestSuite
}
func (suite *UtilTestSuite) TestTemplateConfirm() {
confirmData := email.ConfirmData{
Username: "test",
InstanceURL: "https://example.org",
InstanceName: "Test Instance",
ConfirmLink: "https://example.org/confirm_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa",
}
mailBody, err := suite.sender.ExecuteTemplate("email_confirm.tmpl", confirmData)
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))
}
func (suite *UtilTestSuite) TestTemplateReset() {
resetData := email.ResetData{
Username: "test",
InstanceURL: "https://example.org",
InstanceName: "Test Instance",
ResetLink: "https://example.org/reset_email?token=ee24f71d-e615-43f9-afae-385c0799b7fa",
}
mailBody, err := suite.sender.ExecuteTemplate("email_reset.tmpl", resetData)
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))
}
func TestUtilTestSuite(t *testing.T) {
suite.Run(t, &UtilTestSuite{})
}