scsusers/emails.go

170 lines
4.1 KiB
Go
Raw Normal View History

package scsusers
import (
"bytes"
"fmt"
"log"
"net/smtp"
"strings"
"time"
)
func SendRegistrationEmail(recipient, username, password string) bool {
data := struct {
SiteName string
FromEmail string
UserName string
Pass string
}{
SiteName: c.SiteName,
FromEmail: c.FromEmail,
UserName: username,
Pass: password,
}
var body bytes.Buffer
err := c.Templates.Registration.Execute(&body, data)
if err != nil {
log.Printf("scsusers.sendRegistrationEmail: Registration template failed to execute: %v returned %s\n", data, err.Error())
return false
}
subject := fmt.Sprintf("Welcome to %s", c.SiteName)
err = SendMail(c.SMTPServer, c.FromEmail, subject, body.String(), recipient)
if err != nil {
log.Printf("scsusers.SendRegistrationEmail: Error sending mail to %s: %s\n", recipient, err.Error())
return false
}
return true
}
func SendAlertEmail(username, recipient, message string) bool {
data := struct {
SiteName string
FromEmail string
UserName string
Activity string
}{
SiteName: c.SiteName,
FromEmail: c.FromEmail,
UserName: username,
Activity: message,
}
var body bytes.Buffer
err := c.Templates.Alert.Execute(&body, data)
if err != nil {
log.Printf("scsusers.sendAlertEmail: Alert template failed to execute: %v returned %s\n", data, err.Error())
return false
}
subject := fmt.Sprintf("new Activity Notification on %s", c.SiteName)
err = SendMail(c.SMTPServer, c.FromEmail, subject, body.String(), recipient)
if err != nil {
log.Printf("scsusers.sendAlertEmail: Error sending mail to %s: %s\n", recipient, err.Error())
return false
}
return true
}
func SendRecoveryEmail(recipient, username, code string) bool {
data := struct {
SiteName string
FromEmail string
UserName string
RecoveryCode string
}{
SiteName: c.SiteName,
FromEmail: c.FromEmail,
UserName: username,
RecoveryCode: code,
}
var body bytes.Buffer
2023-10-06 23:39:33 +00:00
err := c.Templates.Recovery.Execute(&body, data)
if err != nil {
log.Printf("scsusers.sendRecoveryEmail: Recovery template failed to execute: %v returned %s\n", data, err.Error())
return false
}
subject := fmt.Sprintf("Account recovery at %s", c.SiteName)
err = SendMail(c.SMTPServer, c.FromEmail, subject, body.String(), recipient)
if err != nil {
log.Printf("scsusers.sendRecoveryEmail: Error sending mail to %s: %s\n", recipient, err.Error())
return false
}
return true
}
func Send2faEmail(recipient, fullname, code string) error {
data := struct {
2023-09-26 20:45:19 +00:00
SiteName string
FromEmail string
FullName string
Code string
}{
2023-09-26 20:45:19 +00:00
SiteName: c.SiteName,
FromEmail: c.FromEmail,
FullName: fullname,
Code: code,
}
var body bytes.Buffer
err := c.Templates.TwoFA.Execute(&body, data)
if err != nil {
return fmt.Errorf("scsusers.send2fayEmail: 2fa template failed to execute: %v returned %s", data, err.Error())
}
subject := fmt.Sprintf("Two Factor Authentication Code at %s", c.SiteName)
err = SendMail(c.SMTPServer, c.FromEmail, subject, body.String(), recipient)
return err
}
func SendMail(addr, from, subject, body string, to string) error {
r := strings.NewReplacer("\r\n", "", "\r", "", "\n", "", "%0a", "", "%0d", "")
c, err := smtp.Dial(addr)
if err != nil {
return err
}
defer c.Close()
if err = c.Mail(r.Replace(from)); err != nil {
return err
}
to = r.Replace(to)
if err = c.Rcpt(to); err != nil {
return err
}
w, err := c.Data()
if err != nil {
return err
}
date := time.Now()
msg := "To: " + to + "\r\n" +
"From: " + from + "\r\n" +
"Subject: " + subject + "\r\n" +
"Content-Type: text/html; charset=\"UTF-8\"\r\n" +
"Date: " + date.Format(time.RFC1123Z) + "\r\n" +
"\r\n" + body
_, err = w.Write([]byte(msg))
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}
2023-12-18 18:35:55 +00:00
func SendAlert(u int64, subject, body string) error {
2024-01-10 19:35:09 +00:00
if c.Testing {
return nil
}
2023-12-18 18:35:55 +00:00
user, ok := GetById(u)
if !ok {
return fmt.Errorf("scsusers.SendAlert: User %d not found", u)
}
email, ok := user.Get("email")
if !ok {
return fmt.Errorf("scsusers.SendAlert: User %d has no email", u)
}
return SendMail(c.SMTPServer, c.FromEmail, subject, body, email)
}