Skip to content

Commit

Permalink
allow Content-ID to be set on email attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Nov 7, 2021
1 parent 51bb9e3 commit 9e93843
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ yarn-error.log*
/js/public/logo.png
/.mypy_cache/
/activate.sh
/scratch/
7 changes: 6 additions & 1 deletion aioaws/ses.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class SesAttachment:
file: Union[Path, bytes]
name: Optional[str] = None
mime_type: Optional[str] = None
content_id: Optional[str] = None


@dataclass
Expand Down Expand Up @@ -188,7 +189,11 @@ async def prepare_attachment(a: SesAttachment) -> Tuple[MIMEBase, int]:
msg.set_payload(data)
encode_base64(msg)

msg.add_header('Content-Disposition', 'attachment', filename=filename)
if a.content_id is None:
msg.add_header('Content-Disposition', 'attachment', filename=filename)
else:
msg.add_header('Content-ID', a.content_id)
msg.add_header('Content-Disposition', 'inline', filename=filename)
return msg, len(data)


Expand Down
5 changes: 3 additions & 2 deletions aioaws/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ def ses_email_data(data: Dict[str, str]) -> Dict[str, Any]:
for part in msg.walk():
if payload := part.get_payload(decode=True):
part_info = {'Content-Type': part.get_content_type(), 'payload': payload.decode().replace('\r\n', '\n')}
if cd := part['Content-Disposition']:
part_info['Content-Disposition'] = cd
for key in 'Content-Disposition', 'Content-ID':
if cd := part[key]:
part_info[key] = cd
d['payload'].append(part_info)

return {'body': dict(data), 'email': d}
Expand Down
30 changes: 30 additions & 0 deletions tests/test_ses.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,36 @@ async def test_custom_headers(client: AsyncClient, aws: DummyServer):
}


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(
'testing@sender.com',
'the subject',
['testing@recipient.com'],
'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_encoded_unsub(client: AsyncClient, aws: DummyServer):
ses = SesClient(client, SesConfig('test_access_key', 'test_secret_key', 'testing-region-1'))
unsub_link = 'https://www.example.com/unsubscrible?blob=?blob=$MzMgMTYwMTY3MDEyOCBMMzcbN_nhcDZNg-6D=='
Expand Down

0 comments on commit 9e93843

Please sign in to comment.