相关疑难解决方法(0)

如何从 Python 中的 RFC 2822 邮件标头中提取多个电子邮件地址?

Python 的email模块非常适合解析头文件。但是,To:header 可以有多个接收者,也可能有多个To:header。那么如何拆分每个电子邮件地址呢?我不能在逗号上拆分,因为可以引用逗号。有没有办法做到这一点?

演示代码:

msg="""To: user1@company1.com, "User Two" <user2@company2.com", "Three, User <user3@company3.com>                               
From: anotheruser@user.com                                                                                                      
Subject: This is a subject                                                                                                      

This is the message.                                                                                                            
"""

import email

msg822 = email.message_from_string(msg)
for to in msg822.get_all("To"):
    print("To:",to)
Run Code Online (Sandbox Code Playgroud)

电流输出:

$ python x.py
To: user1@company1.com, "User Two" <user2@company2.com", "Three, User <user3@company3.com>
$ 
Run Code Online (Sandbox Code Playgroud)

python email rfc822

6
推荐指数
1
解决办法
1809
查看次数

标签 统计

email ×1

python ×1

rfc822 ×1