Skip to content

Suggestion for a helper function for email.utils #162648

Backlog

Description

@bitmodeler

Feature or enhancement

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...

import csv
from email.utils import parseaddr

def get_recipients(message)->dict:
    recipients={}
    to, encoding=decode_header(message.get('To'))[0]
    if isinstance(to, bytes):
       to=to.decode(encoding)

    try: # in case it's malformed...
         for recipient in csv.reader([to]):
             to_name, to_smtp=parseaddr(recipient)
             recipients[to_name]=to_smtp

    except:
            pass

    return recipients # get_recipients

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Activity

  1. added
    type-feature
    on Nov 25, 2026
  2. added
    stdlib
    on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibtopic-emailtype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions