45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
package service
|
|
|
|
import (
|
|
"me-fit/log"
|
|
"me-fit/types"
|
|
|
|
"fmt"
|
|
"net/smtp"
|
|
)
|
|
|
|
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 NewMailImpl(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>\nTo: %v\nSubject: %v\nMIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n%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)
|
|
}
|
|
}
|