PDF email attachments are opened as HTML

Summary

When trying to open a PDF attachment, the file is downloaded with an .html file extension which causes the browser to display the PDF file as an HTML document. This is, of course, not a usable representation.

I could not verify if this issue also exists in Chrome because I only have Firefox installed. I also tested it in Microsoft Edge, but could not reproduce the issue because Edge automatically downloads the file for me and stores it as a .pdf file.

Steps to reproduce

  1. Send an email, e.g. with Outlook from your private email address, with a PDF attachment to your @foodsharing.network mailbox.
  2. Go to https://foodsharing.de/?page=mailbox.
  3. Open the received email (from step 1).
  4. Click on the attachment.
  5. In the open dialog, select "open with" (for me the default application is Firefox) and press "OK".
  6. The file will be opened as an HTML document.

NOTE: the issue can also be reproduced with https://beta.foodsharing.de/?page=mailbox

What is the current bug behavior?

The attachment is opened as an HTML document which produces gibberish because the actual file is a PDF document.

What is the expected correct behavior?

The file is opened as a PDF document.

Relevant error messages and/or screenshots

The opened file looks like this. The interesting part of this screenshot is actually the address bar: the file is downloaded as an HTML file. gibberish

In the second screenshot we see the reason for this behavior. The Content-Type header is set as follows

content-type: text/html; charset=UTF-8

Therefore, Firefox assumes that the content is an HTML document which causes Firefox to only display applications in the "open with" dropdown which are registered for handling HTML documents. Furthermore, it automatically adds the .html file extension when saving the file.

wrong-content-type

I used this file to reproduce the issue. sample.pdf

Possible fixes

The issue can be mitigated if you just download the file by selecting "save file" instead of "open with". In this case, the browser does not automatically add a file extension based on the Content-Type header, i.e. the file is just stored with the .pdf extension.

To actually fix this issue, the server has to set the Content-Type header correctly. It should be possible to extract the correct file type form the MIME type for an attachment in the email.