@@ -83,6 +83,17 @@ func isVerifyError(err error) bool {
8383// - tlsLevel TLS security level that was estabilished.
8484// - tlsErr Error that prevented TLS from working if tlsLevel != TLSAuthenticated
8585func (rd * remoteDelivery ) connect (ctx context.Context , conn mxConn , host string , tlsCfg * tls.Config ) (tlsLevel module.TLSLevel , tlsErr , err error ) {
86+ return rd .connectPort (ctx , conn , host , smtpPort , tlsCfg )
87+ }
88+
89+ // connectPort attempts to connect to the MX, first trying STARTTLS with X.509
90+ // verification but falling back to unauthenticated TLS or plaintext as
91+ // necessary.
92+ //
93+ // Return values:
94+ // - tlsLevel TLS security level that was estabilished.
95+ // - tlsErr Error that prevented TLS from working if tlsLevel != TLSAuthenticated
96+ func (rd * remoteDelivery ) connectPort (ctx context.Context , conn mxConn , host string , port string , tlsCfg * tls.Config ) (tlsLevel module.TLSLevel , tlsErr , err error ) {
8697 tlsLevel = module .TLSAuthenticated
8798 if rd .rt .tlsConfig != nil {
8899 tlsCfg = rd .rt .tlsConfig .Clone ()
@@ -96,7 +107,7 @@ retry:
96107 // TLS errors separately hence starttls=false.
97108 _ , err = conn .Connect (ctx , config.Endpoint {
98109 Host : host ,
99- Port : smtpPort ,
110+ Port : port ,
100111 }, false , nil )
101112 if err != nil {
102113 return module .TLSNone , nil , err
@@ -151,6 +162,10 @@ retry:
151162}
152163
153164func (rd * remoteDelivery ) attemptMX (ctx context.Context , conn * mxConn , record * net.MX ) error {
165+ return rd .attemptMXWithPort (ctx , conn , record , smtpPort )
166+ }
167+
168+ func (rd * remoteDelivery ) attemptMXWithPort (ctx context.Context , conn * mxConn , record * net.MX , port string ) error {
154169 mxLevel := module .MXNone
155170
156171 connCtx , cancel := context .WithCancel (ctx )
@@ -169,7 +184,7 @@ func (rd *remoteDelivery) attemptMX(ctx context.Context, conn *mxConn, record *n
169184 p .PrepareConn (ctx , record .Host )
170185 }
171186
172- tlsLevel , tlsErr , err := rd .connect (connCtx , * conn , record .Host , rd .rt .tlsConfig )
187+ tlsLevel , tlsErr , err := rd .connectPort (connCtx , * conn , record .Host , port , rd .rt .tlsConfig )
173188 if err != nil {
174189 return err
175190 }
@@ -316,7 +331,12 @@ func (rd *remoteDelivery) newConn(ctx context.Context, domain string) (*mxConn,
316331 conn .dnssecOk = dnssecOk
317332
318333 var lastErr error
334+ ports := rd .rt .smtpPorts
335+ if len (ports ) == 0 {
336+ ports = []string {smtpPort }
337+ }
319338 region = trace .StartRegion (ctx , "remote/Connect+TLS" )
339+ recordsLoop:
320340 for _ , record := range records {
321341 if record .Host == "." {
322342 return nil , & exterrors.SMTPError {
@@ -326,14 +346,16 @@ func (rd *remoteDelivery) newConn(ctx context.Context, domain string) (*mxConn,
326346 }
327347 }
328348
329- if err := rd .attemptMX (ctx , & conn , record ); err != nil {
330- if len (records ) != 0 {
331- rd .Log .Error ("cannot use MX" , err , "remote_server" , record .Host , "domain" , domain )
349+ for _ , port := range ports {
350+ if err := rd .attemptMXWithPort (ctx , & conn , record , port ); err != nil {
351+ if len (records ) != 0 {
352+ rd .Log .Error ("cannot use MX" , err , "remote_server" , record .Host , "remote_port" , port , "domain" , domain )
353+ }
354+ lastErr = err
355+ continue
332356 }
333- lastErr = err
334- continue
357+ break recordsLoop
335358 }
336- break
337359 }
338360 region .End ()
339361
0 commit comments