# Path: aiosmtplib/errors.py
# class SMTPNotSupported(SMTPException):
#     """
#     A command or argument sent to the SMTP server is not supported.
#     """
# 
# class SMTPRecipientsRefused(SMTPException):
#     """
#     SMTP server refused multiple recipients.
#     """
# 
#     def __init__(self, recipients: List[SMTPRecipientRefused]) -> None:
#         self.recipients = recipients
#         self.args = (recipients,)
# 
# class SMTPResponseException(SMTPException):
#     """
#     Base class for all server responses with error codes.
#     """
# 
#     def __init__(self, code: int, message: str) -> None:
#         self.code = code
#         self.message = message
#         self.args = (code, message)
# 
# Path: aiosmtplib/status.py
# class SMTPStatus(enum.IntEnum):
#     """
#     Defines SMTP statuses for code readability.
# 
#     See also: http://www.greenend.org.uk/rjk/tech/smtpreplies.html
#     """
# 
#     invalid_response = -1
#     system_status_ok = 211
#     help_message = 214
#     ready = 220
#     closing = 221
#     auth_successful = 235
#     completed = 250
#     will_forward = 251
#     cannot_vrfy = 252
#     auth_continue = 334
#     start_input = 354
#     domain_unavailable = 421
#     mailbox_unavailable = 450
#     error_processing = 451
#     insufficient_storage = 452
#     tls_not_available = 454
#     unrecognized_command = 500
#     unrecognized_parameters = 501
#     command_not_implemented = 502
#     bad_command_sequence = 503
#     parameter_not_implemented = 504
#     domain_does_not_accept_mail = 521
#     access_denied = 530  # Sendmail specific
#     auth_failed = 535
#     mailbox_does_not_exist = 550
#     user_not_local = 551
#     storage_exceeded = 552
#     mailbox_name_invalid = 553
#     transaction_failed = 554
#     syntax_error = 555
# 
# Path: aiosmtplib/email.py
# def formataddr(pair: Tuple[str, str]) -> str:
#     """
#     Copied from the standard library, and modified to handle international (UTF-8)
#     email addresses.
# 
#     The inverse of parseaddr(), this takes a 2-tuple of the form
#     (realname, email_address) and returns the string value suitable
#     for an RFC 2822 From, To or Cc header.
#     If the first element of pair is false, then the second element is
#     returned unmodified.
#     """
#     name, address = pair
#     if name:
#         encoded_name = UTF8_CHARSET.header_encode(name)
#         return "{} <{}>".format(encoded_name, address)
#     else:
#         quotes = ""
#         if SPECIALS_REGEX.search(name):
#             quotes = '"'
#             name = ESCAPES_REGEX.sub(r"\\\g<0>", name)
#             return "{}{}{} <{}>".format(quotes, name, quotes, address)
# 
#     return address


import copy
import email.generator
import email.header
import pytest
from aiosmtplib import (
    SMTPNotSupported,
    SMTPRecipientsRefused,
    SMTPResponseException,
    SMTPStatus,
)
from aiosmtplib.email import formataddr
                message_str,
                rcpt_options=["NOTIFY=FAILURE,DELAY"],
            )

        recipient_exc = excinfo.value.recipients[0]
        assert recipient_exc.code == SMTPStatus.syntax_error
        assert (
            recipient_exc.message
            == "RCPT TO parameters not recognized or not implemented"
        )


async def test_sendmail_simple_failure(smtp_client, smtpd_server):
    async with smtp_client:
        with pytest.raises(SMTPRecipientsRefused):
            #  @@ is an invalid recipient.
            await smtp_client.sendmail("test@example.com", ["@@"], "blah")


async def test_sendmail_error_silent_rset_handles_disconnect(
    smtp_client,
    smtpd_server,
    smtpd_class,
    smtpd_response_handler_factory,
    monkeypatch,
    sender_str,
    recipient_str,
    message_str,
):
    response_handler = smtpd_response_handler_factory(

# Path: aiosmtplib/errors.py
# class SMTPNotSupported(SMTPException):
#     """
#     A command or argument sent to the SMTP server is not supported.
#     """
# 
# class SMTPRecipientsRefused(SMTPException):
#     """
#     SMTP server refused multiple recipients.
#     """
# 
#     def __init__(self, recipients: List[SMTPRecipientRefused]) -> None:
#         self.recipients = recipients
#         self.args = (recipients,)
# 
# class SMTPResponseException(SMTPException):
#     """
#     Base class for all server responses with error codes.
#     """
# 
#     def __init__(self, code: int, message: str) -> None:
#         self.code = code
#         self.message = message
#         self.args = (code, message)
# 
# Path: aiosmtplib/status.py
# class SMTPStatus(enum.IntEnum):
#     """
#     Defines SMTP statuses for code readability.
# 
#     See also: http://www.greenend.org.uk/rjk/tech/smtpreplies.html
#     """
# 
#     invalid_response = -1
#     system_status_ok = 211
#     help_message = 214
#     ready = 220
#     closing = 221
#     auth_successful = 235
#     completed = 250
#     will_forward = 251
#     cannot_vrfy = 252
#     auth_continue = 334
#     start_input = 354
#     domain_unavailable = 421
#     mailbox_unavailable = 450
#     error_processing = 451
#     insufficient_storage = 452
#     tls_not_available = 454
#     unrecognized_command = 500
#     unrecognized_parameters = 501
#     command_not_implemented = 502
#     bad_command_sequence = 503
#     parameter_not_implemented = 504
#     domain_does_not_accept_mail = 521
#     access_denied = 530  # Sendmail specific
#     auth_failed = 535
#     mailbox_does_not_exist = 550
#     user_not_local = 551
#     storage_exceeded = 552
#     mailbox_name_invalid = 553
#     transaction_failed = 554
#     syntax_error = 555
# 
# Path: aiosmtplib/email.py
# def formataddr(pair: Tuple[str, str]) -> str:
#     """
#     Copied from the standard library, and modified to handle international (UTF-8)
#     email addresses.
# 
#     The inverse of parseaddr(), this takes a 2-tuple of the form
#     (realname, email_address) and returns the string value suitable
#     for an RFC 2822 From, To or Cc header.
#     If the first element of pair is false, then the second element is
#     returned unmodified.
#     """
#     name, address = pair
#     if name:
#         encoded_name = UTF8_CHARSET.header_encode(name)
#         return "{} <{}>".format(encoded_name, address)
#     else:
#         quotes = ""
#         if SPECIALS_REGEX.search(name):
#             quotes = '"'
#             name = ESCAPES_REGEX.sub(r"\\\g<0>", name)
#             return "{}{}{} <{}>".format(quotes, name, quotes, address)
# 
#     return address


# Path: tests/test_sendmail.py
import copy
import email.generator
import email.header
import pytest
from aiosmtplib import (
    SMTPNotSupported,
    SMTPRecipientsRefused,
    SMTPResponseException,
    SMTPStatus,
)
from aiosmtplib.email import formataddr

                message_str,
                rcpt_options=["NOTIFY=FAILURE,DELAY"],
            )

        recipient_exc = excinfo.value.recipients[0]
        assert recipient_exc.code == SMTPStatus.syntax_error
        assert (
            recipient_exc.message
            == "RCPT TO parameters not recognized or not implemented"
        )


async def test_sendmail_simple_failure(smtp_client, smtpd_server):
    async with smtp_client:
        with pytest.raises(SMTPRecipientsRefused):
            #  @@ is an invalid recipient.
            await smtp_client.sendmail("test@example.com", ["@@"], "blah")


async def test_sendmail_error_silent_rset_handles_disconnect(
    smtp_client,
    smtpd_server,
    smtpd_class,
    smtpd_response_handler_factory,
    monkeypatch,
    sender_str,
    recipient_str,
    message_str,
):
    response_handler = smtpd_response_handler_factory(

        "{} error".format(SMTPStatus.unrecognized_parameters), close_after=True