gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

sender.go (2872B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package email
     19 
     20 import (
     21 	"fmt"
     22 	"net/smtp"
     23 	"text/template"
     24 
     25 	"github.com/superseriousbusiness/gotosocial/internal/config"
     26 )
     27 
     28 // Sender contains functions for sending emails to instance users/new signups.
     29 type Sender interface {
     30 	// SendConfirmEmail sends a 'please confirm your email' style email to the given toAddress, with the given data.
     31 	SendConfirmEmail(toAddress string, data ConfirmData) error
     32 
     33 	// SendResetEmail sends a 'reset your password' style email to the given toAddress, with the given data.
     34 	SendResetEmail(toAddress string, data ResetData) error
     35 
     36 	// SendTestEmail sends a 'testing email sending' style email to the given toAddress, with the given data.
     37 	SendTestEmail(toAddress string, data TestData) error
     38 
     39 	// SendNewReportEmail sends an email notification to the given addresses, letting them
     40 	// know that a new report has been created targeting a user on this instance.
     41 	//
     42 	// It is expected that the toAddresses have already been filtered to ensure that they
     43 	// all belong to admins + moderators.
     44 	SendNewReportEmail(toAddresses []string, data NewReportData) error
     45 
     46 	// SendReportClosedEmail sends an email notification to the given address, letting them
     47 	// know that a report that they created has been closed / resolved by an admin.
     48 	SendReportClosedEmail(toAddress string, data ReportClosedData) error
     49 }
     50 
     51 // NewSender returns a new email Sender interface with the given configuration, or an error if something goes wrong.
     52 func NewSender() (Sender, error) {
     53 	templateBaseDir := config.GetWebTemplateBaseDir()
     54 	t, err := loadTemplates(templateBaseDir)
     55 	if err != nil {
     56 		return nil, err
     57 	}
     58 
     59 	username := config.GetSMTPUsername()
     60 	password := config.GetSMTPPassword()
     61 	host := config.GetSMTPHost()
     62 	port := config.GetSMTPPort()
     63 	from := config.GetSMTPFrom()
     64 
     65 	return &sender{
     66 		hostAddress: fmt.Sprintf("%s:%d", host, port),
     67 		from:        from,
     68 		auth:        smtp.PlainAuth("", username, password, host),
     69 		template:    t,
     70 	}, nil
     71 }
     72 
     73 type sender struct {
     74 	hostAddress string
     75 	from        string
     76 	auth        smtp.Auth
     77 	template    *template.Template
     78 }