Python 邮件模块2024 年 8 月 29 日 | 阅读 16 分钟 电子邮件包是一个电子邮件消息管理库。电子邮件包的整体结构可以分解为三个基本组件,外加一个调节其他组件行为的第四个组件。 该包的主要组件是表示电子邮件消息的“对象模型”。应用程序与该包交互的主要接口是消息子模块中描述的对象模型接口。生成器将 EmailMessage 转换回序列化的字节流。(解析器和生成器也可以处理文本字符流,但这不被推荐,因为很容易出现无效的消息。) 策略模块是控制组件。每个 EmailMessage、生成器和解析器都有一个策略对象来控制其行为。策略通常仅在创建 EmailMessage 时指定,无论是通过直接实例化 EmailMessage 来创建新电子邮件,还是通过解析器解析输入流。但是,当使用生成器序列化消息时,可以更改策略。例如,这允许从磁盘处理一个通用的电子邮件消息,但在使用典型的 SMTP 设置发送到邮件服务器时进行序列化。 电子邮件包将应用程序对各种控制 RFC 的了解降到最低。理论上,程序应该能够将电子邮件消息视为 Unicode 文本和二进制附件的层次化树,而无需担心如何将它们序列化。但在实践中,通常需要至少了解一些控制 MIME 消息及其结构的规则,特别是 MIME“内容类型”的名称和性质,以及它们如何标识多部分文档。 本文简要介绍了电子邮件模块的主要功能。然后讨论策略控件,从而结束对库主要组件的处理。 接下来讨论标题注册表和内容管理器子组件,它们分别提供更详细的标题和有效负载操作功能。这两个组件都提供了处理和创建非平凡消息的功能,并且为其可扩展 API 提供了文档,这将对高级应用程序非常有用。 以上代表了电子邮件包的现代(Unicode 友好)API。从 Message 类开始,后续部分描述了历史 compat32 API,该 API 与电子邮件消息的表示方式的复杂性更为紧密地相关。 Python 内置的 smtplib 模块允许您使用简单邮件传输协议 (SMTP) 发送电子邮件。对于 SMTP,smtplib 使用 RFC 821 协议。本文示例将使用 Gmail SMTP 服务器,但同样的概念也适用于其他电子邮件服务。尽管大多数电子邮件提供商使用的连接端口与本教程中描述的相同,但您可以通过简单的 Google 搜索来核实您的端口。 本地 SMTP 调试服务器可以帮助您在发送任何电子邮件之前排除电子邮件故障并确保您的电子邮件操作没有错误。为此指南设置一个用于开发的 Gmail 帐户或一个会拒绝电子邮件并将它们打印到命令提示符的 SMTP 调试服务器。下表概述了这两种可能性。 现在让我们看一个示例代码 代码输出 nirnay@superbook:~$ python3 mail1.py Please choose one of the below-listed options:: 1. To enter the details of the sender. [Sender email and sender Password] 2. To enter the details of the receiver. [Receiver email] 3. To enter the sending email's subject. 4. To enter the body of the sending email. 5. To send the email. 6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body. 7. To exit from the code execution. 1 Enter the email of the sender:: [email protected] Enter the Password of the sender:: S3cur3P@ssw0rd wanna proceed with the program or want to exit the program [Y/n] y Please choose one of the below-listed options:: 1. To enter the details of the sender. [Sender email and sender Password] 2. To enter the details of the receiver. [Receiver email] 3. To enter the sending email's subject. 4. To enter the body of the sending email. 5. To send the email. 6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body. 7. To exit from the code execution. 2 Enter the email of the receiver:: [email protected] wanna proceed with the program or want to exit the program [Y/n] y Please choose one of the below-listed options:: 1. To enter the details of the sender. [Sender email and sender Password] 2. To enter the details of the receiver. [Receiver email] 3. To enter the sending email's subject. 4. To enter the body of the sending email. 5. To send the email. 6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body. 7. To exit from the code execution. 3 Enter the Subject of the sending email:: Test EMail Sub Enter the Body of the sending email:: Hi message from Nirnay wanna proceed with the program or want to exit the program [Y/n] y Please choose one of the below-listed options:: 1. To enter the details of the sender. [Sender email and sender Password] 2. To enter the details of the receiver. [Receiver email] 3. To enter the sending email's subject. 4. To enter the body of the sending email. 5. To send the email. 6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body. 7. To exit from the code execution. 4 Enter the Subject of the sending email:: Test Email Subject Enter the Body of the sending email:: Hi message from Nirnay. wanna proceed with the program or want to exit the program [Y/n] y Please choose one of the below-listed options:: 1. To enter the details of the sender. [Sender email and sender Password] 2. To enter the details of the receiver. [Receiver email] 3. To enter the sending email's subject. 4. To enter the body of the sending email. 5. To send the email. 6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body. 7. To exit from the code execution. 6 Sender Mail:: [email protected] Receiver Mail:: [email protected] Email Subject:: Test Email Subject Email Body:: Hi message from Nirnay. wanna proceed with the program or want to exit the program [Y/n] y Please choose one of the below-listed options:: 1. To enter the details of the sender. [Sender email and sender Password] 2. To enter the details of the receiver. [Receiver email] 3. To enter the sending email's subject. 4. To enter the body of the sending email. 5. To send the email. 6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body. 7. To exit from the code execution. 5 Email sent successfully. wanna proceed with the program or want to exit the program [Y/n] y Please choose one of the below-listed options:: 1. To enter the details of the sender. [Sender email and sender Password] 2. To enter the details of the receiver. [Receiver email] 3. To enter the sending email's subject. 4. To enter the body of the sending email. 5. To send the email. 6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body. 7. To exit from the code execution. 7 说明 在上述代码的输出中,我们已执行了程序。执行代码后,用户会看到七个不同的选项,代表了代码的七种不同功能。通过多选项菜单向用户提供的各种功能包括输入发件人详细信息(即发件人电子邮件)、获取收件人的电子邮件地址、输入电子邮件的主题和正文,以及最后最重要的一项功能是发送电子邮件,为此已输入所有详细信息。还有一个选项可以打印所有输入的详细信息,例如将用于发送邮件的发件人凭据、收件人电子邮件、电子邮件主题和正文;通过此选项,用户可以验证输入的详细信息,如果发现任何错误,用户可以通过重新输入该详细信息轻松纠正。最终让用户不必发送任何错误数据。在完成所有操作后,通过选择将退出程序的最后一个选项来退出代码执行。 假设您想随 Python 代码发送附件。相应的代码如下: 代码输出 nirnay@superbook:~$ python3 mail2.py Please choose one of the below-listed options:: 1. To enter the details of the sender. [Sender email and sender Password] 2. To enter the details of the receiver. [Receiver email] 3. To enter the Subject and Body of the sending email. 4. To enter the attachment for the sending email. 5. To send the email. 6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body. 7. To exit from the code execution. 1 Enter the email of the sender:: [email protected] Enter the Password of the sender:: MyP@ssw0rd wanna proceed with the program or want to exit the program [Y/n] y Please choose one of the below-listed options:: 1. To enter the details of the sender. [Sender email and sender Password] 2. To enter the details of the receiver. [Receiver email] 3. To enter the Subject and Body of the sending email. 4. To enter the attachment for the sending email. 5. To send the email. 6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body. 7. To exit from the code execution. 2 Enter the email of the receiver:: [email protected] wanna proceed with the program or want to exit the program [Y/n] y Please choose one of the below-listed options:: 1. To enter the details of the sender. [Sender email and sender Password] 2. To enter the details of the receiver. [Receiver email] 3. To enter the Subject and Body of the sending email. 4. To enter the attachment for the sending email. 5. To send the email. 6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body. 7. To exit from the code execution. 3 Enter the Subject of the sending email:: Test Email Enter the Body of the sending email:: This is the body of the sending email by Nirnay !!! wanna proceed with the program or want to exit the program [Y/n] y Please choose one of the below-listed options:: 1. To enter the details of the sender. [Sender email and sender Password] 2. To enter the details of the receiver. [Receiver email] 3. To enter the Subject and Body of the sending email. 4. To enter the attachment for the sending email. 5. To send the email. 6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body. 7. To exit from the code execution. 4 Enter the path of the attachment for the sending email:: file_to_attach.txt wanna proceed with the program or want to exit the program [Y/n] y Please choose one of the below-listed options:: 1. To enter the details of the sender. [Sender email and sender Password] 2. To enter the details of the receiver. [Receiver email] 3. To enter the Subject and Body of the sending email. 4. To enter the attachment for the sending email. 5. To send the email. 6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body. 7. To exit from the code execution. 6 Sender Mail:: [email protected] Receiver Mail:: [email protected] Email Subject:: Test Email Email Body:: This is the body of the sending email by Nirnay !!! wanna proceed with the program or want to exit the program [Y/n] y Please choose one of the below-listed options:: 1. To enter the details of the sender. [Sender email and sender Password] 2. To enter the details of the receiver. [Receiver email] 3. To enter the Subject and Body of the sending email. 4. To enter the attachment for the sending email. 5. To send the email. 6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body. 7. To exit from the code execution. 5 Email sent successfully. wanna proceed with the program or want to exit the program [Y/n] y Please choose one of the below-listed options:: 1. To enter the details of the sender. [Sender email and sender Password] 2. To enter the details of the receiver. [Receiver email] 3. To enter the Subject and Body of the sending email. 4. To enter the attachment for the sending email. 5. To send the email. 6. To verify all the entered details. [Sender email, Sender Password, Receiver email, Email Subject and Email Body. 7. To exit from the code execution. 7 说明 在上述代码的输出中,我们已执行了程序。执行代码后,用户会看到七个不同的选项,代表了代码的七种不同功能。通过多选项菜单向用户提供的各种功能包括输入发件人详细信息(即发件人电子邮件)、获取收件人的电子邮件地址、输入电子邮件的主题和正文、指定将与发送电子邮件关联的附件的附加选项,以及最后最重要的一项功能是发送电子邮件,为此已输入所有详细信息。还有一个选项可以打印所有输入的详细信息,例如将用于发送邮件的发件人凭据、收件人电子邮件、电子邮件主题和正文;通过此选项,用户可以验证输入的详细信息,如果发现任何错误,用户可以通过重新输入该详细信息轻松纠正。最终让用户不必发送任何错误数据。在完成所有操作后,通过选择将退出程序的最后一个选项来退出代码执行。 结论因此,在本文中,我们了解了电子邮件模块,并看到了它的各种用例场景。 下一主题Python 中的递归入门程序 |
我们请求您订阅我们的新闻通讯以获取最新更新。