You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Suggestion for a helper function for email.utils #162648
You can extract a "To" for a mail message, but this might be a list of emails, and each one might in turn be a combination of display names and smtp addresses (which parseaddr by itself cannot presently handle...) Complicating this, a display name might contain a comma, complicating a split of the recipients.
I would suggest that a helper function returns a dictionary of recipients (based on the "To" header...), where the key is the display name of a recipient, and each value is the smtp address. I include a function below that implements this. The only issue is: what if the same smtp address is used for different display names? That edge case wasn't of concern to me, but if it was to someone else, they could check for it by evaluating the returned dictionary.
I did a google search and did not see a straightforward suggestion for handling that...
importcsvfromemail.utilsimportparseaddrdefget_recipients(message)->dict:
recipients={}
to, encoding=decode_header(message.get('To'))[0]
ifisinstance(to, bytes):
to=to.decode(encoding)
try: # in case it's malformed...forrecipientincsv.reader([to]):
to_name, to_smtp=parseaddr(recipient)
recipients[to_name]=to_smtpexcept:
passreturnrecipients# get_recipients
Activity