()
| 179 | } |
| 180 | |
| 181 | func (e *Email) dial() (*smtp.Client, error) { |
| 182 | host, port, err := net.SplitHostPort(e.smtpAddress) |
| 183 | if err != nil { |
| 184 | return nil, err |
| 185 | } |
| 186 | |
| 187 | // Always clone so we never mutate the caller's TLSConfig. |
| 188 | var tlsConfig *tls.Config |
| 189 | if e.TLSConfig == nil { |
| 190 | tlsConfig = &tls.Config{ServerName: host} |
| 191 | } else { |
| 192 | tlsConfig = e.TLSConfig.Clone() |
| 193 | if tlsConfig.ServerName == "" { |
| 194 | tlsConfig.ServerName = host |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | dialer := &net.Dialer{Timeout: e.DialTimeout} |
| 199 | |
| 200 | if port == "465" { |
| 201 | conn, err := tls.DialWithDialer(dialer, "tcp", e.smtpAddress, tlsConfig) |
| 202 | if err != nil { |
| 203 | return nil, err |
| 204 | } |
| 205 | c, err := smtp.NewClient(conn, host) |
| 206 | if err != nil { |
| 207 | conn.Close() |
| 208 | return nil, err |
| 209 | } |
| 210 | return c, nil |
| 211 | } |
| 212 | |
| 213 | conn, err := dialer.Dial("tcp", e.smtpAddress) |
| 214 | if err != nil { |
| 215 | return nil, err |
| 216 | } |
| 217 | c, err := smtp.NewClient(conn, host) |
| 218 | if err != nil { |
| 219 | conn.Close() |
| 220 | return nil, err |
| 221 | } |
| 222 | // Drive EHLO explicitly so we can surface its error. (*Client).Extension |
| 223 | // triggers a lazy hello() and swallows its error, which would silently |
| 224 | // treat a failed EHLO as "STARTTLS not advertised" and stay cleartext. |
| 225 | if err := c.Hello("localhost"); err != nil { |
| 226 | c.Close() |
| 227 | return nil, err |
| 228 | } |
| 229 | if ok, _ := c.Extension("STARTTLS"); ok { |
| 230 | if err := c.StartTLS(tlsConfig); err != nil { |
| 231 | c.Close() |
| 232 | return nil, err |
| 233 | } |
| 234 | } |
| 235 | return c, nil |
| 236 | } |
no outgoing calls