2021-07-12 18:22:16 +00:00
|
|
|
package email
|
|
|
|
|
|
|
|
import (
|
2022-01-08 08:38:42 +00:00
|
|
|
"crypto/tls"
|
2021-07-12 18:22:16 +00:00
|
|
|
"log"
|
2022-01-08 08:38:42 +00:00
|
|
|
"strconv"
|
2021-07-12 18:22:16 +00:00
|
|
|
|
2021-07-23 16:27:44 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-01-08 08:38:42 +00:00
|
|
|
gomail "gopkg.in/mail.v2"
|
2021-07-12 18:22:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Sender struct {
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSender() Sender {
|
2022-01-07 13:57:31 +00:00
|
|
|
return Sender{User: constants.SMTP_USERNAME, Password: constants.SMTP_PASSWORD}
|
2021-07-12 18:22:16 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 08:38:42 +00:00
|
|
|
func (sender Sender) SendMail(to []string, Subject, bodyMessage string) error {
|
|
|
|
m := gomail.NewMessage()
|
|
|
|
m.SetHeader("From", constants.SENDER_EMAIL)
|
|
|
|
m.SetHeader("To", to...)
|
|
|
|
m.SetHeader("Subject", Subject)
|
|
|
|
m.SetBody("text/html", bodyMessage)
|
|
|
|
port, _ := strconv.Atoi(constants.SMTP_PORT)
|
|
|
|
d := gomail.NewDialer(constants.SMTP_HOST, port, constants.SMTP_USERNAME, constants.SMTP_PASSWORD)
|
|
|
|
if constants.ENV == "development" {
|
|
|
|
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
|
|
|
|
}
|
|
|
|
if err := d.DialAndSend(m); err != nil {
|
2021-07-12 18:22:16 +00:00
|
|
|
log.Printf("smtp error: %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|