diff options
author | 2023-03-14 17:11:04 +0100 | |
---|---|---|
committer | 2023-03-14 16:11:04 +0000 | |
commit | 196cd88b1c7c44a337ca12f6a804f1bb7fa83c4a (patch) | |
tree | 9607d95b3f4f55a1ebfeded2f7aa9a3c8866bd0a /internal/email | |
parent | [chore] fix + update swagger docs (#1622) (diff) | |
download | gotosocial-196cd88b1c7c44a337ca12f6a804f1bb7fa83c4a.tar.xz |
[feature] Allow admins to send test emails (#1620)
* [feature] Allow admins to send test emails
* implement unwrap on new error type
* add + use gtserror types
* GoToSocial Email Test -> GoToSocial Test Email
* add + use getInstance db call
* removed unused "unknown" error type
Diffstat (limited to 'internal/email')
-rw-r--r-- | internal/email/confirm.go | 35 | ||||
-rw-r--r-- | internal/email/noopsender.go | 21 | ||||
-rw-r--r-- | internal/email/reset.go | 33 | ||||
-rw-r--r-- | internal/email/sender.go | 3 | ||||
-rw-r--r-- | internal/email/test.go | 58 |
5 files changed, 121 insertions, 29 deletions
diff --git a/internal/email/confirm.go b/internal/email/confirm.go index 94aebc61f..a6548e7d1 100644 --- a/internal/email/confirm.go +++ b/internal/email/confirm.go @@ -21,8 +21,7 @@ import ( "bytes" "net/smtp" - "github.com/superseriousbusiness/gotosocial/internal/config" - "github.com/superseriousbusiness/gotosocial/internal/log" + "github.com/superseriousbusiness/gotosocial/internal/gtserror" ) const ( @@ -30,6 +29,19 @@ const ( confirmSubject = "GoToSocial Email Confirmation" ) +// 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 +} + func (s *sender) SendConfirmEmail(toAddress string, data ConfirmData) error { buf := &bytes.Buffer{} if err := s.template.ExecuteTemplate(buf, confirmTemplate, data); err != nil { @@ -41,19 +53,10 @@ func (s *sender) SendConfirmEmail(toAddress string, data ConfirmData) error { if err != nil { return err } - log.Trace(nil, s.hostAddress+"\n"+config.GetSMTPUsername()+":password"+"\n"+s.from+"\n"+toAddress+"\n\n"+string(msg)+"\n") - 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 + if err := smtp.SendMail(s.hostAddress, s.auth, s.from, []string{toAddress}, msg); err != nil { + return gtserror.SetType(err, gtserror.TypeSMTP) + } + + return nil } diff --git a/internal/email/noopsender.go b/internal/email/noopsender.go index 435ffb04c..7164440f3 100644 --- a/internal/email/noopsender.go +++ b/internal/email/noopsender.go @@ -88,3 +88,24 @@ func (s *noopSender) SendResetEmail(toAddress string, data ResetData) error { return nil } + +func (s *noopSender) SendTestEmail(toAddress string, data TestData) error { + buf := &bytes.Buffer{} + if err := s.template.ExecuteTemplate(buf, testTemplate, data); err != nil { + return err + } + testBody := buf.String() + + msg, err := assembleMessage(testSubject, testBody, toAddress, "test@example.org") + if err != nil { + return err + } + + log.Tracef(nil, "NOT SENDING test email to %s with contents: %s", toAddress, msg) + + if s.sendCallback != nil { + s.sendCallback(toAddress, string(msg)) + } + + return nil +} diff --git a/internal/email/reset.go b/internal/email/reset.go index 0b950c1c9..cb1da9fee 100644 --- a/internal/email/reset.go +++ b/internal/email/reset.go @@ -20,6 +20,8 @@ package email import ( "bytes" "net/smtp" + + "github.com/superseriousbusiness/gotosocial/internal/gtserror" ) const ( @@ -27,6 +29,19 @@ const ( resetSubject = "GoToSocial Password Reset" ) +// 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 +} + func (s *sender) SendResetEmail(toAddress string, data ResetData) error { buf := &bytes.Buffer{} if err := s.template.ExecuteTemplate(buf, resetTemplate, data); err != nil { @@ -38,18 +53,10 @@ func (s *sender) SendResetEmail(toAddress string, data ResetData) error { if err != nil { return err } - 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 + if err := smtp.SendMail(s.hostAddress, s.auth, s.from, []string{toAddress}, msg); err != nil { + return gtserror.SetType(err, gtserror.TypeSMTP) + } + + return nil } diff --git a/internal/email/sender.go b/internal/email/sender.go index 3d188011f..13dd26531 100644 --- a/internal/email/sender.go +++ b/internal/email/sender.go @@ -32,6 +32,9 @@ type Sender interface { // SendResetEmail sends a 'reset your password' style email to the given toAddress, with the given data. SendResetEmail(toAddress string, data ResetData) error + + // SendTestEmail sends a 'testing email sending' style email to the given toAddress, with the given data. + SendTestEmail(toAddress string, data TestData) error } // NewSender returns a new email Sender interface with the given configuration, or an error if something goes wrong. diff --git a/internal/email/test.go b/internal/email/test.go new file mode 100644 index 000000000..1e411f161 --- /dev/null +++ b/internal/email/test.go @@ -0,0 +1,58 @@ +// GoToSocial +// Copyright (C) GoToSocial Authors admin@gotosocial.org +// SPDX-License-Identifier: AGPL-3.0-or-later +// +// 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" + "net/smtp" + + "github.com/superseriousbusiness/gotosocial/internal/gtserror" +) + +const ( + testTemplate = "email_test_text.tmpl" + testSubject = "GoToSocial Test Email" +) + +type TestData struct { + // Username of admin user who sent the test. + SendingUsername string + // URL of the instance to present to the receiver. + InstanceURL string + // Name of the instance to present to the receiver. + InstanceName string +} + +func (s *sender) SendTestEmail(toAddress string, data TestData) error { + buf := &bytes.Buffer{} + if err := s.template.ExecuteTemplate(buf, testTemplate, data); err != nil { + return err + } + testBody := buf.String() + + msg, err := assembleMessage(testSubject, testBody, toAddress, s.from) + if err != nil { + return err + } + + if err := smtp.SendMail(s.hostAddress, s.auth, s.from, []string{toAddress}, msg); err != nil { + return gtserror.SetType(err, gtserror.TypeSMTP) + } + + return nil +} |