0

I am trying to send html emails to users and i have one html template.

html content starts with:

Dear XYZ, ..

how is it possible to change XYZ with users name that i am sending this mail to? how can i crawl the html and change that name and do this?:

subject, from_email, to = 'subject', 'test@gmail.com', 'email'
html_content = render_to_string('the_template.html', {'varname':'value'}) # ...
text_content = strip_tags(html_content) 
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

this would save me a month!

4

1 回答 1

2

好吧,你的模板应该有

Dear {{name}}

作为它的一部分,当你渲染而不是使用

html_content = render_to_string('the_template.html', {'varname':'value'})

利用

html_content = render_to_string('the_template.html', {'name':<whatever name you want>})

如果您从request对象中引用它,则可以使用

Dear {{request.user.first_name}} {{request.user.last_name}}

反而。

于 2013-08-04T06:50:02.173 回答