mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2026-01-05 23:03:15 -06:00
flesh out the email sender interface
This commit is contained in:
parent
3b31e558ab
commit
c868cc3e65
6 changed files with 307 additions and 1 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue