Skip to content

Commit

Permalink
Try to reorder parts
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadrien Huvelle committed Dec 18, 2024
1 parent 6dc1479 commit 0d2431d
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions mail_embed_image/models/ir_mail_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import uuid
from base64 import b64encode
from email.mime.image import MIMEImage

from email.mime.multipart import MIMEMultipart
import requests
from lxml.html import fromstring, tostring

Expand Down Expand Up @@ -58,12 +58,39 @@ def build_email(
subtype_alternative=subtype_alternative,
)
if fileparts:
for fpart in fileparts:
result.attach(fpart)
# Multipart method MUST be multipart/related for CIDs embedding
# Gmail and Office won't process the images otherwise
if image_embedding_method == "cid":
result.set_type("multipart/related")

for fpart in fileparts:
result.attach(fpart)

# after all part where added, we need to reorganize the parts
# before reorganisation, the parts are in this shape:
# - boundary 1
# - text/plain
# - text/html
# - image/png
# After, the parts are in this shape:
# - boundary 1
# - multipart/alternative
# - boundary 2
# - text/plain
# - text/html
# - image/png

# maybe I could user: result.make_alternative() / result.make_related()
alternative = MIMEMultipart('alternative')
all_parts = [alternative]
for part in result.iter_parts():
print(part.get_content_type())
if part.get_content_type() in ["text/html", "text/plain"]:
alternative.attach(part)
else:
all_parts.append(part)
result.set_payload(all_parts)

return result

def _build_email_replace_img_src(self, html_body):
Expand Down

0 comments on commit 0d2431d

Please sign in to comment.