Skip to content

Commit

Permalink
Correction du header Content-type des pièces jointes aux emails (#3829
Browse files Browse the repository at this point in the history
)

## Linked issues

- Resolve #3791
  • Loading branch information
VincentAntoine authored Nov 6, 2024
2 parents 9cf7585 + ad8cbab commit 0750e52
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
24 changes: 22 additions & 2 deletions datascience/src/pipeline/helpers/emails.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import email
import io
import mimetypes
import smtplib
from email.message import EmailMessage
from logging import Logger
Expand Down Expand Up @@ -30,6 +31,18 @@
)
from src.pipeline.entities.communication_means import CommunicationMeans

mimetypes.add_type(
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
".docx",
strict=True,
)

mimetypes.add_type(
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
".xlsx",
strict=True,
)


def create_html_email(
to: Union[str, List[str]],
Expand Down Expand Up @@ -126,10 +139,17 @@ def create_html_email(

if attachments:
for filename, filebytes in attachments:
(mimetype, _) = guess_type(filename)
try:
(maintype, subtype) = mimetype.split("/")
except Exception:
maintype = "application"
subtype = "octet-stream"

msg.add_attachment(
filebytes,
maintype="application",
subtype="octet-stream",
maintype=maintype,
subtype=subtype,
filename=filename,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1877,18 +1877,20 @@ def test_create_email(
assert len(attachments) == 3 if pno_has_uploaded_attachments else 1
attachment_0 = attachments[0]
assert attachment_0.get_filename() == "Preavis_Le navire 123-abc.pdf"
assert attachment_0.get_content_type() == "application/octet-stream"
assert attachment_0.get_content_type() == "application/pdf"
assert attachment_0.get_content() == b"PDF Document"

if pno_has_uploaded_attachments:
attachment_1 = attachments[1]
assert attachment_1.get_filename() == "Uploaded document n°1.pdf"
assert attachment_1.get_content_type() == "application/octet-stream"
assert attachment_1.get_content_type() == "application/pdf"
assert attachment_1.get_content() == b"PDF"

attachment_2 = attachments[2]
assert attachment_2.get_filename() == "Uploaded document n°2.docx"
assert attachment_2.get_content_type() == "application/octet-stream"
assert attachment_2.get_content_type() == (
"application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
assert attachment_2.get_content() == b"Text Document"

body = pno_to_send.message.get_body()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ def test_create_email(malfunction_to_notify_data, cnsp_logo, notification_type):

attachment = attachments[0]
assert attachment.get_content_disposition() == "attachment"
assert attachment.get_content_type() == "application/octet-stream"
assert attachment.get_content_type() == "application/pdf"
assert attachment.get_filename() == "Notification.pdf"
assert attachment.get_content() == pdf

Expand Down

0 comments on commit 0750e52

Please sign in to comment.