django html_message和send mail中的消息有什么区别

时间:2021-10-07 20:28:17

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.

当我试图用django发送邮件时,只发送html消息,而不是普通消息,我想知道两者之间的区别。是否有最好的方式发送邮件通过模板或html文件,因为我想要一个在我的应用程序的通讯邮件系统。

Note:- the difference is of more important.

注:-区别更重要。

THIS IS WHAT I DID

这就是我所做的

msg_html = (' HELLLOOOOO') msg_plain = 'Normalallalaa

mg_html = (' HELLLOOOOO') msg_plain = 'Normalallalaa。

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

msg_plain send_mail(“titleeeee”,“sender@test”(“reciever@tese”),html_message = msg_html)

My mail contained only Hello in bold

我的邮件中只有Hello的粗体字。

Where did my message go.

我的留言到哪里去了?

2 个解决方案

#1


0  

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.

消息是纯文本,而html_message是使用HTML格式化的消息。如果您同时设置了这两个选项,那么您的电子邮件客户端可能正在显示HTML版本。

#2


1  

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.

问题是有些(旧的或特定配置的)电子邮件客户端不会显示HTML消息,因此您需要确保所有用户都能正确地阅读您的电子邮件。如果您决定只发送文本,那么能够获得HTML消息的用户将不会从中受益。

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.

我使用django.template.loader。render_to_string以给定的上下文呈现模板。这个函数返回的值是一个带有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):

然后我基于HTML消息创建一个纯文本消息(例如,通过使用名为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.

最后我用django.core.mail发送了这封邮件。send_mail,传递html_text和明文作为参数。

#1


0  

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.

消息是纯文本,而html_message是使用HTML格式化的消息。如果您同时设置了这两个选项,那么您的电子邮件客户端可能正在显示HTML版本。

#2


1  

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.

问题是有些(旧的或特定配置的)电子邮件客户端不会显示HTML消息,因此您需要确保所有用户都能正确地阅读您的电子邮件。如果您决定只发送文本,那么能够获得HTML消息的用户将不会从中受益。

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.

我使用django.template.loader。render_to_string以给定的上下文呈现模板。这个函数返回的值是一个带有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):

然后我基于HTML消息创建一个纯文本消息(例如,通过使用名为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.

最后我用django.core.mail发送了这封邮件。send_mail,传递html_text和明文作为参数。