2015-09-22 02:12:37 +05:30
|
|
|
package email
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2016-07-11 21:46:32 +05:30
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
2015-09-22 02:12:37 +05:30
|
|
|
|
|
|
|
"gopkg.in/gomail.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SmtpEmailerType = "smtp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RegisterEmailerConfigType(SmtpEmailerType, func() EmailerConfig { return &SmtpEmailerConfig{} })
|
|
|
|
}
|
|
|
|
|
|
|
|
type SmtpEmailerConfig struct {
|
|
|
|
Host string `json:"host"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
2016-06-28 12:04:55 +05:30
|
|
|
FromAddr string `json:"from"`
|
2016-07-11 21:46:32 +05:30
|
|
|
|
|
|
|
// OPTIONAL: If empty and host is of form "host:port" just use that. For backward
|
|
|
|
// compatibility do not change this.
|
|
|
|
Port int `json:"port"`
|
|
|
|
|
|
|
|
// DEPRICATED: If "username" and "password" are provided, use them.
|
|
|
|
Auth string `json:"auth"`
|
2015-09-22 02:12:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg SmtpEmailerConfig) EmailerType() string {
|
|
|
|
return SmtpEmailerType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg SmtpEmailerConfig) EmailerID() string {
|
2015-09-22 04:09:29 +05:30
|
|
|
return SmtpEmailerType
|
2015-09-22 02:12:37 +05:30
|
|
|
}
|
|
|
|
|
2016-06-28 12:04:55 +05:30
|
|
|
func (cfg SmtpEmailerConfig) Emailer(fromAddr string) (Emailer, error) {
|
|
|
|
from := cfg.FromAddr
|
|
|
|
if from == "" {
|
|
|
|
from = fromAddr
|
|
|
|
}
|
|
|
|
if from == "" {
|
|
|
|
return nil, errors.New(`missing "from" field in email config`)
|
|
|
|
}
|
|
|
|
|
2016-07-11 21:46:32 +05:30
|
|
|
host, port := cfg.Host, cfg.Port
|
|
|
|
|
|
|
|
// If port hasn't been supplied, check the "host" field.
|
|
|
|
if port == 0 {
|
|
|
|
hostStr, portStr, err := net.SplitHostPort(cfg.Host)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf(`"host" must be in format of "host:port" %v`, err)
|
|
|
|
}
|
|
|
|
host = hostStr
|
|
|
|
if port, err = strconv.Atoi(portStr); err != nil {
|
|
|
|
return nil, fmt.Errorf(`failed to parse %q as "host:port" %v`, cfg.Host, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cfg.Username == "") != (cfg.Password == "") {
|
|
|
|
return nil, errors.New(`must provide both "username" and "password"`)
|
|
|
|
}
|
|
|
|
|
2015-09-22 02:12:37 +05:30
|
|
|
var dialer *gomail.Dialer
|
2016-07-11 21:46:32 +05:30
|
|
|
if cfg.Username == "" {
|
|
|
|
// NOTE(ericchiang): Guess SSL using the same logic as gomail. We should
|
|
|
|
// eventually allow this to be set explicitly.
|
|
|
|
dialer = &gomail.Dialer{Host: host, Port: port, SSL: port == 465}
|
2015-09-22 02:12:37 +05:30
|
|
|
} else {
|
2016-07-11 21:46:32 +05:30
|
|
|
dialer = gomail.NewPlainDialer(host, port, cfg.Username, cfg.Password)
|
2015-09-22 02:12:37 +05:30
|
|
|
}
|
2016-07-11 21:46:32 +05:30
|
|
|
|
|
|
|
return &smtpEmailer{dialer: dialer, from: from}, nil
|
2015-09-22 02:12:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type smtpEmailerConfig SmtpEmailerConfig
|
|
|
|
|
|
|
|
func (cfg *SmtpEmailerConfig) UnmarshalJSON(data []byte) error {
|
|
|
|
smtpCfg := smtpEmailerConfig{}
|
|
|
|
err := json.Unmarshal(data, &smtpCfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if smtpCfg.Host == "" {
|
|
|
|
return errors.New("must set SMTP host")
|
|
|
|
}
|
2015-09-26 01:19:57 +05:30
|
|
|
if smtpCfg.Port == 0 {
|
2015-09-22 02:12:37 +05:30
|
|
|
return errors.New("must set SMTP port")
|
|
|
|
}
|
|
|
|
*cfg = SmtpEmailerConfig(smtpCfg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type smtpEmailer struct {
|
|
|
|
dialer *gomail.Dialer
|
2016-06-28 12:04:55 +05:30
|
|
|
from string
|
2015-09-22 02:12:37 +05:30
|
|
|
}
|
|
|
|
|
2016-06-28 12:04:55 +05:30
|
|
|
func (emailer *smtpEmailer) SendMail(subject, text, html string, to ...string) error {
|
2015-09-22 02:12:37 +05:30
|
|
|
msg := gomail.NewMessage()
|
2016-06-28 12:04:55 +05:30
|
|
|
msg.SetHeader("From", emailer.from)
|
2015-09-22 02:12:37 +05:30
|
|
|
msg.SetHeader("To", to...)
|
|
|
|
msg.SetHeader("Subject", subject)
|
|
|
|
msg.SetBody("text/plain", text)
|
|
|
|
msg.SetBody("text/html", html)
|
|
|
|
err := emailer.dialer.DialAndSend(msg)
|
|
|
|
if err != nil {
|
|
|
|
counterEmailSendErr.Add(1)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|