0

ユーザーの選択に基づいて入力するテンプレートがあります。このテンプレートをメールの本文としてレンダリングするのではなく、添付ファイルとして送信したいと考えています。添付ファイルとして使用したい .bib テンプレート (および、.ris および .txt と同様) に「書き込む」にはどうすればよいですか?

ビュー.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")
4

1 に答える 1

0

使ってやっと手に入れた

bib_content = render_to_string(myTemplate, myContext) 
msg.attach(filename="references.bib", content=bib_content, mimetype="application/x-bibtex")
于 2015-02-26T21:06:07.920 に答える