try to send emails over TLS first

This commit is contained in:
Aine
2022-09-08 09:52:58 +03:00
parent d434edd930
commit 9a121b6ed5

View File

@@ -27,6 +27,9 @@ type mta struct {
log *logger.Logger log *logger.Logger
} }
// SMTPAddrs priority list
var SMTPAddrs = []string{":465", ":587", ":25"}
func NewMTA(loglevel string) utils.MTA { func NewMTA(loglevel string) utils.MTA {
return &mta{ return &mta{
log: logger.New("smtp/mta.", loglevel), log: logger.New("smtp/mta.", loglevel),
@@ -70,16 +73,16 @@ func (m *mta) Send(from, to, data string) error {
return nil return nil
} }
func (m *mta) tryServer(localname, mxhost string) *smtp.Client { func (m *mta) tryServer(localname, mxhost, addr string) *smtp.Client {
m.log.Debug("trying SMTP connection to %s", mxhost) m.log.Debug("trying SMTP connection to %s%s", mxhost, addr)
conn, err := smtp.Dial(mxhost + ":smtp") conn, err := smtp.Dial(mxhost + addr)
if err != nil { if err != nil {
m.log.Warn("cannot connect to the %s: %v", mxhost, err) m.log.Warn("cannot connect to the %s%s: %v", mxhost, addr, err)
return nil return nil
} }
err = conn.Hello(localname) err = conn.Hello(localname)
if err != nil { if err != nil {
m.log.Warn("cannot call HELLO command of the %s: %v", mxhost, err) m.log.Warn("cannot call HELLO command of the %s%s: %v", mxhost, addr, err)
return nil return nil
} }
if ok, _ := conn.Extension("STARTTLS"); ok { if ok, _ := conn.Extension("STARTTLS"); ok {
@@ -106,18 +109,22 @@ func (m *mta) connect(from, to string) (*smtp.Client, error) {
} }
for _, mx := range mxs { for _, mx := range mxs {
client := m.tryServer(localname, mx.Host) for _, addr := range SMTPAddrs {
if client != nil { client := m.tryServer(localname, strings.TrimSuffix(mx.Host, "."), addr)
return client, nil if client != nil {
return client, nil
}
} }
} }
// If there are no MX records, according to https://datatracker.ietf.org/doc/html/rfc5321#section-5.1, // If there are no MX records, according to https://datatracker.ietf.org/doc/html/rfc5321#section-5.1,
// we're supposed to try talking directly to the host. // we're supposed to try talking directly to the host.
if len(mxs) == 0 { if len(mxs) == 0 {
client := m.tryServer(localname, hostname) for _, addr := range SMTPAddrs {
if client != nil { client := m.tryServer(localname, hostname, addr)
return client, nil if client != nil {
return client, nil
}
} }
} }