fix: move implementation to "internal" package
Some checks failed
Build Docker Image / Build-Docker-Image (push) Has been cancelled
Some checks failed
Build Docker Image / Build-Docker-Image (push) Has been cancelled
This commit is contained in:
55
internal/service/mail.go
Normal file
55
internal/service/mail.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/smtp"
|
||||
"spend-sparrow/internal/log"
|
||||
"spend-sparrow/internal/types"
|
||||
)
|
||||
|
||||
type Mail interface {
|
||||
// Sending an email is a fire and forget operation. Thus no error handling
|
||||
SendMail(to string, subject string, message string)
|
||||
}
|
||||
|
||||
type MailImpl struct {
|
||||
server *types.Settings
|
||||
}
|
||||
|
||||
func NewMail(server *types.Settings) MailImpl {
|
||||
return MailImpl{server: server}
|
||||
}
|
||||
|
||||
func (m MailImpl) SendMail(to string, subject string, message string) {
|
||||
go m.internalSendMail(to, subject, message)
|
||||
}
|
||||
|
||||
func (m MailImpl) internalSendMail(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)
|
||||
|
||||
log.Info("Sending mail to %v", to)
|
||||
err := smtp.SendMail(s.Host+":"+s.Port, auth, s.FromMail, []string{to}, []byte(msg))
|
||||
if err != nil {
|
||||
log.Error("Error sending mail: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user