2020-09-20 19:01:24 +08:00
|
|
|
package email
|
2018-10-25 21:51:47 +08:00
|
|
|
|
|
|
|
import (
|
2020-05-17 23:37:48 +08:00
|
|
|
"crypto/tls"
|
2018-10-25 21:51:47 +08:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"net/smtp"
|
2020-05-31 23:46:56 +08:00
|
|
|
"net/textproto"
|
2024-05-18 20:19:24 +08:00
|
|
|
"strings"
|
2018-10-25 21:51:47 +08:00
|
|
|
|
2023-05-09 01:13:25 +08:00
|
|
|
"github.com/knadh/listmonk/models"
|
2020-05-17 01:11:30 +08:00
|
|
|
"github.com/knadh/smtppool"
|
2018-10-25 21:51:47 +08:00
|
|
|
)
|
|
|
|
|
2022-11-10 02:31:24 +08:00
|
|
|
const (
|
|
|
|
emName = "email"
|
|
|
|
hdrReturnPath = "Return-Path"
|
2024-05-18 20:19:24 +08:00
|
|
|
hdrBcc = "Bcc"
|
|
|
|
hdrCc = "Cc"
|
2022-11-10 02:31:24 +08:00
|
|
|
)
|
2018-10-25 21:51:47 +08:00
|
|
|
|
|
|
|
// Server represents an SMTP server's credentials.
|
|
|
|
type Server struct {
|
2020-05-31 23:46:56 +08:00
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
AuthProtocol string `json:"auth_protocol"`
|
2022-01-03 21:58:36 +08:00
|
|
|
TLSType string `json:"tls_type"`
|
2020-05-31 23:46:56 +08:00
|
|
|
TLSSkipVerify bool `json:"tls_skip_verify"`
|
|
|
|
EmailHeaders map[string]string `json:"email_headers"`
|
2020-05-17 01:11:30 +08:00
|
|
|
|
|
|
|
// Rest of the options are embedded directly from the smtppool lib.
|
|
|
|
// The JSON tag is for config unmarshal to work.
|
|
|
|
smtppool.Opt `json:",squash"`
|
|
|
|
|
|
|
|
pool *smtppool.Pool
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
2020-05-17 01:11:30 +08:00
|
|
|
// Emailer is the SMTP e-mail messenger.
|
|
|
|
type Emailer struct {
|
2020-07-08 19:00:14 +08:00
|
|
|
servers []*Server
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
2022-02-28 21:19:50 +08:00
|
|
|
// New returns an SMTP e-mail Messenger backend with the given SMTP servers.
|
2020-09-20 19:01:24 +08:00
|
|
|
func New(servers ...Server) (*Emailer, error) {
|
2020-05-17 01:11:30 +08:00
|
|
|
e := &Emailer{
|
2020-07-08 19:00:14 +08:00
|
|
|
servers: make([]*Server, 0, len(servers)),
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
2020-05-17 23:37:48 +08:00
|
|
|
for _, srv := range servers {
|
|
|
|
s := srv
|
2018-10-25 21:51:47 +08:00
|
|
|
var auth smtp.Auth
|
2020-04-01 22:26:40 +08:00
|
|
|
switch s.AuthProtocol {
|
|
|
|
case "cram":
|
2018-10-25 21:51:47 +08:00
|
|
|
auth = smtp.CRAMMD5Auth(s.Username, s.Password)
|
2020-04-01 22:26:40 +08:00
|
|
|
case "plain":
|
2018-10-25 21:51:47 +08:00
|
|
|
auth = smtp.PlainAuth("", s.Username, s.Password, s.Host)
|
2020-04-01 22:26:40 +08:00
|
|
|
case "login":
|
2020-05-17 02:08:19 +08:00
|
|
|
auth = &smtppool.LoginAuth{Username: s.Username, Password: s.Password}
|
2020-08-01 19:15:29 +08:00
|
|
|
case "", "none":
|
2020-04-01 22:26:40 +08:00
|
|
|
default:
|
2020-05-11 23:30:06 +08:00
|
|
|
return nil, fmt.Errorf("unknown SMTP auth type '%s'", s.AuthProtocol)
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
2020-05-17 01:11:30 +08:00
|
|
|
s.Opt.Auth = auth
|
2018-10-25 21:51:47 +08:00
|
|
|
|
2020-05-17 23:37:48 +08:00
|
|
|
// TLS config.
|
2022-01-03 21:58:36 +08:00
|
|
|
if s.TLSType != "none" {
|
2020-05-17 23:37:48 +08:00
|
|
|
s.TLSConfig = &tls.Config{}
|
|
|
|
if s.TLSSkipVerify {
|
|
|
|
s.TLSConfig.InsecureSkipVerify = s.TLSSkipVerify
|
|
|
|
} else {
|
|
|
|
s.TLSConfig.ServerName = s.Host
|
|
|
|
}
|
2022-01-03 21:58:36 +08:00
|
|
|
|
|
|
|
// SSL/TLS, not STARTTLS.
|
|
|
|
if s.TLSType == "TLS" {
|
|
|
|
s.Opt.SSL = true
|
|
|
|
}
|
2020-05-17 23:37:48 +08:00
|
|
|
}
|
|
|
|
|
2020-05-17 01:11:30 +08:00
|
|
|
pool, err := smtppool.New(s.Opt)
|
2018-10-25 21:51:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-17 01:11:30 +08:00
|
|
|
s.pool = pool
|
2020-07-08 19:00:14 +08:00
|
|
|
e.servers = append(e.servers, &s)
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the Server's name.
|
2020-05-17 01:11:30 +08:00
|
|
|
func (e *Emailer) Name() string {
|
2018-10-25 21:51:47 +08:00
|
|
|
return emName
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push pushes a message to the server.
|
2023-05-09 01:13:25 +08:00
|
|
|
func (e *Emailer) Push(m models.Message) error {
|
2018-10-25 21:51:47 +08:00
|
|
|
// If there are more than one SMTP servers, send to a random
|
|
|
|
// one from the list.
|
2020-07-08 19:00:14 +08:00
|
|
|
var (
|
|
|
|
ln = len(e.servers)
|
|
|
|
srv *Server
|
|
|
|
)
|
|
|
|
if ln > 1 {
|
|
|
|
srv = e.servers[rand.Intn(ln)]
|
2018-10-25 21:51:47 +08:00
|
|
|
} else {
|
2020-07-08 19:00:14 +08:00
|
|
|
srv = e.servers[0]
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:10:48 +08:00
|
|
|
// Are there attachments?
|
2020-05-17 01:11:30 +08:00
|
|
|
var files []smtppool.Attachment
|
2020-08-01 20:24:51 +08:00
|
|
|
if m.Attachments != nil {
|
|
|
|
files = make([]smtppool.Attachment, 0, len(m.Attachments))
|
|
|
|
for _, f := range m.Attachments {
|
2020-05-17 01:11:30 +08:00
|
|
|
a := smtppool.Attachment{
|
2019-07-18 15:10:48 +08:00
|
|
|
Filename: f.Name,
|
|
|
|
Header: f.Header,
|
|
|
|
Content: make([]byte, len(f.Content)),
|
|
|
|
}
|
|
|
|
copy(a.Content, f.Content)
|
|
|
|
files = append(files, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-17 01:11:30 +08:00
|
|
|
em := smtppool.Email{
|
2020-08-01 20:24:51 +08:00
|
|
|
From: m.From,
|
|
|
|
To: m.To,
|
|
|
|
Subject: m.Subject,
|
2019-07-18 15:10:48 +08:00
|
|
|
Attachments: files,
|
2020-04-10 06:27:39 +08:00
|
|
|
}
|
|
|
|
|
2020-08-01 20:24:51 +08:00
|
|
|
em.Headers = textproto.MIMEHeader{}
|
2022-11-10 02:31:24 +08:00
|
|
|
|
|
|
|
// Attach SMTP level headers.
|
|
|
|
for k, v := range srv.EmailHeaders {
|
|
|
|
em.Headers.Set(k, v)
|
|
|
|
}
|
|
|
|
|
2020-08-01 20:24:51 +08:00
|
|
|
// Attach e-mail level headers.
|
2022-11-10 02:31:24 +08:00
|
|
|
for k, v := range m.Headers {
|
|
|
|
em.Headers.Set(k, v[0])
|
2020-08-01 20:24:51 +08:00
|
|
|
}
|
|
|
|
|
2022-11-10 02:31:24 +08:00
|
|
|
// If the `Return-Path` header is set, it should be set as the
|
|
|
|
// the SMTP envelope sender (via the Sender field of the email struct).
|
|
|
|
if sender := em.Headers.Get(hdrReturnPath); sender != "" {
|
|
|
|
em.Sender = sender
|
|
|
|
em.Headers.Del(hdrReturnPath)
|
2020-05-31 23:46:56 +08:00
|
|
|
}
|
|
|
|
|
2024-05-18 20:19:24 +08:00
|
|
|
// If the `Bcc` header is set, it should be set on the Envelope
|
|
|
|
if bcc := em.Headers.Get(hdrBcc); bcc != "" {
|
|
|
|
for _, part := range strings.Split(bcc, ",") {
|
|
|
|
em.Bcc = append(em.Bcc, strings.TrimSpace(part))
|
|
|
|
}
|
|
|
|
em.Headers.Del(hdrBcc)
|
2024-08-30 15:54:45 +08:00
|
|
|
}
|
2024-05-18 20:19:24 +08:00
|
|
|
|
|
|
|
// If the `Cc` header is set, it should be set on the Envelope
|
|
|
|
if cc := em.Headers.Get(hdrCc); cc != "" {
|
|
|
|
for _, part := range strings.Split(cc, ",") {
|
|
|
|
em.Cc = append(em.Cc, strings.TrimSpace(part))
|
|
|
|
}
|
|
|
|
em.Headers.Del(hdrCc)
|
2024-08-30 15:54:45 +08:00
|
|
|
}
|
2024-05-18 20:19:24 +08:00
|
|
|
|
2021-01-30 17:29:21 +08:00
|
|
|
switch m.ContentType {
|
2020-04-10 06:27:39 +08:00
|
|
|
case "plain":
|
2021-01-30 17:29:21 +08:00
|
|
|
em.Text = []byte(m.Body)
|
2020-04-10 06:27:39 +08:00
|
|
|
default:
|
2020-08-01 20:24:51 +08:00
|
|
|
em.HTML = m.Body
|
2021-01-30 17:29:21 +08:00
|
|
|
if len(m.AltBody) > 0 {
|
|
|
|
em.Text = m.AltBody
|
|
|
|
}
|
2020-04-10 06:27:39 +08:00
|
|
|
}
|
2018-10-25 21:51:47 +08:00
|
|
|
|
2020-05-17 01:11:30 +08:00
|
|
|
return srv.pool.Send(em)
|
2018-10-25 21:51:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush flushes the message queue to the server.
|
2020-05-17 01:11:30 +08:00
|
|
|
func (e *Emailer) Flush() error {
|
2018-10-25 21:51:47 +08:00
|
|
|
return nil
|
|
|
|
}
|
2020-07-08 19:00:14 +08:00
|
|
|
|
|
|
|
// Close closes the SMTP pools.
|
|
|
|
func (e *Emailer) Close() error {
|
|
|
|
for _, s := range e.servers {
|
|
|
|
s.pool.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|