// Copyright 2024 Patial Tech. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package mailer import ( "errors" "net/mail" "gitserver.in/patialtech/rano/config" ) type ( transporter interface { send(*message) error } Recipients struct { To []mail.Address `json:"to"` Cc []mail.Address `json:"cc"` Bcc []mail.Address `json:"bcc"` } message struct { From string `json:"from" validate:"required"` Recipients Recipients `json:"recipients" validate:"required"` Subject string `json:"subject" validate:"required"` HtmlBody string `json:"htmlBody" validate:"required"` ReplyTo *mail.Address `json:"replyTo"` } Template interface { Subject() string HtmlBody() (string, error) } ) func Send(to []mail.Address, tpl Template) error { return send(Recipients{To: to}, tpl) } func SendCC(subject string, to, cc []mail.Address, tpl Template) error { return send(Recipients{To: to, Cc: cc}, tpl) } func send(recipients Recipients, tpl Template) error { if tpl == nil { return errors.New("mailer: email template is nil") } if len(recipients.To) == 0 { return errors.New("mailer: no recipient found") } // TODO remove recepient with bounce hiostory body, err := tpl.HtmlBody() if err != nil { return err } // get ENV based transporter and send mail return getTransporter().send(&message{ From: config.Read().MailerFrom, Recipients: recipients, Subject: tpl.Subject(), HtmlBody: body, }) } func getTransporter() transporter { switch config.AppEnv { default: return transportDev{} } }