0

I have a template that I populate based on a user's choices. Rather than render this template as the body of the email, I want to send it as an attachment. How do I 'write' to my .bib template (and, similarly to my .ris and .txt) that I want to use as an attachment?

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")
steph
  • 701
  • 2
  • 10
  • 30

1 Answers1

0

I finally got it using

bib_content = render_to_string(myTemplate, myContext) 
msg.attach(filename="references.bib", content=bib_content, mimetype="application/x-bibtex")
steph
  • 701
  • 2
  • 10
  • 30