Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between django html_message and message in send mail

When im trying to send a mail with django send mail only the html message is coming and not the normal message i want to know the difference between both. Is there any best way to send mails through template or html files because i want a comming mailing system in my app.

Note:- the difference is of more important.

THIS IS WHAT I DID

msg_html = (' HELLLOOOOO') msg_plain = 'Normalallalaa

send_mail("titleeeee", msg_plain,"sender@test",["reciever@tese",],html_message=msg_html)

My mail contained only Hello in bold

Where did my message go.

like image 867
Faizan Shaikh Avatar asked Oct 31 '25 13:10

Faizan Shaikh


2 Answers

The problem is that some (old or specifically configured) email clients won't display HTML messages, therefore you need to make sure all your users can read your emails properly. If you decide to send just text, then users capable of getting HTML messages won't benefit from it.

This is the strategy I follow:

I use django.template.loader.render_to_string to render a template with a given context. The value this function returns is a string with a message in HTML.

html_text = render_to_string(template, context)

Then I create a plain text message based on the HTML message (for instance, by using a package named html2text):

plain_text = html2text(html_text)

And finally I send the email with django.core.mail.send_mail, passing html_text and plain_text as parameters.

like image 186
Lorenzo Peña Avatar answered Nov 02 '25 06:11

Lorenzo Peña


Message is plain text, while html_message is a message formatted using HTML. If you set both, probably your email client is displaying the HTML version.

like image 29
wobbily_col Avatar answered Nov 02 '25 06:11

wobbily_col