mail/internal/mail/smtp.go
func (d *Dialer) Dial() (SendCloser, error) {
	conn, err := NetDialTimeout("tcp", addr(d.Host, d.Port), d.Timeout)
	if err != nil {
		return nil, err
	}

	if d.SSL {
		conn = tlsClient(conn, d.tlsConfig())
	}

	c, err := smtpNewClient(conn, d.Host)
	if err != nil {
		return nil, err
	}

	if d.Timeout > 0 {
		conn.SetDeadline(time.Now().Add(d.Timeout))
	}

	if d.LocalName != "" {
		if err := c.Hello(d.LocalName); err != nil {
			return nil, err
		}
	}

	if !d.SSL && d.StartTLSPolicy != NoStartTLS {
		ok, _ := c.Extension("STARTTLS")
		if !ok && d.StartTLSPolicy == MandatoryStartTLS {
			err := StartTLSUnsupportedError{
				Policy: d.StartTLSPolicy}
			return nil, err
		}

		if ok {
			if err := c.StartTLS(d.tlsConfig()); err != nil {
				c.Close()
				return nil, err
			}
		}
	}

	if d.Auth == nil && d.Username != "" {
		if ok, auths := c.Extension("AUTH"); ok {
			if strings.Contains(auths, "CRAM-MD5") {
				d.Auth = smtp.CRAMMD5Auth(d.Username, d.Password)
			} else if strings.Contains(auths, "LOGIN") &&
				!strings.Contains(auths, "PLAIN") {
				d.Auth = &loginAuth{
					username: d.Username,
					password: d.Password,
					host:     d.Host,
				}
			} else {
				d.Auth = smtp.PlainAuth("", d.Username, d.Password, d.Host)
			}
		}
	}

	if d.Auth != nil {
		if err = c.Auth(d.Auth); err != nil {
			c.Close()
			return nil, err
		}
	}

	return &smtpSender{c, conn, d}, nil
}
func (d *Dialer) Dial() (SendCloser, error) {
	conn, err := NetDialTimeout("tcp", addr(d.Host, d.Port), d.Timeout)
	if err != nil {
		return nil, err
	}

	if d.SSL {
		conn = tlsClient(conn, d.tlsConfig())
	}

	c, err := smtpNewClient(conn, d.Host)
	if err != nil {
		return nil, err
	}

	if d.Timeout > 0 {
		conn.SetDeadline(time.Now().Add(d.Timeout))
	}

	if d.LocalName != "" {
		if err := c.Hello(d.LocalName); err != nil {
			return nil, err
		}
	}

	if !d.SSL && d.StartTLSPolicy != NoStartTLS {
		ok, _ := c.Extension("STARTTLS")
		if !ok && d.StartTLSPolicy == MandatoryStartTLS {
			err := StartTLSUnsupportedError{
				Policy: d.StartTLSPolicy}
			return nil, err
		}

		if ok {
			if err := c.StartTLS(d.tlsConfig()); err != nil {
				c.Close()
				return nil, err
			}
		}
	}

	if d.Auth == nil && d.Username != "" {
		if ok, auths := c.Extension("AUTH"); ok {
			if strings.Contains(auths, "CRAM-MD5") {
				d.Auth = smtp.CRAMMD5Auth(d.Username, d.Password)
			} else if strings.Contains(auths, "LOGIN") &&
				!strings.Contains(auths, "PLAIN") {
				d.Auth = &loginAuth{
					username: d.Username,
					password: d.Password,
					host:     d.Host,
				}
			} else {
				d.Auth = smtp.PlainAuth("", d.Username, d.Password, d.Host)
			}
		}
	}

	if d.Auth != nil {
		if err = c.Auth(d.Auth); err != nil {
			c.Close()
			return nil, err
		}
	}

	return &smtpSender{c, conn, d}, nil
}
func ( d * Dialer ) Dial ( ) ( SendCloser , error ) { conn , err := NetDialTimeout ( " " , addr ( d . Host , d . Port ) , d . Timeout ) if err != nil { return nil , err } if d . SSL { conn = tlsClient ( conn , d . tlsConfig ( ) ) } c , err := smtpNewClient ( conn , d . Host ) if err != nil { return nil , err } if d . Timeout > 0 { conn . SetDeadline ( time . Now ( ) . Add ( d . Timeout ) ) } if d . LocalName != " " { if err := c . Hello ( d . LocalName ) ; err != nil { return nil , err } } if ! d . SSL && d . StartTLSPolicy != NoStartTLS { ok , _ := c . Extension ( " " ) if ! ok && d . StartTLSPolicy == MandatoryStartTLS { err := StartTLSUnsupportedError { Policy : d . StartTLSPolicy } return nil , err } if ok { if err := c . StartTLS ( d . tlsConfig ( ) ) ; err != nil { c . Close ( ) return nil , err } } } if d . Auth == nil && d . Username != " " { if ok , auths := c . Extension ( " " ) ; ok { if strings . Contains ( auths , " " ) { d . Auth = smtp . CRAMMD5Auth ( d . Username , d . Password ) } else if strings . Contains ( auths , " " ) && ! strings . Contains ( auths , " " ) { d . Auth = & loginAuth { username : d . Username , password : d . Password , host : d . Host , } } else { d . Auth = smtp . PlainAuth ( " " , d . Username , d . Password , d . Host ) } } } if d . Auth != nil { if err = c . Auth ( d . Auth ) ; err != nil { c . Close ( ) return nil , err } } return & smtpSender { c , conn , d } , nil }
// Dial dials and authenticates to an SMTP server. The returned SendCloser
// should be closed when done using it.
Dial dials and authenticates to an SMTP server . The returned SendCloser should be closed when done using it .