def test_plain_text_with_attachments(): msg = Message(from_address="*****@*****.**", to="*****@*****.**", subject="hello", body="hello world") msg.attach_attachment(content_type="text/plain", data=b"this is test") assert "Content-Type: multipart/mixed" in str(msg)
async def send_email(self, msg: PubSubMessage) -> None: email = EmailMessage(**msg.data) email_raw = Message( subject=email.subject, to=email.to, body=email.body, html=email.html, from_address=email.from_address or self.email.from_address, cc=email.cc, bcc=email.bcc, reply_to=email.reply_to, date=email.date, ) for attachment in email.attachments: email_raw.attach( Attachment( filename=attachment.filename, content_type=attachment.content_type, data=attachment.data, )) await self.email.send(email_raw)
def test_mail_and_rcpt_options(): msg = Message() assert msg.mail_options == [] assert msg.rcpt_options == [] msg = Message(mail_options=["BODY=8BITMIME"]) assert msg.mail_options == ["BODY=8BITMIME"] msg = Message(rcpt_options=["NOTIFY=OK"]) assert msg.rcpt_options == ["NOTIFY=OK"]
def test_to_address(): msg = Message(to="*****@*****.**") assert msg.to_address == {"*****@*****.**"} msg = Message(to="*****@*****.**", cc="*****@*****.**", bcc=["*****@*****.**", "*****@*****.**"]) expected_to_address = { "*****@*****.**", "*****@*****.**", "*****@*****.**", "*****@*****.**", } assert msg.to_address == expected_to_address msg = Message(to="*****@*****.**", cc="*****@*****.**") assert msg.to_address == {"*****@*****.**"}
def test_plain_text(): plain_text = "Hello!\nIt works." msg = Message(from_address="*****@*****.**", to="*****@*****.**", body=plain_text) assert msg.body == plain_text assert "Content-Type: text/plain" in str(msg)
def test_extra_headers(): msg = Message( from_address="*****@*****.**", to="*****@*****.**", extra_headers={"Extra-Header-Test": "Test"}, ) assert "Extra-Header-Test: Test" in str(msg)
def test_html(): html_text = "<b>Hello</b><br/>It works." msg = Message(from_address="*****@*****.**", to="*****@*****.**", html=html_text) assert msg.html == html_text assert "Content-Type: multipart/alternative" in str(msg)
def test_attach(): msg = Message() att = Attachment() atts = [Attachment() for i in range(3)] msg.attach(att) assert msg.attachments == [att] msg.attach(*atts) assert msg.attachments == [att] + atts
async def test_send_email(clear_inbox, get_emails): await clear_inbox() mail = Mail(hostname="localhost", port=1025) msg = Message( from_address="*****@*****.**", subject="Hello Subject", to="*****@*****.**", body="Hello World", ) await mail.send(msg) email = await get_emails() assert msg.from_address in email["sender"] assert msg.subject == email["subject"] assert msg.body in email["source"] assert msg.message_id in email["source"]
def test_attachment_unicode_filename(): msg = Message(from_address="*****@*****.**", to="*****@*****.**") # Chinese filename :) msg.attach_attachment("我的测试文档.txt", "text/plain", "this is test") assert "UTF8''%E6%88%91%E7%9A%84%E6%B5%8B%E8%AF" "%95%E6%96%87%E6%A1%A3.txt" in str( msg)
def test_charset(): msg = Message() assert msg.charset == "utf-8" msg = Message(charset="ascii") assert msg.charset == "ascii"
def test_reply_to(): msg = Message(from_address="*****@*****.**", to="*****@*****.**", reply_to="*****@*****.**") assert msg.reply_to == "*****@*****.**" assert "*****@*****.**" in str(msg)
def test_validate(): msg = Message(from_address="*****@*****.**") with pytest.raises(SenderError): msg.validate() msg = Message(to="*****@*****.**") with pytest.raises(SenderError): msg.validate() msg = Message(subject="subject\r", from_address="*****@*****.**", to="*****@*****.**") with pytest.raises(SenderError): msg.validate() msg = Message(subject="subject\n", from_address="*****@*****.**", to="*****@*****.**") with pytest.raises(SenderError): msg.validate()
def test_bcc(): msg = Message(from_address="*****@*****.**", to="*****@*****.**", bcc="*****@*****.**") assert "*****@*****.**" not in str(msg)
def test_cc(): msg = Message(from_address="*****@*****.**", to="*****@*****.**", cc="*****@*****.**") assert "*****@*****.**" in str(msg)
def test_from_address(): msg = Message(from_address="*****@*****.**", to="*****@*****.**") assert msg.from_address == "*****@*****.**" assert "*****@*****.**" in str(msg)
def test_to(): msg = Message(from_address="*****@*****.**", to="*****@*****.**") assert msg.to == {"*****@*****.**"} assert "*****@*****.**" in str(msg) msg = Message(to=["*****@*****.**", "*****@*****.**"]) assert msg.to == {"*****@*****.**", "*****@*****.**"}
def test_subject(): msg = Message("test") assert msg.subject in "test" msg = Message("test", from_address="*****@*****.**", to="*****@*****.**") assert msg.subject in str(msg)
def test_attachment_ascii_filename(): msg = Message(from_address="*****@*****.**", to="*****@*****.**") msg.attach_attachment("my test doc.txt", "text/plain", b"this is test") assert "Content-Disposition: attachment; filename=" '"my test doc.txt"' in str( msg)
def test_message_id(): msg = Message(from_address="*****@*****.**", to="*****@*****.**") assert f"Message-ID: {msg.message_id}" in str(msg)
def test_attach_attachment(): msg = Message() msg.attach_attachment("test.txt", "text/plain", "this is test") assert msg.attachments[0].filename == "test.txt" assert msg.attachments[0].content_type == "text/plain" assert msg.attachments[0].data == "this is test"