30 lines
737 B
Go
30 lines
737 B
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"me-fit/types"
|
|
"net/smtp"
|
|
)
|
|
|
|
type MailService struct {
|
|
serverSettings *types.ServerSettings
|
|
}
|
|
|
|
func NewMailService(serverSettings *types.ServerSettings) MailService {
|
|
return MailService{serverSettings: serverSettings}
|
|
}
|
|
|
|
func (m MailService) SendMail(to string, subject string, message string) error {
|
|
if m.serverSettings.Smtp == nil {
|
|
return nil
|
|
}
|
|
|
|
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)
|
|
|
|
return smtp.SendMail(s.Host+":"+s.Port, auth, s.FromMail, []string{to}, []byte(msg))
|
|
}
|