bharal
bharal

Reputation: 16194

django - gmail shows the raw html of an email (ie the tag)

I'm using the django helper method send_mail, which might be the problem, but I cannot tell from the docs if that is the case.

So - here is my method:

send_mail('Alert!', theEmail.format(user.username),'[email protected]',
        [user.email], fail_silently=False)

And theEmail looks like this:

theEmail = """ Hi {0}!
Here is an alert email
And here is a link: a link in time

But when i run this code, the email sends fine - but in gmail, the message is printed with all the tags visible.

Is there some sort of "send as html" thing i'm meant to do ?

Upvotes: 1

Views: 1542

Answers (3)

sean2000
sean2000

Reputation: 516

try something like:

from django.core.mail import EmailMessage

msg = EmailMessage(subject, html, from_email, [recipients])
msg.content_subtype = 'html'
msg.send()

Upvotes: 4

Alexander Fedotov
Alexander Fedotov

Reputation: 1075

Try use html_message argument:

send_mail('Alert!', '', '[email protected]', [user.email], html_message=theEmail.format(user.username), fail_silently=False)

Upvotes: 1

Torsten Engelbrecht
Torsten Engelbrecht

Reputation: 13496

You can play around with some of Django's built-in functions to escape the html string before sending (maybe gmail does some escaping/unescaping themself), e.g.:

from django.utils.safestring import mark_for_escaping
my_html = mark_for_escaping(my_raw_html)

or

from django.utils.safestring import mark_safe
my_html = mark_safe(my_raw_html)

send_mail does not support a feature like "send as html".

Upvotes: 1

Related Questions