1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-01-23 22:12:48 +02:00

77 lines
1.4 KiB
Go
Raw Normal View History

2022-07-07 00:19:05 +03:00
package mailer
import (
"fmt"
"io"
"net/mail"
"net/smtp"
"github.com/domodwyer/mailyak/v3"
)
var _ Mailer = (*SmtpClient)(nil)
// NewSmtpClient creates new `SmtpClient` with the provided configuration.
func NewSmtpClient(
host string,
port int,
username string,
password string,
tls bool,
) *SmtpClient {
return &SmtpClient{
host: host,
port: port,
username: username,
password: password,
tls: tls,
}
}
// SmtpClient defines a SMTP mail client structure that implements
// `mailer.Mailer` interface.
type SmtpClient struct {
host string
port int
username string
password string
tls bool
}
// Send implements `mailer.Mailer` interface.
func (m *SmtpClient) Send(
fromEmail mail.Address,
toEmail mail.Address,
subject string,
htmlContent string,
2022-07-07 00:19:05 +03:00
attachments map[string]io.Reader,
) error {
smtpAuth := smtp.PlainAuth("", m.username, m.password, m.host)
// create mail instance
var yak *mailyak.MailYak
if m.tls {
var tlsErr error
yak, tlsErr = mailyak.NewWithTLS(fmt.Sprintf("%s:%d", m.host, m.port), smtpAuth, nil)
if tlsErr != nil {
return tlsErr
}
} else {
yak = mailyak.New(fmt.Sprintf("%s:%d", m.host, m.port), smtpAuth)
}
if fromEmail.Name != "" {
yak.FromName(fromEmail.Name)
}
yak.From(fromEmail.Address)
yak.To(toEmail.Address)
2022-07-07 00:19:05 +03:00
yak.Subject(subject)
yak.HTML().Set(htmlContent)
2022-07-07 00:19:05 +03:00
for name, data := range attachments {
yak.Attach(name, data)
}
return yak.Send()
}