Created
March 5, 2020 15:12
-
-
Save felipe3dfx/73514b020f9c50697fbd580e0b88c31a to your computer and use it in GitHub Desktop.
Create email with embedding image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
email_message = EmailMultiAlternatives( | |
subject='subject', | |
body='some text', | |
from_email='example@example.com', | |
to=['example@example'], | |
) | |
email_message.attach_alternative(render_to_string('email.html', context), 'text/html') | |
email_message.mixed_subtype = 'related' | |
# Image | |
image = open('qrcode.png') | |
msg_img = MIMEImage(image.read()) | |
mimetype = msg_img['Content-Type'] | |
del msg_img['Content-Type'] | |
del msg_img['Content-Disposition'] | |
msg_img.add_header('Content-Disposition', 'inline', filename='qrcode.png') | |
msg_img.add_header('Content-Type', mimetype, name='qrcode.png') | |
msg_img.add_header('Content-ID', f'<qrcode.png>') | |
msg_img.add_header('X-Attachment-Id', 'qrcode.png') | |
email_message.attach(msg_img) | |
email_message.send() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment