Allow config to have multiple remotes.

This will enable development teams to test emails from
a system like mailcatcher while also having the email
delivered to the indented mailbox.
This commit is contained in:
Mark Gardner
2022-02-25 07:37:12 -06:00
parent 4f1148d77b
commit 4221919689
8 changed files with 239 additions and 112 deletions

29
smtp.go
View File

@@ -322,7 +322,11 @@ var testHookStartTLS func(*tls.Config) // nil, except for tests
// attachments (see the mime/multipart package), or other mail
// functionality. Higher-level packages exist outside of the standard
// library.
func SendMail(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
func SendMail(r *Remote, from string, to []string, msg []byte) error {
if r.Sender != "" {
from = r.Sender
}
if err := validateLine(from); err != nil {
return err
}
@@ -331,22 +335,19 @@ func SendMail(addr string, a smtp.Auth, from string, to []string, msg []byte) er
return err
}
}
host, port, err := net.SplitHostPort(addr)
if err != nil {
return err
}
var c *Client
if port == "465" || port == "smtps" {
var err error
if r.Scheme == "smtps" {
config := &tls.Config{
ServerName: host,
InsecureSkipVerify: *remoteSkipVerify,
ServerName: r.Hostname,
InsecureSkipVerify: r.SkipVerify,
}
conn, err := tls.Dial("tcp", addr, config)
conn, err := tls.Dial("tcp", r.Addr, config)
if err != nil {
return err
}
defer conn.Close()
c, err = NewClient(conn, host)
c, err = NewClient(conn, r.Hostname)
if err != nil {
return err
}
@@ -354,7 +355,7 @@ func SendMail(addr string, a smtp.Auth, from string, to []string, msg []byte) er
return err
}
} else {
c, err = Dial(addr)
c, err = Dial(r.Addr)
if err != nil {
return err
}
@@ -365,7 +366,7 @@ func SendMail(addr string, a smtp.Auth, from string, to []string, msg []byte) er
if ok, _ := c.Extension("STARTTLS"); ok {
config := &tls.Config{
ServerName: c.serverName,
InsecureSkipVerify: *remoteSkipVerify,
InsecureSkipVerify: r.SkipVerify,
}
if testHookStartTLS != nil {
testHookStartTLS(config)
@@ -375,11 +376,11 @@ func SendMail(addr string, a smtp.Auth, from string, to []string, msg []byte) er
}
}
}
if a != nil && c.ext != nil {
if r.Auth != nil && c.ext != nil {
if _, ok := c.ext["AUTH"]; !ok {
return errors.New("smtp: server doesn't support AUTH")
}
if err = c.Auth(a); err != nil {
if err = c.Auth(r.Auth); err != nil {
return err
}
}