package service import ( "fmt" "net/smtp" "me-fit/types" "me-fit/utils" ) type MailService interface { // Sending an email is a fire and forget operation. Thus no error handling SendMail(to string, subject string, message string) } type MailServiceImpl struct { serverSettings *types.ServerSettings } func NewMailServiceImpl(serverSettings *types.ServerSettings) MailServiceImpl { return MailServiceImpl{serverSettings: serverSettings} } func (m MailServiceImpl) SendMail(to string, subject string, message string) { if m.serverSettings.Smtp == nil { return } s := m.serverSettings.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) err := smtp.SendMail(s.Host+":"+s.Port, auth, s.FromMail, []string{to}, []byte(msg)) if err != nil { utils.LogError("Error sending mail: %v", err) } }