async def test_inline_attachment(client: AsyncClient, aws: DummyServer): ses = SesClient(client, SesConfig('test_access_key', 'test_secret_key', 'testing-region-1')) await ses.send_email( '*****@*****.**', 'the subject', ['*****@*****.**'], 'this is a test email', html_body='this is the <b>html body</b>.', attachments=[SesAttachment(file=b'some attachment', name='foobar.txt', content_id='<testing-content-id>')], ) assert len(aws.app['emails']) == 1 assert aws.app['emails'][0]['email']['payload'] == [ { 'Content-Type': 'text/plain', 'payload': 'this is a test email\n', }, { 'Content-Type': 'text/html', 'payload': 'this is the <b>html body</b>.\n', }, { 'Content-Type': 'text/plain', 'payload': 'some attachment', 'Content-Disposition': 'inline; filename="foobar.txt"', 'Content-ID': '<testing-content-id>', }, ]
async def test_attachment_path(client: AsyncClient, aws: DummyServer, tmp_path): ses = SesClient(client, SesConfig('test_access_key', 'test_secret_key', 'testing-region-1')) p = tmp_path / 'testing.txt' p.write_text('hello') await ses.send_email( '*****@*****.**', 'test with attachment', ['*****@*****.**'], html_body='<b>html body</b>', attachments=[SesAttachment(file=p)], ) assert len(aws.app['emails']) == 1 email = aws.app['emails'][0]['email'] assert email == { 'Subject': 'test with attachment', 'From': '*****@*****.**', 'To': '*****@*****.**', 'MIME-Version': '1.0', 'payload': [ {'Content-Type': 'text/plain', 'payload': '\n'}, {'Content-Type': 'text/html', 'payload': '<b>html body</b>\n'}, { 'Content-Type': 'text/plain', 'payload': 'hello', 'Content-Disposition': 'attachment; filename="testing.txt"', }, ], }
async def test_send_email_attachment(client: AsyncClient, aws: DummyServer): ses = SesClient(client, SesConfig('test_access_key', 'test_secret_key', 'testing-region-1')) await ses.send_email( '*****@*****.**', 'test with attachment £££ more', ['*****@*****.**'], 'this is a test email', attachments=[SesAttachment(file=b'some binary data', name='testing.txt', mime_type='text/plain')], ) assert len(aws.app['emails']) == 1 eml = aws.app['emails'][0] assert eml['email'] == { 'Subject': 'test with attachment £££ more', 'From': '*****@*****.**', 'MIME-Version': '1.0', 'To': '*****@*****.**', 'payload': [ {'Content-Type': 'text/plain', 'payload': 'this is a test email\n'}, { 'Content-Type': 'text/plain', 'payload': 'some binary data', 'Content-Disposition': 'attachment; filename="testing.txt"', }, ], }
async def test_send_email_attachment(client: AsyncClient, aws: DummyServer): ses = SesClient(client, SesConfig('test_access_key', 'test_secret_key', 'testing-region-1')) await ses.send_email( '*****@*****.**', 'test with attachment £££ more', ['*****@*****.**'], 'this is a test email', attachments=[SesAttachment(file=b'some binary data', name='testing.txt', mime_type='text/plain')], ) assert len(aws.app['emails']) == 1 eml = aws.app['emails'][0] assert eml['email'] == { 'Subject': 'test with attachment £££ more', 'From': '*****@*****.**', 'MIME-Version': '1.0', 'To': '*****@*****.**', 'payload': [ {'Content-Type': 'text/plain', 'payload': 'this is a test email\n'}, { 'Content-Type': 'text/plain', 'payload': 'some binary data', 'Content-Disposition': 'attachment; filename="testing.txt"', }, ], } raw_body = base64.b64decode(eml['body']['RawMessage.Data'].encode()) assert re.fullmatch( ( br'Subject: test with attachment =\?utf-8\?b\?wqPCo8Kj\?= more\n' br'From: testing@sender\.com\n' br'To: testing@recipient\.com\n' br'MIME-Version: 1\.0\n' br'Content-Type: multipart/mixed; boundary="===============(\d+)=="\n\n' br'--===============\1==\n' br'Content-Type: text/plain; charset="utf-8"\n' br'Content-Transfer-Encoding: 7bit\n\n' br'this is a test email\n\n' br'--===============(\d+)==\n' br'Content-Type: text/plain\n' br'MIME-Version: 1\.0\n' br'Content-Transfer-Encoding: base64\n' br'Content-Disposition: attachment; filename="testing\.txt"\n\n' br'c29tZSBiaW5hcnkgZGF0YQ==\n\n' br'--===============\2==--\n' ), raw_body, )
async def test_attachment_email_with_html(client: AsyncClient, aws: DummyServer): ses = SesClient(client, SesConfig('test_access_key', 'test_secret_key', 'testing-region-1')) await ses.send_email( '*****@*****.**', 'the subject', ['*****@*****.**'], 'this is a test email', html_body='this is the <b>html body</b>.', attachments=[SesAttachment(file=b'some attachment', name='testing.txt', mime_type='application/pdf')], ) assert len(aws.app['emails']) == 1 eml = aws.app['emails'][0] assert eml['email']['Subject'] == 'the subject' raw_body = base64.b64decode(eml['body']['RawMessage.Data'].encode()) assert re.fullmatch( ( br'Subject: the subject\n' br'From: testing@sender\.com\n' br'To: testing@recipient\.com\n' br'MIME-Version: 1\.0\n' br'Content-Type: multipart/mixed; boundary="===============(\d+)=="\n\n' br'--===============\1==\n' br'Content-Type: multipart/alternative;\n' br' boundary="===============(\d+)=="\n\n' br'--===============\2==\n' br'Content-Type: text/plain; charset="utf-8"\n' br'Content-Transfer-Encoding: 7bit\n\n' br'this is a test email\n\n' br'--===============\2==\n' br'Content-Type: text/html; charset="utf-8"\n' br'Content-Transfer-Encoding: 7bit\n' br'MIME-Version: 1\.0\n\n' br'this is the <b>html body</b>\.\n\n' br'--===============\2==--\n\n' br'--===============\1==\n' br'Content-Type: text/plain\n' br'MIME-Version: 1\.0\n' br'Content-Transfer-Encoding: base64\n' br'Content-Disposition: attachment; filename="testing\.txt"\n\n' br'c29tZSBhdHRhY2htZW50\n\n' br'--===============\1==--\n' ), raw_body, )