2024-10-24 17:05:17 +00:00
|
|
|
package emailApi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.stuve.uni-ulm.de/stuve-it/stuve-it-backend/logger"
|
2024-10-24 17:56:54 +00:00
|
|
|
"github.com/microcosm-cc/bluemonday"
|
2024-10-24 17:05:17 +00:00
|
|
|
"github.com/pocketbase/pocketbase"
|
|
|
|
"github.com/pocketbase/pocketbase/core"
|
|
|
|
"github.com/pocketbase/pocketbase/models"
|
|
|
|
"github.com/pocketbase/pocketbase/tools/mailer"
|
|
|
|
"github.com/pocketbase/pocketbase/tools/template"
|
|
|
|
"net/mail"
|
|
|
|
)
|
|
|
|
|
|
|
|
// sendEmailToUser sends an email notification to the user
|
2024-10-25 14:53:06 +00:00
|
|
|
func sendEmailToUser(app *pocketbase.PocketBase, registry *template.Registry, recipient *models.Record, sender *models.Record, emailRecord *models.Record) error {
|
2024-10-24 17:05:17 +00:00
|
|
|
// check if recipient and message are set and recipient is not the sender
|
|
|
|
if recipient == nil || emailRecord == nil || sender == nil {
|
2024-10-25 14:53:06 +00:00
|
|
|
return fmt.Errorf("unable to send email to recipient: recipient, sender or email record is nil")
|
2024-10-24 17:05:17 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 17:56:54 +00:00
|
|
|
// sanitize email content
|
|
|
|
policy := bluemonday.UGCPolicy()
|
|
|
|
content := emailRecord.GetString("content")
|
|
|
|
safeContent := policy.Sanitize(content)
|
|
|
|
|
|
|
|
// get recipient and sender email
|
2024-10-24 17:05:17 +00:00
|
|
|
recipientEmail := recipient.GetString("email")
|
|
|
|
senderEmail := sender.GetString("email")
|
|
|
|
|
2024-10-24 20:06:12 +00:00
|
|
|
// get email subject
|
|
|
|
subject := "[StuVe IT] " + emailRecord.GetString("subject")
|
|
|
|
|
2024-10-24 17:05:17 +00:00
|
|
|
// render email template
|
|
|
|
html, err := registry.LoadFiles(
|
2024-10-24 17:21:50 +00:00
|
|
|
"html/blankEmail.html",
|
2024-10-24 17:05:17 +00:00
|
|
|
).Render(map[string]any{
|
2024-10-24 17:39:02 +00:00
|
|
|
"APP_URL": app.Settings().Meta.AppUrl,
|
|
|
|
"SENDER_SN": sender.GetString("sn"),
|
|
|
|
"SENDER_GIVENNAME": sender.GetString("givenName"),
|
|
|
|
"SENDER_EMAIL": senderEmail,
|
2024-10-24 17:58:59 +00:00
|
|
|
"CONTENT": safeContent,
|
2024-10-24 20:06:12 +00:00
|
|
|
"SUBJECT": subject,
|
2024-10-24 17:05:17 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2024-10-25 14:53:06 +00:00
|
|
|
return fmt.Errorf("error rendering email template for user with id '%s': %v", recipient.Id, err)
|
2024-10-24 17:05:17 +00:00
|
|
|
}
|
|
|
|
|
2024-10-24 17:56:54 +00:00
|
|
|
// set reply-to header
|
2024-10-24 17:05:17 +00:00
|
|
|
headers := make(map[string]string)
|
|
|
|
headers["Reply-To"] = senderEmail
|
|
|
|
|
|
|
|
// send email
|
|
|
|
email := &mailer.Message{
|
|
|
|
From: mail.Address{
|
|
|
|
Address: app.Settings().Meta.SenderAddress,
|
|
|
|
Name: app.Settings().Meta.SenderName,
|
|
|
|
},
|
|
|
|
To: []mail.Address{{Address: recipientEmail}},
|
2024-10-24 20:06:12 +00:00
|
|
|
Subject: subject,
|
2024-10-24 17:05:17 +00:00
|
|
|
HTML: html,
|
2024-10-24 17:39:02 +00:00
|
|
|
Headers: headers,
|
2024-10-24 17:05:17 +00:00
|
|
|
}
|
|
|
|
if err := app.NewMailClient().Send(email); err != nil {
|
2024-10-25 14:53:06 +00:00
|
|
|
return fmt.Errorf("error sending email to user with id '%s': %v", recipient.Id, err)
|
2024-10-24 17:05:17 +00:00
|
|
|
}
|
2024-10-25 14:53:06 +00:00
|
|
|
return nil
|
2024-10-24 17:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func sendEmails(app *pocketbase.PocketBase, emailRecord *models.Record) {
|
|
|
|
registry := template.NewRegistry()
|
|
|
|
|
|
|
|
// expand the createdMessageRecord to get recipient user and send email notification if recipient is set
|
2024-10-24 17:14:45 +00:00
|
|
|
if errs := app.Dao().ExpandRecord(emailRecord, []string{"recipients", "sender"}, nil); len(errs) > 0 {
|
2024-10-24 17:05:17 +00:00
|
|
|
// return new error with all errors
|
|
|
|
logger.LogErrorF("Error expanding created email record: %v", errs)
|
|
|
|
emailRecord.Set("errors", fmt.Sprintf("The email was not sent to anyone due to an error."))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-25 14:53:06 +00:00
|
|
|
// get sender and recipients
|
2024-10-24 17:05:17 +00:00
|
|
|
sender := emailRecord.ExpandedOne("sender")
|
|
|
|
recipients := emailRecord.ExpandedAll("recipients")
|
2024-10-25 14:53:06 +00:00
|
|
|
|
|
|
|
// this is the list of user ids that the email was sent to
|
|
|
|
sendToIds := make([]string, 0)
|
|
|
|
|
|
|
|
// send email to each recipient
|
2024-10-24 17:05:17 +00:00
|
|
|
for _, recipient := range recipients {
|
2024-10-25 14:53:06 +00:00
|
|
|
|
|
|
|
// send email to user
|
|
|
|
err := sendEmailToUser(app, registry, recipient, sender, emailRecord)
|
|
|
|
|
|
|
|
// check if there was an error sending the email
|
|
|
|
if err != nil {
|
|
|
|
logger.LogErrorF("%v", err)
|
|
|
|
} else {
|
|
|
|
sendToIds = append(sendToIds, recipient.Id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set email record fields
|
|
|
|
emailRecord.Set("sentTo", sendToIds)
|
|
|
|
emailRecord.Set("wasSent", true)
|
|
|
|
|
|
|
|
// save email record
|
|
|
|
if err := app.Dao().SaveRecord(emailRecord); err != nil {
|
|
|
|
logger.LogErrorF("Error saving email record: %v", err)
|
2024-10-24 17:05:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitEmailApi initializes the email notifier
|
|
|
|
//
|
|
|
|
// the function sends an email notification after it was created
|
2024-10-24 17:39:02 +00:00
|
|
|
func InitEmailApi(app *pocketbase.PocketBase, _ *core.ServeEvent) error {
|
2024-10-24 17:05:17 +00:00
|
|
|
|
|
|
|
logger.LogInfoF("Adding email notifier")
|
|
|
|
|
|
|
|
app.OnModelAfterCreate("emails").Add(func(e *core.ModelEvent) error {
|
|
|
|
|
|
|
|
// get created message record
|
|
|
|
createdMessageRecord, err := app.Dao().FindRecordById("emails", e.Model.GetId())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go sendEmails(app, createdMessageRecord)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|