src.dualinventive.com/go/authentication-service/internal/pwreset/emailSender.go

36 lines
908 B
Go

package pwreset
import (
"net/smtp"
)
//EmailSender is responsible for sending an email.
type EmailSender interface {
Send(to []string, body []byte) error
From() string
}
//NewEmailSender returns an smtp email sender.
func NewEmailSender(host, port, user, pass, sender string) EmailSender {
return &emailSender{host, port, user, pass, sender, smtp.SendMail}
}
//nolint: staticcheck
type emailSender struct {
host, port, user, pass, sender string
send func(string, smtp.Auth, string, []string, []byte) error
}
//From returns the address of the sending account
func (e *emailSender) From() string {
return e.sender
}
//Send sends the given body to given recipients using smtp.
func (e *emailSender) Send(to []string, body []byte) error {
addr := e.host + ":" + e.port
auth := smtp.PlainAuth("", e.user, e.pass, e.host)
return e.send(addr, auth, e.sender, to, body)
}