标签 smtp 下的文章

GO SMTP SSL链接出错问题

Golang自带了一个smtp模块,正常登录流程如下

hostname := fmt.Sprintf("%s:%s", host, port)  
domain := strings.Split(email, "@")[1]  
auth := smtp.PlainAuth("", email, password, domain)
smtpClient, err = smtp.Dial(hostname)  
if err != nil {  
   log.Println("Dial: ", err)  
   return false  
}
err = smtpClient.Auth(auth)  
if err != nil {  
   log.Println("Auth: ", err)  
   return false  
}

但是我发现在链接465即配置了SSL的smtp服务器会导致30秒之后直接返回一个EOF错误

最后通过翻阅smtp/smtp.go代码发现如下:

//默认使用该方法
func Dial(addr string) (*Client, error) {
    //先创建tcp链接
    conn, err := net.Dial("tcp", addr)
    if err != nil {
        return nil, err
    }
    host, _, _ := net.SplitHostPort(addr)
    //在进入NewClient
    return NewClient(conn, host)
}

// NewClient returns a new Client using an existing connection and host as a
// server name to be used when authenticating.
func NewClient(conn net.Conn, host string) (*Client, error) {
    text := textproto.NewConn(conn)
    _, _, err := text.ReadResponse(220)
    //其实这里已经失败了,所以返回一个EOF
    if err != nil {
        text.Close()
        return nil, err
    }
    c := &Client{Text: text, conn: conn, serverName: host, localName: "localhost"}
    _, c.tls = conn.(*tls.Conn)
    return c, nil
}

那么我们只需要创建一个tls.Dial,然后在通过tls的Conn去创建一个NewClient即可。

最后修复代码如下

func SmtpLogin(host, port, email, password string, ssl bool) bool {
    hostname := fmt.Sprintf("%s:%s", host, port)
    domain := strings.Split(email, "@")[1]
    auth := smtp.PlainAuth("", email, password, domain)
    var smtpClient *smtp.Client
    var err error

    if ssl {
        tlsconfig := &tls.Config{
            InsecureSkipVerify: true,
            ServerName:         host,
        }
        //创建一个tls链接
        if conn, err := tls.Dial("tcp", hostname, tlsconfig); err != nil {
            log.Println("tls.Dial: ", err)
            return false
        } else {
            smtpClient, err = smtp.NewClient(conn, domain)
        }

    } else {

        if tcpConn, err := net.Dial("tcp", hostname); err != nil {
            log.Println("net.Dial: ", err)
            return false
        } else {
            smtpClient, err = smtp.NewClient(tcpConn, domain)
        }
    }

    if err != nil {
        log.Println("smtp.NewClient: ", err)
        return false
    }

    err = smtpClient.Auth(auth)
    if err != nil {
        log.Println("Auth: ", err)
        return false
    }
    return true
}

参考资料

https://gist.github.com/jim3ma/b5c9edeac77ac92157f8f8affa290f45
https://bastengao.com/blog/2019/11/go-smtp-ssl.html
https://gist.github.com/chrisgillis/10888032
https://stackoverflow.com/questions/57063411/go-smtp-unable-to-send-email-through-gmail-getting-eof