Python's email
module is great for parsing headers. However, the To:
header can have multiple recipients, and there may be multiple To:
headers. So how do I split out each of the email addresses? I can't split on the comma, since the comma can be quoted. Is there a way to do this?
Demo code:
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)
Current output:
$ python x.py
To: user1@company1.com, "User Two" <user2@company2.com", "Three, User <user3@company3.com>
$