При добавлении встроенных вложений в тело письма они не отображаются с кодом ниже.
from email.message import EmailMessage
from email.mime.image import MIMEImage
msg = EmailMessage()
msg["From"] = "Test@example.com"
msg["To"] = "Test1@example.com"
msg["Subject"] = "Test Email"
plain_text, html_message = # Plain Text and HTML email content created
msg.set_content(plain_text)
msg.add_alternative(html_message, subtype='html')
# adding the inline image to the email
with open('logo.png', 'rb') as img:
logo = MIMEImage(img.read())
logo.add_header('Content-ID', f'<connect_logo_purple>')
logo.add_header('X-Attachment-Id', 'connect_logo_purple.png')
logo['Content-Disposition'] = f'inline; filename=connect_logo_purple.png'
msg.attach(logo)
НО , когда встроенное вложение добавляется после add_attachment
, оно отображается.
# ... Create message like above
msg.set_content(plain_text)
msg.add_alternative(html_message, subtype='html')
# Only when an attachment is added does the inline images show up
msg.add_attachment(
#..details filled in
)
# .. continue with inline image
with open('logo.png', 'rb') as img:
logo = MIMEImage(img.read())
logo.add_header('Content-ID', f'<connect_logo_purple>')
logo.add_header('X-Attachment-Id', 'connect_logo_purple.png')
logo['Content-Disposition'] = f'inline; filename=connect_logo_purple.png'
msg.attach(logo)