Skip to content

Instantly share code, notes, and snippets.

@syedsaqibali
Last active March 16, 2019 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save syedsaqibali/9066627 to your computer and use it in GitHub Desktop.
Save syedsaqibali/9066627 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(["myEmail@gmail.com"], None, toType=None, subject="Hello World3", templateDict={})
@rijojohn85
Copy link

Hi,
How do I add context to my template if I need to?

@doverradio
Copy link

settings is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment