2022-01-06 08:00:24 +00:00
|
|
|
package smtp
|
2020-05-20 12:28:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2023-12-05 17:01:03 +00:00
|
|
|
"errors"
|
2021-04-28 07:53:33 +00:00
|
|
|
"net"
|
|
|
|
"net/smtp"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
2022-02-16 15:49:17 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/notification/channels"
|
|
|
|
"github.com/zitadel/zitadel/internal/notification/messages"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-05-20 12:28:08 +00:00
|
|
|
)
|
|
|
|
|
2022-01-06 08:00:24 +00:00
|
|
|
var _ channels.NotificationChannel = (*Email)(nil)
|
|
|
|
|
2020-05-20 12:28:08 +00:00
|
|
|
type Email struct {
|
2023-08-29 07:08:24 +00:00
|
|
|
smtpClient *smtp.Client
|
|
|
|
senderAddress string
|
|
|
|
senderName string
|
|
|
|
replyToAddress string
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 13:20:53 +00:00
|
|
|
func InitChannel(cfg *Config) (*Email, error) {
|
|
|
|
client, err := cfg.SMTP.connectToSMTP(cfg.Tls)
|
2020-05-20 12:28:08 +00:00
|
|
|
if err != nil {
|
2022-05-13 12:13:07 +00:00
|
|
|
logging.New().WithError(err).Error("could not connect to smtp")
|
2020-05-20 12:28:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-07 13:22:37 +00:00
|
|
|
logging.New().Debug("successfully initialized smtp email channel")
|
2020-05-20 12:28:08 +00:00
|
|
|
return &Email{
|
2023-08-29 07:08:24 +00:00
|
|
|
smtpClient: client,
|
2023-10-10 13:20:53 +00:00
|
|
|
senderName: cfg.FromName,
|
|
|
|
senderAddress: cfg.From,
|
|
|
|
replyToAddress: cfg.ReplyToAddress,
|
2020-05-20 12:28:08 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:00:24 +00:00
|
|
|
func (email *Email) HandleMessage(message channels.Message) error {
|
2020-05-20 12:28:08 +00:00
|
|
|
defer email.smtpClient.Close()
|
2022-01-06 08:00:24 +00:00
|
|
|
emailMsg, ok := message.(*messages.Email)
|
2020-05-20 12:28:08 +00:00
|
|
|
if !ok {
|
2024-06-20 19:51:42 +00:00
|
|
|
return zerrors.ThrowInternal(nil, "EMAIL-s8JLs", "Errors.SMTP.NotEmailMessage")
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
2022-01-06 08:00:24 +00:00
|
|
|
|
|
|
|
if emailMsg.Content == "" || emailMsg.Subject == "" || len(emailMsg.Recipients) == 0 {
|
2024-06-20 19:51:42 +00:00
|
|
|
return zerrors.ThrowInternal(nil, "EMAIL-zGemZ", "Errors.SMTP.RequiredAttributes")
|
2022-01-06 08:00:24 +00:00
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
emailMsg.SenderEmail = email.senderAddress
|
|
|
|
emailMsg.SenderName = email.senderName
|
2023-08-29 07:08:24 +00:00
|
|
|
emailMsg.ReplyToAddress = email.replyToAddress
|
2020-05-20 12:28:08 +00:00
|
|
|
// To && From
|
|
|
|
if err := email.smtpClient.Mail(emailMsg.SenderEmail); err != nil {
|
2024-06-20 19:51:42 +00:00
|
|
|
return zerrors.ThrowInternal(err, "EMAIL-s3is3", "Errors.SMTP.CouldNotSetSender")
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
for _, recp := range append(append(emailMsg.Recipients, emailMsg.CC...), emailMsg.BCC...) {
|
|
|
|
if err := email.smtpClient.Rcpt(recp); err != nil {
|
2024-06-20 19:51:42 +00:00
|
|
|
return zerrors.ThrowInternal(err, "EMAIL-s4is4", "Errors.SMTP.CouldNotSetRecipient")
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data
|
|
|
|
w, err := email.smtpClient.Data()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-28 22:09:06 +00:00
|
|
|
content, err := emailMsg.GetContent()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write([]byte(content))
|
2020-05-20 12:28:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return email.smtpClient.Quit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (smtpConfig SMTP) connectToSMTP(tlsRequired bool) (client *smtp.Client, err error) {
|
|
|
|
host, _, err := net.SplitHostPort(smtpConfig.Host)
|
|
|
|
if err != nil {
|
2024-06-20 19:51:42 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "EMAIL-spR56", "Errors.SMTP.CouldNotSplit")
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !tlsRequired {
|
2024-04-11 07:16:10 +00:00
|
|
|
client, err = smtpConfig.getSMTPClient()
|
2020-05-20 12:28:08 +00:00
|
|
|
} else {
|
2024-04-11 07:16:10 +00:00
|
|
|
client, err = smtpConfig.getSMTPClientWithTls(host)
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = smtpConfig.smtpAuth(client, host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2024-04-11 07:16:10 +00:00
|
|
|
func (smtpConfig SMTP) getSMTPClient() (*smtp.Client, error) {
|
2020-05-20 12:28:08 +00:00
|
|
|
client, err := smtp.Dial(smtpConfig.Host)
|
|
|
|
if err != nil {
|
2024-06-20 19:51:42 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "EMAIL-skwos", "Errors.SMTP.CouldNotDial")
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2024-04-11 07:16:10 +00:00
|
|
|
func (smtpConfig SMTP) getSMTPClientWithTls(host string) (*smtp.Client, error) {
|
2020-05-20 12:28:08 +00:00
|
|
|
conn, err := tls.Dial("tcp", smtpConfig.Host, &tls.Config{})
|
2021-04-28 07:53:33 +00:00
|
|
|
|
|
|
|
if errors.As(err, &tls.RecordHeaderError{}) {
|
2024-06-20 19:51:42 +00:00
|
|
|
logging.OnError(err).Warn("could not connect using normal tls. trying starttls instead...")
|
2024-04-11 07:16:10 +00:00
|
|
|
return smtpConfig.getSMTPClientWithStartTls(host)
|
2021-04-28 07:53:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 12:28:08 +00:00
|
|
|
if err != nil {
|
2024-06-20 19:51:42 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "EMAIL-sl39s", "Errors.SMTP.CouldNotDialTLS")
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err := smtp.NewClient(conn, host)
|
|
|
|
if err != nil {
|
2024-06-20 19:51:42 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "EMAIL-skwi4", "Errors.SMTP.CouldNotCreateClient")
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
return client, err
|
|
|
|
}
|
|
|
|
|
2024-04-11 07:16:10 +00:00
|
|
|
func (smtpConfig SMTP) getSMTPClientWithStartTls(host string) (*smtp.Client, error) {
|
|
|
|
client, err := smtpConfig.getSMTPClient()
|
2021-04-28 07:53:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := client.StartTLS(&tls.Config{
|
|
|
|
ServerName: host,
|
|
|
|
}); err != nil {
|
2024-06-20 19:51:42 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "EMAIL-guvsQ", "Errors.SMTP.CouldNotStartTLS")
|
2021-04-28 07:53:33 +00:00
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2020-05-20 12:28:08 +00:00
|
|
|
func (smtpConfig SMTP) smtpAuth(client *smtp.Client, host string) error {
|
|
|
|
if !smtpConfig.HasAuth() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Auth
|
2024-04-30 07:31:07 +00:00
|
|
|
err := client.Auth(PlainOrLoginAuth(smtpConfig.User, smtpConfig.Password, host))
|
2023-03-28 22:09:06 +00:00
|
|
|
if err != nil {
|
2024-06-20 19:51:42 +00:00
|
|
|
return zerrors.ThrowInternal(err, "EMAIL-s9kfs", "Errors.SMTP.CouldNotAuth")
|
2023-03-28 22:09:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
2024-06-20 19:51:42 +00:00
|
|
|
|
|
|
|
func TestConfiguration(cfg *Config, testEmail string) error {
|
|
|
|
client, err := cfg.SMTP.connectToSMTP(cfg.Tls)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
message := &messages.Email{
|
|
|
|
Recipients: []string{testEmail},
|
|
|
|
Subject: "Test email",
|
|
|
|
Content: "This is a test email to check if your SMTP provider works fine",
|
|
|
|
SenderEmail: cfg.From,
|
|
|
|
SenderName: cfg.FromName,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := client.Mail(cfg.From); err != nil {
|
|
|
|
return zerrors.ThrowInternal(err, "EMAIL-s3is3", "Errors.SMTP.CouldNotSetSender")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := client.Rcpt(testEmail); err != nil {
|
|
|
|
return zerrors.ThrowInternal(err, "EMAIL-s4is4", "Errors.SMTP.CouldNotSetRecipient")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open data connection
|
|
|
|
w, err := client.Data()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send content
|
|
|
|
content, err := message.GetContent()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write([]byte(content))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close IO and quit smtp connection
|
|
|
|
err = w.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return client.Quit()
|
|
|
|
}
|