Python Network Programming 简明教程
Python - SMTP
简单邮件传输协议 (SMTP) 是一种协议,负责发送电子邮件和在邮件服务器之间传递电子邮件。
Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending an e-mail and routing e-mail between mail servers.
Python 提供 smtplib 模块,该模块定义了一个 SMTP 客户端会话对象,可以用来向带有 SMTP 或 ESMTP 侦听程序守护进程的任何互联网计算机发送邮件。
Python provides smtplib module, which defines an SMTP client session object that can be used to send mails to any Internet machine with an SMTP or ESMTP listener daemon.
SMTP 对象有一个名为 sendmail 的实例函数,它通常用于处理邮件发送工作。它采用三个参数:
An SMTP object has an instance method called sendmail, which is typically used to do the work of mailing a message. It takes three parameters −
-
The sender − A string with the address of the sender.
-
The receivers − A list of strings, one for each recipient.
-
The message − A message as a string formatted as specified in the various RFCs.
Example
这里有使用 Python 脚本发送一封电子邮件的简单方法。不妨试一下:
Here is a simple way to send one e-mail using Python script. Try it once −
#!/usr/bin/python3
import smtplib
sender = 'from@fromdomain.com'
receivers = ['to@todomain.com']
message = """From: From Person <from@fromdomain.com>
To: To Person <to@todomain.com>
Subject: SMTP e-mail test
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
这里,你使用三重引号在消息中放置了一封简单的电子邮件,注意正确设置标题格式。一封电子邮件需要一个 From 、 To 和 Subject 标题,用空行将这些标题与电子邮件正文分隔。
Here, you have placed a basic e-mail in message, using a triple quote, taking care to format the headers correctly. An e-mail requires a From, To, and a Subject header, separated from the body of the e-mail with a blank line.
若要发送邮件,使用 smtpObj 连接到本地计算机上的 SMTP 服务器。然后将 sendmail 方法与消息、发件人地址和收件人地址一起用作参数(即使发件人地址和收件人地址包含在电子邮件本身中,它们并不总是用来转发邮件)。
To send the mail you use smtpObj to connect to the SMTP server on the local machine. Then use the sendmail method along with the message, the from address, and the destination address as parameters (even though the from and to addresses are within the e-mail itself, these are not always used to route the mail).
如果你没有在本地计算机上运行 SMTP 服务器,可以使用 smtplib 客户端与远程 SMTP 服务器通信。除非你使用的是电子邮件服务(如 gmail 或 Yahoo! 邮件),否则你的电子邮件提供商必须向你提供了发送邮件服务器详细信息,你可以向他们提供信息,如下所示:
If you are not running an SMTP server on your local machine, you can use smtplib client to communicate with a remote SMTP server. Unless you are using a webmail service (such as gmail or Yahoo! Mail), your e-mail provider must have provided you with the outgoing mail server details that you can supply them, as follows −
mail = smtplib.SMTP('smtp.gmail.com', 587)
Sending an HTML e-mail using Python
当你使用 Python 发送文本消息时,所有内容都被视为简单文本。即使你在文本消息中包含 HTML 标记,它也会显示为简单文本,并且 HTML 标记不会根据 HTML 语法进行格式设置。但是,Python 提供了一个将其发送为实际 HTML 消息的选项。
When you send a text message using Python, then all the content is treated as simple text. Even if you include HTML tags in a text message, it is displayed as simple text and HTML tags will not be formatted according to the HTML syntax. However, Python provides an option to send an HTML message as actual HTML message.
在发送电子邮件消息时,你可以指定 Mime 版本、内容类型和字符集以发送 HTML 电子邮件。
While sending an e-mail message, you can specify a Mime version, content type and the character set to send an HTML e-mail.
Example
以下是将 HTML 内容作为电子邮件发送的示例。不妨试一下:
Following is an example to send the HTML content as an e-mail. Try it once −
#!/usr/bin/python3
import smtplib
message = """From: From Person <from@fromdomain.com>
To: To Person <to@todomain.com>
MIME-Version: 1.0
Content-type: text/html
Subject: SMTP HTML e-mail test
This is an e-mail message to be sent in HTML format
<b>This is HTML message.</b>
<h1>This is headline.</h1>
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"