How to use the aiosmtplib.email.flatten_message function in aiosmtplib

To help you get started, we’ve selected a few aiosmtplib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cole / aiosmtplib / tests / test_email_utils.py View on Github external
def test_flatten_message():
    message = EmailMessage()
    message["To"] = "bob@example.com"
    message["Subject"] = "Hello, World."
    message["From"] = "alice@example.com"
    message.set_content("This is a test")

    flat_message = flatten_message(message)

    expected_message = b"""To: bob@example.com\r
Subject: Hello, World.\r
From: alice@example.com\r
Content-Type: text/plain; charset="utf-8"\r
Content-Transfer-Encoding: 7bit\r
MIME-Version: 1.0\r
\r
This is a test\r
"""
    assert flat_message == expected_message
github cole / aiosmtplib / tests / test_email_utils.py View on Github external
def test_flatten_message_removes_bcc_from_message_text():
    message = EmailMessage()
    message["Bcc"] = "alice@example.com"

    flat_message = flatten_message(message)

    assert flat_message == b"\r\n"  # empty message
github cole / aiosmtplib / tests / test_email_utils.py View on Github external
message["To"] = "bob@example.com"
    message["Cc"] = "claire@example.com"
    message["Bcc"] = "dustin@example.com"

    message["Subject"] = "Hello, World."
    message["From"] = "alice@example.com"
    message.set_content("This is a test")

    message["Resent-Date"] = "Mon, 20 Nov 2017 21:04:27 -0000"
    message["Resent-To"] = "eliza@example.com"
    message["Resent-Cc"] = "fred@example.com"
    message["Resent-Bcc"] = "gina@example.com"
    message["Resent-Subject"] = "Fwd: Hello, World."
    message["Resent-From"] = "hubert@example.com"

    flat_message = flatten_message(message)

    expected_message = b"""To: bob@example.com\r
Cc: claire@example.com\r
Subject: Hello, World.\r
From: alice@example.com\r
Content-Type: text/plain; charset="utf-8"\r
Content-Transfer-Encoding: 7bit\r
MIME-Version: 1.0\r
Resent-Date: Mon, 20 Nov 2017 21:04:27 -0000\r
Resent-To: eliza@example.com\r
Resent-Cc: fred@example.com\r
Resent-Subject: Fwd: Hello, World.\r
Resent-From: hubert@example.com\r
\r
This is a test\r
"""
github cole / aiosmtplib / tests / test_email_utils.py View on Github external
def test_flatten_message_utf8_options(utf8, cte_type, expected_chunk):
    message = EmailMessage()
    message["From"] = "ålice@example.com"

    flat_message = flatten_message(message, utf8=utf8, cte_type=cte_type)

    assert expected_chunk in flat_message
github cole / aiosmtplib / aiosmtplib / smtp.py View on Github external
if not self.supports_extension("smtputf8"):
                raise SMTPNotSupported(
                    "An address containing non-ASCII characters was provided, but "
                    "SMTPUTF8 is not supported by this server"
                )
            elif "smtputf8" not in [option.lower() for option in mail_options]:
                mail_options.append("SMTPUTF8")

        if self.supports_extension("8BITMIME"):
            if "body=8bitmime" not in [option.lower() for option in mail_options]:
                mail_options.append("BODY=8BITMIME")
            cte_type = "8bit"
        else:
            cte_type = "7bit"

        flat_message = flatten_message(message, utf8=utf8_required, cte_type=cte_type)

        return await self.sendmail(
            sender,
            recipients,
            flat_message,
            mail_options=mail_options,
            rcpt_options=rcpt_options,
            timeout=timeout,
        )