我有一个模板,我根据用户的选择填充。我想将此模板作为附件发送,而不是将此模板呈现为电子邮件的正文。我如何写'我的.bib模板(和我的.ris和.txt类似)我想用作附件吗?
views.py
# I can create this file from a link on my page but I want to send it as an email attachment??
response = render_to_response('publications/publications.bib', {'publications': publications}, context_instance=RequestContext(request), content_type='text/x-bibtex; charset=UTF-8')
response['Content-Disposition'] = 'attachment; filename="references.bib"'
return response
# email creation
text_template = 'mytemplate.txt'
html_template = 'mytemplate.html'
text_content = render_to_string(mytemplate, context, context_instance=RequestContext(request))
html_content = render_to_string(html_template, context, context_instance=RequestContext(request))
msg = EmailMultiAlternatives(subject, text_content, from_email, to)
msg.attach_alternative(html_content, "text/html")
msg.send()
new_attempt.py
# this doesn't work but maybe it's closer?
publications = request.session.get('all_publications')
response = render_to_response('publications/publications.bib', {'publications': publications}, context_instance=RequestContext(request), content_type='text/x-bibtex; charset=UTF-8')
response['Content-Disposition'] = 'attachment; filename="references.bib"'
msg.attach(filename="references.bib", content=response, mimetype="text/x-bibtex")
答案 0 :(得分:0)
我终于使用了
bib_content = render_to_string(myTemplate, myContext)
msg.attach(filename="references.bib", content=bib_content, mimetype="application/x-bibtex")