# Path: aiosmtplib/email.py
# def extract_recipients(
#     message: Union[email.message.EmailMessage, email.message.Message]
# ) -> List[str]:
#     """
#     Extract the recipients from the message object given.
#     """
#     recipients = []  # type: List[str]
# 
#     resent_dates = message.get_all("Resent-Date")
# 
#     if resent_dates is not None and len(resent_dates) > 1:
#         raise ValueError("Message has more than one 'Resent-' header block")
#     elif resent_dates:
#         recipient_headers = ("Resent-To", "Resent-Cc", "Resent-Bcc")
#     else:
#         recipient_headers = ("To", "Cc", "Bcc")
# 
#     for header in recipient_headers:
#         for recipient in message.get_all(header, failobj=[]):
#             recipients.extend(extract_addresses(recipient))
# 
#     return recipients
# 
# def extract_sender(
#     message: Union[email.message.EmailMessage, email.message.Message]
# ) -> Optional[str]:
#     """
#     Extract the sender from the message object given.
#     """
#     resent_dates = message.get_all("Resent-Date")
# 
#     if resent_dates is not None and len(resent_dates) > 1:
#         raise ValueError("Message has more than one 'Resent-' header block")
#     elif resent_dates:
#         sender_header_name = "Resent-Sender"
#         from_header_name = "Resent-From"
#     else:
#         sender_header_name = "Sender"
#         from_header_name = "From"
# 
#     # Prefer the sender field per RFC 2822:3.6.2.
#     if sender_header_name in message:
#         sender_header = message[sender_header_name]
#     else:
#         sender_header = message[from_header_name]
# 
#     if sender_header is None:
#         return None
# 
#     return extract_addresses(sender_header)[0]
# 
# def flatten_message(
#     message: Union[email.message.EmailMessage, email.message.Message],
#     utf8: bool = False,
#     cte_type: str = "8bit",
# ) -> bytes:
#     # Make a local copy so we can delete the bcc headers.
#     message_copy = copy.copy(message)
#     del message_copy["Bcc"]
#     del message_copy["Resent-Bcc"]
# 
#     if isinstance(message.policy, email.policy.Compat32):  # type: ignore
#         # Compat32 cannot use UTF8
#         policy = message.policy.clone(  # type: ignore
#             linesep=LINE_SEP, cte_type=cte_type
#         )
#     else:
#         policy = message.policy.clone(  # type: ignore
#             linesep=LINE_SEP, utf8=utf8, cte_type=cte_type
#         )
# 
#     with io.BytesIO() as messageio:
#         generator = email.generator.BytesGenerator(messageio, policy=policy)
#         generator.flatten(message_copy)
#         flat_message = messageio.getvalue()
# 
#     return flat_message
# 
# def parse_address(address: str) -> str:
#     """
#     Parse an email address, falling back to the raw string given.
#     """
#     display_name, parsed_address = email.utils.parseaddr(address)
# 
#     return parsed_address or address.strip()
# 
# def quote_address(address: str) -> str:
#     """
#     Quote a subset of the email addresses defined by RFC 821.
#     """
#     parsed_address = parse_address(address)
#     return "<{}>".format(parsed_address)


from email.header import Header
from email.headerregistry import Address
from email.message import EmailMessage, Message
from hypothesis import example, given
from hypothesis.strategies import emails
from aiosmtplib.email import (
    extract_recipients,
    extract_sender,
    flatten_message,
    parse_address,
    quote_address,
)
import pytest
        ),
        (
            Address(display_name="Alice Smith", username="alice", domain="example.com"),
            Header("Alice Smith <alice@example.com>"),
            "alice@example.com",
        ),
        (
            Address(display_name="ålice Smith", username="ålice", domain="example.com"),
            Header("ålice Smith <ålice@example.com>", "utf-8"),
            "ålice@example.com",
        ),
        (
            Address(display_name="ålice Smith", username="alice", domain="example.com"),
            Header("ålice Smith <alice@example.com>", "utf-8"),
            "alice@example.com",
        ),
    ),
    ids=("str", "ascii", "utf8_address", "utf8_display_name"),
)
def test_extract_sender(mime_header, compat32_header, expected_sender):
    mime_message = EmailMessage()
    mime_message["From"] = mime_header

    mime_sender = extract_sender(mime_message)

    assert mime_sender == expected_sender

    compat32_message = Message()
    compat32_message["From"] = compat32_header


# Path: aiosmtplib/email.py
# def extract_recipients(
#     message: Union[email.message.EmailMessage, email.message.Message]
# ) -> List[str]:
#     """
#     Extract the recipients from the message object given.
#     """
#     recipients = []  # type: List[str]
# 
#     resent_dates = message.get_all("Resent-Date")
# 
#     if resent_dates is not None and len(resent_dates) > 1:
#         raise ValueError("Message has more than one 'Resent-' header block")
#     elif resent_dates:
#         recipient_headers = ("Resent-To", "Resent-Cc", "Resent-Bcc")
#     else:
#         recipient_headers = ("To", "Cc", "Bcc")
# 
#     for header in recipient_headers:
#         for recipient in message.get_all(header, failobj=[]):
#             recipients.extend(extract_addresses(recipient))
# 
#     return recipients
# 
# def extract_sender(
#     message: Union[email.message.EmailMessage, email.message.Message]
# ) -> Optional[str]:
#     """
#     Extract the sender from the message object given.
#     """
#     resent_dates = message.get_all("Resent-Date")
# 
#     if resent_dates is not None and len(resent_dates) > 1:
#         raise ValueError("Message has more than one 'Resent-' header block")
#     elif resent_dates:
#         sender_header_name = "Resent-Sender"
#         from_header_name = "Resent-From"
#     else:
#         sender_header_name = "Sender"
#         from_header_name = "From"
# 
#     # Prefer the sender field per RFC 2822:3.6.2.
#     if sender_header_name in message:
#         sender_header = message[sender_header_name]
#     else:
#         sender_header = message[from_header_name]
# 
#     if sender_header is None:
#         return None
# 
#     return extract_addresses(sender_header)[0]
# 
# def flatten_message(
#     message: Union[email.message.EmailMessage, email.message.Message],
#     utf8: bool = False,
#     cte_type: str = "8bit",
# ) -> bytes:
#     # Make a local copy so we can delete the bcc headers.
#     message_copy = copy.copy(message)
#     del message_copy["Bcc"]
#     del message_copy["Resent-Bcc"]
# 
#     if isinstance(message.policy, email.policy.Compat32):  # type: ignore
#         # Compat32 cannot use UTF8
#         policy = message.policy.clone(  # type: ignore
#             linesep=LINE_SEP, cte_type=cte_type
#         )
#     else:
#         policy = message.policy.clone(  # type: ignore
#             linesep=LINE_SEP, utf8=utf8, cte_type=cte_type
#         )
# 
#     with io.BytesIO() as messageio:
#         generator = email.generator.BytesGenerator(messageio, policy=policy)
#         generator.flatten(message_copy)
#         flat_message = messageio.getvalue()
# 
#     return flat_message
# 
# def parse_address(address: str) -> str:
#     """
#     Parse an email address, falling back to the raw string given.
#     """
#     display_name, parsed_address = email.utils.parseaddr(address)
# 
#     return parsed_address or address.strip()
# 
# def quote_address(address: str) -> str:
#     """
#     Quote a subset of the email addresses defined by RFC 821.
#     """
#     parsed_address = parse_address(address)
#     return "<{}>".format(parsed_address)


# Path: tests/test_email_utils.py
from email.header import Header
from email.headerregistry import Address
from email.message import EmailMessage, Message
from hypothesis import example, given
from hypothesis.strategies import emails
from aiosmtplib.email import (
    extract_recipients,
    extract_sender,
    flatten_message,
    parse_address,
    quote_address,
)
import pytest

        ),
        (
            Address(display_name="Alice Smith", username="alice", domain="example.com"),
            Header("Alice Smith <alice@example.com>"),
            "alice@example.com",
        ),
        (
            Address(display_name="ålice Smith", username="ålice", domain="example.com"),
            Header("ålice Smith <ålice@example.com>", "utf-8"),
            "ålice@example.com",
        ),
        (
            Address(display_name="ålice Smith", username="alice", domain="example.com"),
            Header("ålice Smith <alice@example.com>", "utf-8"),
            "alice@example.com",
        ),
    ),
    ids=("str", "ascii", "utf8_address", "utf8_display_name"),
)
def test_extract_sender(mime_header, compat32_header, expected_sender):
    mime_message = EmailMessage()
    mime_message["From"] = mime_header

    mime_sender = extract_sender(mime_message)

    assert mime_sender == expected_sender

    compat32_message = Message()
    compat32_message["From"] = compat32_header


    compat32_sender = extract_sender(compat32_message)