Python Network Programming 简明教程

Python - Email Messages

电子邮件是一种服务,允许我们在互联网上以电子模式发送邮件。它提供了一种经济、高效且实时的信息分发方式。

Email is a service which allows us to send the message in electronic mode over the internet. It offers an efficient, inexpensive and real time mean of distributing information among people.

E-Mail Address

每个电子邮件用户都会为其电子邮件帐户分配一个唯一名称。此名称称为电子邮件地址。不同的用户可以根据电子邮件地址发送和接收邮件。

Each user of email is assigned a unique name for his email account. This name is known as E-mail address. Different users can send and receive messages according to the e-mail address.

电子邮件通常采用 username@domainname 形式。例如, webmaster@tutorialspoint.com 是一个电子邮件地址,其中 webmaster 是用户名,tutorialspoint.com 是域名。

E-mail is generally of the form username@domainname. For example, webmaster@tutorialspoint.com is an e-mail address where webmaster is username and tutorialspoint.com is domain name.

  1. The username and the domain name are separated by @ (at) symbol.

  2. E-mail addresses are not case sensitive.

  3. Spaces are not allowed in e-mail address.

电子邮件信息的前面五行被称为电子邮件头。头部部分包括以下字段:

The first five lines of an E-mail message is called E-mail header. The header part comprises of following fields:

  1. From

  2. Date

  3. To

  4. Subject

  5. CC

  6. BCC

From 字段表示发件人地址,即谁发送了电子邮件。

The From field indicates the sender’s address i.e. who sent the e-mail.

Date 字段表示发送电子邮件的日期。

The Date field indicates the date when the e-mail was sent.

To 字段表示收件人地址,即向谁发送了电子邮件。

The To field indicates the recipient’s address i.e. to whom the e-mail is sent.

Subject 字段表示电子邮件的目的。它应准确且切中要害。

The Subject field indicates the purpose of e-mail. It should be precise and to the point.

CC 代表抄送。它包括我们希望了解信息但并非明确的收件人的收件人地址。

CC stands for Carbon copy. It includes those recipient addresses whom we want to keep informed but not exactly the intended recipient.

BCC 代表密送。当我们不希望一位或多位收件人知道其他人在邮件中被抄送时,我们使用它。

BCC stands for Black Carbon Copy. It is used when we do not want one or more of the recipients to know that someone else was copied on the message.

问候是实际信息的第一句话。例如。尊敬的先生或各位、等等。

Greeting is the opening of the actual message. Eg. Hi Sir or Hi Guys etc.

它表示消息的实际内容。

It represents the actual content of the message.

这是电子邮件消息的最后部分。它包括发件人的姓名、地址和联系电话。

This is the final part of an e-mail message. It includes Name of Sender, Address, and Contact Number.

Python 有 EmailMessage 类,可以用来构建电子邮件消息。此类具有对电子邮件消息不同部分进行自定义所需的函数,例如 TO 和 FROM 标记、主题行以及电子邮件的内容。

Python has EmailMessage class which can be used build email messages. This class ahs the required methods to customize different parts of the email message like - the TO and FROM tags, the Subject Line as well as the content of the email.

Example

在下面的示例中,我们创建了包含电子邮件所有必要部分的电子邮件消息。一旦我们打印出该消息的内容,我们就可以看到完整的电子邮件。

In the below example we create an email message with all the necessary parts of an email. Once we print out the content of the message we can see the complete email.

import email.message, email.policy, email.utils, sys
text = """Welcome to TutorialsPoint - Simple Easy Learning"""

message = email.message.EmailMessage(email.policy.SMTP)
message['To'] = 'you@yourdomain.com'
message['From'] = 'Learn '
message['Subject'] = 'A mail To you'
message['Date'] = email.utils.formatdate(localtime=True)
message['Message-ID'] = email.utils.make_msgid()
message.set_content(text)
sys.stdout.buffer.write(message.as_bytes())