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