Skip to content

Commit

Permalink
Fixes #128: Content-Disposition Header Gets Ignored. (#129)
Browse files Browse the repository at this point in the history
* Fixes #128: Content-Disposition Header Gets Ignored.

* Updated documentation example.
  • Loading branch information
dvuckovic authored Jul 25, 2022
1 parent ebf2122 commit cd442fa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
7 changes: 5 additions & 2 deletions docs/example.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,15 @@ Used for example for referencing Content-ID images in html of email
message = MessageSchema(
subject='Fastapi-Mail module',
recipients=recipients,
html="<img src='cid:logo_image'>",
html="<img src='cid:logo_image@fastapi-mail'>",
subtype='html',
attachments=[
{
"file": "/path/to/file.png",
"headers": {"Content-ID": "<logo_image>"},
"headers": {
"Content-ID": "<logo_image@fastapi-mail>",
"Content-Disposition": "inline; filename=\"file.png\"", # For inline images only
},
"mime_type": "image",
"mime_subtype": "png",
}
Expand Down
27 changes: 16 additions & 11 deletions fastapi_mail/msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,25 @@ async def attach_file(self, message, attachment):
part.set_payload(await file.read())
encode_base64(part)

filename = file.filename

try:
filename and filename.encode('ascii')
except UnicodeEncodeError:
if not PY3:
filename = filename.encode('utf8')

filename = ('UTF8', '', filename)

part.add_header('Content-Disposition', 'attachment', filename=filename)
if file_meta and 'headers' in file_meta:
for header in file_meta['headers'].keys():
part.add_header(header, file_meta['headers'][header])

# Add an implicit `Content-Disposition` attachment header,
# but only if it wasn't supplied explicitly.
# More info here: https://github.com/sabuhish/fastapi-mail/issues/128
if not part.get('Content-Disposition'):
filename = file.filename

try:
filename and filename.encode('ascii')
except UnicodeEncodeError:
if not PY3:
filename = filename.encode('utf8')

filename = ('UTF8', '', filename)
part.add_header('Content-Disposition', 'attachment', filename=filename)

self.message.attach(part)

async def _message(self, sender):
Expand Down
22 changes: 19 additions & 3 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,18 @@ async def test_attachement_message_with_headers(mail_config):
attachments=[
{
'file': attachement,
'headers': {'Content-ID': 'test ID'},
'headers': {
'Content-ID': 'test ID',
'Content-Disposition': 'inline; filename="attachement.txt"',
},
'mime_type': 'image',
'mime_subtype': 'png',
}
},
{
'file': attachement,
'mime_type': 'image',
'mime_subtype': 'png',
},
],
)
conf = ConnectionConfig(**mail_config)
Expand All @@ -127,9 +135,17 @@ async def test_attachement_message_with_headers(mail_config):
assert mail._payload[1].get_content_subtype() == msg.attachments[0][1].get('mime_subtype')

assert mail._payload[1].__dict__.get('_headers')[0][1] == 'image/png'
assert mail._payload[1].__dict__.get('_headers')[4][1] == msg.attachments[0][1].get(
assert mail._payload[1].__dict__.get('_headers')[3][1] == msg.attachments[0][1].get(
'headers'
).get('Content-ID')
assert mail._payload[1].__dict__.get('_headers')[4][1] == msg.attachments[0][1].get(
'headers'
).get('Content-Disposition')

assert (
mail._payload[2].__dict__.get('_headers')[3][1] == 'attachment; '
"filename*=UTF8''attachement.txt"
)


@pytest.mark.asyncio
Expand Down

0 comments on commit cd442fa

Please # to comment.