From 63ae6fcf3028f21736fc79ccdb021ab1e237b881 Mon Sep 17 00:00:00 2001 From: wzshiming Date: Mon, 3 Jun 2019 23:39:09 +0800 Subject: [PATCH] Add ProxyDial to specify optional dialing to establish a transport connection. --- smtp.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/smtp.go b/smtp.go index 547e04d..5793ec9 100644 --- a/smtp.go +++ b/smtp.go @@ -1,6 +1,7 @@ package mail import ( + "context" "crypto/tls" "fmt" "io" @@ -12,6 +13,9 @@ import ( // A Dialer is a dialer to an SMTP server. type Dialer struct { + // ProxyDial specifies the optional dial function for + // establishing the transport connection. + DialProxy func(ctx context.Context, network, address string) (net.Conn, error) // Host represents the host of the SMTP server. Host string // Port represents the port of the SMTP server. @@ -53,6 +57,7 @@ type Dialer struct { // to the SMTP server. func NewDialer(host string, port int, username, password string) *Dialer { return &Dialer{ + DialProxy: (&net.Dialer{}).DialContext, Host: host, Port: port, Username: username, @@ -79,7 +84,8 @@ var NetDialTimeout = net.DialTimeout // Dial dials and authenticates to an SMTP server. The returned SendCloser // should be closed when done using it. func (d *Dialer) Dial() (SendCloser, error) { - conn, err := NetDialTimeout("tcp", addr(d.Host, d.Port), d.Timeout) + ctx, _ := context.WithTimeout(context.Background(), d.Timeout) + conn, err := d.DialProxy(ctx, "tcp", addr(d.Host, d.Port)) if err != nil { return nil, err }