Skip to content

Instantly share code, notes, and snippets.

@rturowicz
Forked from syedsaqibali/gist:9066627
Created February 18, 2014 09:42
Show Gist options
  • Save rturowicz/9067682 to your computer and use it in GitHub Desktop.
Save rturowicz/9067682 to your computer and use it in GitHub Desktop.
from django.core.mail import send_mail
import os
def sendMyAppEmail(toList, template, toType=None, subject="", templateDict={}):
# Load the image you want to send at bytes
img_data = open(os.path.join(settings.MEDIA_ROOT, 'bumblebee.jpeg'), 'rb').read()
# Create a "related" message container that will hold the HTML
# message and the image
msg = MIMEMultipart(_subtype='related')
# Create the body with HTML. Note that the image, since it is inline, is
# referenced with the URL cid:myimage... you should take care to make
# "myimage" unique
body = MIMEText('<p>Hello <img src="cid:myimage" /></p>', _subtype='html')
msg.attach(body)
# Now create the MIME container for the image
img = MIMEImage(img_data, 'jpeg')
img.add_header('Content-Id', '<myimage>') # angle brackets are important
msg.attach(img)
send_mail(
subject=subject,
message=msg.as_string(),
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=toList,
fail_silently=False,
)
sendMyAppEmail(["[email protected]"], None, toType=None, subject="Hello World3", templateDict={})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment