Simple Sending Email HTML in Django, Django have a module for email, one of them is EmailMessage
, and in the class of EmailMessage
have a function content_subtype
, so, u can change it with what your type do u want, like plain
text, or html
. Default from django is plain text. Let’s checkout it:
>>> from django.core.mail import EmailMessage
>>> dir(EmailMessage.content_subtype)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__' ,...., 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>>
>>> msg = EmailMessage.content_subtype
>>> msg
'plain'
>>> msg = EmailMessage.content_subtype = "html"
>>> msg
'html'
>>>
Awesome right?
Now, how i can use it?
from django.core.mail import EmailMessage
subject = "This is subject"
message = "<html><head></head><body>This is body message</body></html>"
def send_email(to_list, subject, message, sender="Aircourts <[email protected]>"):
msg = EmailMessage(subject, message, sender, to_list)
msg.content_subtype = "html" # Main content is now text/html
return msg.send() #replace 'return' if you use like a form or something else.
Ref: http://masnun.com/2014/01/09/django-sending-html-only-email.html