Skip to content

Instantly share code, notes, and snippets.

@felipe3dfx
Created March 5, 2020 15:12
Show Gist options
  • Save felipe3dfx/73514b020f9c50697fbd580e0b88c31a to your computer and use it in GitHub Desktop.
Save felipe3dfx/73514b020f9c50697fbd580e0b88c31a to your computer and use it in GitHub Desktop.
Create email with embedding image
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