自动发邮件功能也是自动化策划四项目的重要需求之一。例如,我们想在自动化脚本运行完成之后,邮箱就可以收到最新的测试报告结果。假设生成的测试报告与多人相关,每个人都去测试服务器查看就会比较麻烦,如果把这种自动的且不及时的查看变成被动且及时的查收,就方便多了。
SMTP(Simple Mail Transfer Protocol)是简单邮件传输协议,它是一组用于由于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式。
Python的smtplib模块提供了一种很方便的途径用来发送电子邮件。它对SMTP协议进行了简单的封装。我们可以使用SMTP对象的sendmail方法发送邮件,通过help()查看SMTP所提供的方法如下。
输出结果:
Help on class SMTP in module smtplib:
class SMTP(builtins.object)
| This class manages a connection to an SMTP or ESMTP server.
| SMTP Objects:
| SMTP objects have the following attributes:
| helo_resp
| This is the message given by the server in response to the
| most recent HELO command.
|
| ehlo_resp
| This is the message given by the server in response to the
| most recent EHLO command. This is usually multiline.
|
| does_esmtp
| This is a True value _after you do an EHLO command_, if the
| server supports ESMTP.
|
| esmtp_features
| This is a dictionary, which, if the server supports ESMTP,
| will _after you do an EHLO command_, contain the names of the
| SMTP service extensions this server supports, and their
| parameters (if any).
|
| Note, all extension names are mapped to lower case in the
| dictionary.
|
| See each method's docstrings for details. In general, there is a
| method of the same name to perform each SMTP command. There is also a
| method called 'sendmail' that will do an entire mail transaction.
|
| Methods defined here:
|
| __enter__(self)
|
| __exit__(self, *args)
|
| __init__(self, host='', port=0, local_hostname=None, timeout=<object object at 0x000000000037F0C0>, source_address=None)
| 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 returns anything other than a
| success code an SMTPConnectError is raised. If specified,
| `local_hostname` is used as the FQDN of the local host in the HELO/EHLO
| command. Otherwise, the local hostname is found using
| socket.getfqdn(). The `source_address` parameter takes a 2-tuple (host,
| port) for the socket to bind to as its source address before
| connecting. If the host is '' and port is 0, the OS default behavior
| will be used.
|
| auth(self, mechanism, authobject, *, initial_response_ok=True)
| Authentication command - requires response processing.
|
| 'mechanism' specifies which authentication mechanism is to
| be used - the valid values are those listed in the 'auth'
| element of 'esmtp_features'.
|
| 'authobject' must be a callable object taking a single argument:
|
| data = authobject(challenge)
|
| It will be called to process the server's challenge response; the
| challenge argument it is passed will be a bytes. It should return
| bytes data that will be base64 encoded and sent to the server.
|
| Keyword arguments:
| - initial_response_ok: Allow sending the RFC 4954 initial-response
| to the AUTH command, if the authentication methods supports it.
|
| auth_cram_md5(self, challenge=None)
| Authobject to use with CRAM-MD5 authentication. Requires self.user
| and self.password to be set.
|
| auth_login(self, challenge=None)
| Authobject to use with LOGIN authentication. Requires self.user and
| self.password to be set.
|
| auth_plain(self, challenge=None)
| Authobject to use with PLAIN authentication. Requires self.user and
| self.password to be set.
|
| close(self)
| Close the connection to the SMTP server.
|
| connect(self, host='localhost', port=