在Python中,将gererated.bib(and.ris和.txt)文件附加到电子邮件中

在Python中,将gererated.bib(and.ris和.txt)文件附加到电子邮件中,python,django,email-attachments,django-email,Python,Django,Email Attachments,Django Email,我有一个基于用户选择填充的模板。与其将此模板呈现为电子邮件正文,我希望将其作为附件发送。如何“写入”我的.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', {'

我有一个基于用户选择填充的模板。与其将此模板呈现为电子邮件正文,我希望将其作为附件发送。如何“写入”我的.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()
新_.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")

我终于用计算机得到了它

bib_content = render_to_string(myTemplate, myContext) 
msg.attach(filename="references.bib", content=bib_content, mimetype="application/x-bibtex")