try to send emails over TLS first
This commit is contained in:
21
smtp/mta.go
21
smtp/mta.go
@@ -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,20 +109,24 @@ 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 {
|
||||||
|
client := m.tryServer(localname, strings.TrimSuffix(mx.Host, "."), addr)
|
||||||
if client != nil {
|
if client != nil {
|
||||||
return 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 {
|
||||||
|
client := m.tryServer(localname, hostname, addr)
|
||||||
if client != nil {
|
if client != nil {
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf("target SMTP server not found")
|
return nil, fmt.Errorf("target SMTP server not found")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user