Files
spend-sparrow/internal/core/mail.go
Tim Wundenberg 1be46780bb
All checks were successful
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 1m19s
feat: extract into remaining packages
There has been a cyclic dependency.
transaction
	-> treasure_chest
	-> transaction_recurring
	-> transaction

This has been temporarily solved by moving the GenerateTransactions
function into the transaction package. In the future, this function has
to be rewritten to use a proper Service insteas of direct DB access or
replaced with a different system entirely.
2025-12-31 22:26:59 +01:00

56 lines
1.1 KiB
Go

package core
import (
"context"
"fmt"
"log/slog"
"net/smtp"
)
type Mail interface {
// Sending an email is a fire and forget operation. Thus no error handling
SendMail(ctx context.Context, to string, subject string, message string)
}
type MailImpl struct {
server *Settings
}
func NewMail(server *Settings) MailImpl {
return MailImpl{server: server}
}
func (m MailImpl) SendMail(ctx context.Context, to string, subject string, message string) {
go m.internalSendMail(ctx, to, subject, message)
}
func (m MailImpl) internalSendMail(ctx context.Context, to string, subject string, message string) {
if m.server.Smtp == nil {
return
}
s := m.server.Smtp
auth := smtp.PlainAuth("", s.User, s.Pass, s.Host)
msg := fmt.Sprintf(
`From: %v <%v>
To: %v
Subject: %v
MIME-version: 1.0;
Content-Type: text/html; charset="UTF-8";
%v`,
s.FromName,
s.FromMail,
to,
subject,
message)
slog.InfoContext(ctx, "sending mail", "to", to)
err := smtp.SendMail(s.Host+":"+s.Port, auth, s.FromMail, []string{to}, []byte(msg))
if err != nil {
slog.ErrorContext(ctx, "Error sending mail", "err", err)
}
}