任何人都可以帮我发送带有动态内容的HTML电子邮件.一种方法是将整个html代码复制到一个变量中,并在Django视图中填充其中的动态代码,但这似乎不是一个好主意,因为它是一个非常大的html文件.
我将不胜感激任何建议.
谢谢.
Mar*_*n W 50
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.utils.html import strip_tags
subject, from_email, to = 'Subject', 'from@xxx.com', 'to@xxx.com'
html_content = render_to_string('mail_template.html', {'varname':'value'}) # render with dynamic value
text_content = strip_tags(html_content) # Strip the html tag. So people can see the pure text at least.
# create the email, and attach the HTML version as well.
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()
Run Code Online (Sandbox Code Playgroud)
http://www.djangofoo.com/250/sending-html-email/comment-page-1#comment-11401
Bab*_*yan 49
Django包括django.core.mail.send_mail这些天的方法(2018年),不需要EmailMultiAlternatives直接使用类.改为:
from django.core import mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags
subject = 'Subject'
html_message = render_to_string('mail_template.html', {'context': 'values'})
plain_message = strip_tags(html_message)
from_email = 'From <from@example.com>'
to = 'to@example.com'
mail.send_mail(subject, plain_message, from_email, [to], html_message=html_message)
Run Code Online (Sandbox Code Playgroud)
这将发送一封电子邮件,该电子邮件在两个支持html的浏览器中都可见,并将在残缺的电子邮件查看器中显示纯文本.
alk*_*lik 14
对于在 2020 年查看并使用 django v3.x 的任何人(我不知道这是什么时候引入的,因此它可能适用于早期版本。
注意:我只想包含一个没有纯文本版本的 html 版本。我的 Django 视图:
from django.template.loader import render_to_string
from django.core.mail import EmailMessage
# import html message.html file
html_template = 'path/to/message.html'
html_message = render_to_string(html_template, { 'context': context, })
message = EmailMessage(subject, html_message, from_email, [to_email])
message.content_subtype = 'html' # this is required because there is no plain text email message
message.send()
Run Code Online (Sandbox Code Playgroud)
我的 html 文件 (message.html) 如下所示:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Order received</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body style="margin: 0; padding: 0;">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="320" style="border: none; border-collapse: collapse; font-family: Arial, sans-serif; font-size: 14px; line-height: 1.5;">
...
content
...
</table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
此处有更多详细信息:从django 文档发送替代内容类型
这应该做你想要的:
from django.core.mail import EmailMessage
from django.template import Context
from django.template.loader import get_template
template = get_template('myapp/email.html')
context = Context({'user': user, 'other_info': info})
content = template.render(context)
if not user.email:
raise BadHeaderError('No email address given for {0}'.format(user))
msg = EmailMessage(subject, content, from, to=[user.email,])
msg.send()
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅django邮件文档.
试试这个::::
https://godjango.com/19-using-templates-for-sending-emails/
# views.py
from django.http import HttpResponse
from django.template import Context
from django.template.loader import render_to_string, get_template
from django.core.mail import EmailMessage
def email_one(request):
subject = "I am a text email"
to = ['buddy@buddylindsey.com']
from_email = 'test@example.com'
ctx = {
'user': 'buddy',
'purchase': 'Books'
}
message = render_to_string('main/email/email.txt', ctx)
EmailMessage(subject, message, to=to, from_email=from_email).send()
return HttpResponse('email_one')
def email_two(request):
subject = "I am an HTML email"
to = ['buddy@buddylindsey.com']
from_email = 'test@example.com'
ctx = {
'user': 'buddy',
'purchase': 'Books'
}
message = get_template('main/email/email.html').render(Context(ctx))
msg = EmailMessage(subject, message, to=to, from_email=from_email)
msg.content_subtype = 'html'
msg.send()
return HttpResponse('email_two')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40633 次 |
| 最近记录: |