diff --git a/mail/mailer.go b/mail/mailer.go index 864cdbd51..21b2417f4 100644 --- a/mail/mailer.go +++ b/mail/mailer.go @@ -21,10 +21,14 @@ import ( "time" "github.com/jmhodges/clock" + "golang.org/x/net/context" "github.com/prometheus/client_golang/prometheus" "github.com/letsencrypt/boulder/core" + "github.com/letsencrypt/boulder/bdns" blog "github.com/letsencrypt/boulder/log" + berrors "github.com/letsencrypt/boulder/errors" + "github.com/letsencrypt/boulder/probs" ) type idGenerator interface { @@ -122,6 +126,7 @@ func New( username, password string, rootCAs *x509.CertPool, + resolver bdns.Client, from mail.Address, logger blog.Logger, stats prometheus.Registerer, @@ -141,6 +146,7 @@ func New( server: server, port: port, rootCAs: rootCAs, + dnsClient: resolver, }, log: logger, from: from, @@ -181,7 +187,7 @@ func (m *MailerImpl) generateMessage(to []string, subject, body string) ([]byte, fmt.Sprintf("To: %s", strings.Join(addrs, ", ")), fmt.Sprintf("From: %s", m.from.String()), fmt.Sprintf("Subject: %s", subject), - fmt.Sprintf("Date: %s", now.Format(time.RFC822)), + fmt.Sprintf("Date: %s", now.Format(time.RFC1123Z)), fmt.Sprintf("Message-Id: <%s.%s.%s>", now.Format("20060102T150405"), mid.String(), m.from.Address), "MIME-Version: 1.0", "Content-Type: text/plain; charset=UTF-8", @@ -238,23 +244,32 @@ func (m *MailerImpl) Connect() error { type dialerImpl struct { username, password, server, port string rootCAs *x509.CertPool + dnsClient bdns.Client } func (di *dialerImpl) Dial() (smtpClient, error) { - hostport := net.JoinHostPort(di.server, di.port) - var conn net.Conn - var err error - conn, err = tls.Dial("tcp", hostport, &tls.Config{ - RootCAs: di.rootCAs, - }) + deadline := time.Now().Add(30 * time.Second) + ctx, cancel := context.WithDeadline(context.Background(), deadline) + defer cancel() + + addrs, err := di.dnsClient.LookupHost(ctx, di.server) if err != nil { - return nil, err + problem := probs.DNS("%v") + return nil, problem } - client, err := smtp.NewClient(conn, di.server) + if len(addrs) == 0 { + return nil, berrors.DNSError("No valid IP addresses found for %s", di.server) + } + + hostport := net.JoinHostPort(addrs[0].String(), di.port) + client, err := smtp.Dial(hostport) if err != nil { return nil, err } - auth := smtp.PlainAuth("", di.username, di.password, di.server) + client.StartTLS( &tls.Config{ + ServerName: di.server, + }) + auth := smtp.PlainAuth("", di.username, di.password, addrs[0].String()) if err = client.Auth(auth); err != nil { return nil, err }