1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-29 13:49:28 +08:00
Ivan Milošević 1f8a221c22
MF-1059 - Add TLS support for email (#1560)
* Use gomail package for sending emails

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* remove print err

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* Add vendor

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* Rename email structure
remove logger

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* typo in var name

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* rename var

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* remove MF_EMAIL_SECRET

Signed-off-by: Ivan Milosevic <iva@blokovi.com>
2022-02-07 13:51:43 +01:00

106 lines
2.1 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package email
import (
"bytes"
"net/mail"
"strconv"
"text/template"
"github.com/mainflux/mainflux/pkg/errors"
"gopkg.in/gomail.v2"
)
var (
// ErrMissingEmailTemplate missing email template file
errMissingEmailTemplate = errors.New("Missing e-mail template file")
errParseTemplate = errors.New("Parse e-mail template failed")
errExecTemplate = errors.New("Execute e-mail template failed")
errSendMail = errors.New("Sending e-mail failed")
)
type email struct {
To []string
From string
Subject string
Header string
Content string
Footer string
}
// Config email agent configuration.
type Config struct {
Host string
Port string
Username string
Password string
FromAddress string
FromName string
Template string
}
// Agent for mailing
type Agent struct {
conf *Config
tmpl *template.Template
dial *gomail.Dialer
}
// New creates new email agent
func New(c *Config) (*Agent, error) {
a := &Agent{}
a.conf = c
port, err := strconv.Atoi(c.Port)
if err != nil {
return a, err
}
d := gomail.NewDialer(c.Host, port, c.Username, c.Password)
a.dial = d
tmpl, err := template.ParseFiles(c.Template)
if err != nil {
return a, errors.Wrap(errParseTemplate, err)
}
a.tmpl = tmpl
return a, nil
}
// Send sends e-mail
func (a *Agent) Send(To []string, From, Subject, Header, Content, Footer string) error {
if a.tmpl == nil {
return errMissingEmailTemplate
}
buff := new(bytes.Buffer)
e := email{
To: To,
From: From,
Subject: Subject,
Header: Header,
Content: Content,
Footer: Footer,
}
if From == "" {
from := mail.Address{Name: a.conf.FromName, Address: a.conf.FromAddress}
e.From = from.String()
}
if err := a.tmpl.Execute(buff, e); err != nil {
return errors.Wrap(errExecTemplate, err)
}
m := gomail.NewMessage()
m.SetHeader("From", e.From)
m.SetHeader("To", To...)
m.SetHeader("Subject", Subject)
m.SetBody("text/plain", buff.String())
if err := a.dial.DialAndSend(m); err != nil {
return errors.Wrap(errSendMail, err)
}
return nil
}