Initialize a new instance. If specified, `host` is the name of the remote host to which to connect. If specified, `port` specifies the port to which to connect. By default, smtplib.SMTP_PORT is used. If a host is specified the connect method is called, and if it re
(self, host='', port=0, local_hostname=None,
timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
source_address=None)
| 234 | default_port = SMTP_PORT |
| 235 | |
| 236 | def __init__(self, host='', port=0, local_hostname=None, |
| 237 | timeout=socket._GLOBAL_DEFAULT_TIMEOUT, |
| 238 | source_address=None): |
| 239 | """Initialize a new instance. |
| 240 | |
| 241 | If specified, `host` is the name of the remote host to which to |
| 242 | connect. If specified, `port` specifies the port to which to connect. |
| 243 | By default, smtplib.SMTP_PORT is used. If a host is specified the |
| 244 | connect method is called, and if it returns anything other than a |
| 245 | success code an SMTPConnectError is raised. If specified, |
| 246 | `local_hostname` is used as the FQDN of the local host in the HELO/EHLO |
| 247 | command. Otherwise, the local hostname is found using |
| 248 | socket.getfqdn(). The `source_address` parameter takes a 2-tuple (host, |
| 249 | port) for the socket to bind to as its source address before |
| 250 | connecting. If the host is '' and port is 0, the OS default behavior |
| 251 | will be used. |
| 252 | |
| 253 | """ |
| 254 | self._host = host |
| 255 | self.timeout = timeout |
| 256 | self.esmtp_features = {} |
| 257 | self.command_encoding = 'ascii' |
| 258 | self.source_address = source_address |
| 259 | self._auth_challenge_count = 0 |
| 260 | |
| 261 | if host: |
| 262 | (code, msg) = self.connect(host, port) |
| 263 | if code != 220: |
| 264 | self.close() |
| 265 | raise SMTPConnectError(code, msg) |
| 266 | if local_hostname is not None: |
| 267 | self.local_hostname = local_hostname |
| 268 | else: |
| 269 | # RFC 2821 says we should use the fqdn in the EHLO/HELO verb, and |
| 270 | # if that can't be calculated, that we should use a domain literal |
| 271 | # instead (essentially an encoded IP address like [A.B.C.D]). |
| 272 | fqdn = socket.getfqdn() |
| 273 | if '.' in fqdn: |
| 274 | self.local_hostname = fqdn |
| 275 | else: |
| 276 | # We can't find an fqdn hostname, so use a domain literal |
| 277 | addr = '127.0.0.1' |
| 278 | try: |
| 279 | addr = socket.gethostbyname(socket.gethostname()) |
| 280 | except socket.gaierror: |
| 281 | pass |
| 282 | self.local_hostname = '[%s]' % addr |
| 283 | |
| 284 | def __enter__(self): |
| 285 | return self |
no test coverage detected