I need to create a CSV file and send it as an email attachment. I want to create the file in memory so that it does not actually occupy space in my file system. I seem to have it nearly working except I get an error when I try to send the email: 'bytes' object has no attribute 'encode'
Any ideas what's going wrong?
from django.core.mail import EmailMultiAlternatives
import csv, io
csvfile = io.StringIO()
fieldnames = ['user_id', 'user_name', 'user_forecast', 'is_correct', 'correct_answer',
'forecast_to_event_seconds']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for f in forecasts:
writer.writerow({'user_id': f.profile.id, 'user_name': f.profile.name,
'user_forecast': f.answer_text, 'is_correct': f.is_correct, 'correct_answer': correct_answer,
'forecast_to_event_seconds': (event_end-f.created_on).total_seconds()})
# Send email with CSV attachment
subject='Data'
to=["[email protected]",]
from_email=from_email
context={'group': group.replace('-', ' '), 'question': poll.question, 'site': Site.objects.get_current(),
'STATIC_URL': settings.STATIC_URL}
template='polls/notifications/castie_data.txt'
message = render_to_string('polls/notifications/castie_data.txt', context)
msg = EmailMultiAlternatives(subject, message, to=to, from_email=from_email)
msg.attach_alternative(render_to_string('polls/notifications/castie_data.html', context), 'text/html')
file_name = "%s-%s.csv" % (group, poll.question.replace(' ', '-'))
msg.attach_file(file_name, 'text/csv')
msg.content_subtype = "html"
msg.send()
Full Traceback:
Environment:
Request Method: POST Request URL: http://localhost:8000/poll/81e21783370b46218d6e26d1366b97b8/close/
Django Version: 1.7.7 Python Version: 3.4.3
Traceback: File "/Users/s/.virtualenvs/c/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response 111. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/stephaniesocias/.virtualenvs/cassie/lib/python3.4/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 21. return view_func(request, *args, **kwargs) File "/Users/stephaniesocias/git/cassie/cassie-app/polls/views.py" in close_poll 101. create_send_data(poll.id) File "/Users/stephaniesocias/git/cassie/cassie-app/polls/tasks.py" in create_send_data 455. msg.send() File "/Users/stephaniesocias/.virtualenvs/cassie/lib/python3.4/site-packages/django/core/mail/message.py" in send 286. return self.get_connection(fail_silently).send_messages([self]) File "/Users/stephaniesocias/.virtualenvs/cassie/lib/python3.4/site-packages/django/core/mail/backends/smtp.py" in send_messages 99. sent = self._send(message) File "/Users/stephaniesocias/.virtualenvs/cassie/lib/python3.4/site-packages/django/core/mail/backends/smtp.py" in _send 113. message = email_message.message() File "/Users/stephaniesocias/.virtualenvs/cassie/lib/python3.4/site-packages/django/core/mail/message.py" in message 253. msg = self._create_message(msg) File "/Users/stephaniesocias/.virtualenvs/cassie/lib/python3.4/site-packages/django/core/mail/message.py" in _create_message 409. return self._create_attachments(self._create_alternatives(msg)) File "/Users/stephaniesocias/.virtualenvs/cassie/lib/python3.4/site-packages/django/core/mail/message.py" in _create_attachments 325. msg.attach(self._create_attachment(*attachment)) File "/Users/stephaniesocias/.virtualenvs/cassie/lib/python3.4/site-packages/django/core/mail/message.py" in _create_attachment 367. attachment = self._create_mime_attachment(content, mimetype) File "/Users/stephaniesocias/.virtualenvs/cassie/lib/python3.4/site-packages/django/core/mail/message.py" in _create_mime_attachment 338. attachment = SafeMIMEText(content, subtype, encoding) File "/Users/stephaniesocias/.virtualenvs/cassie/lib/python3.4/site-packages/django/core/mail/message.py" in init 175. MIMEText.init(self, text, subtype, None) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/email/mime/text.py" in init 33. try:
Exception Type: AttributeError at /poll/81e21783370b46218d6e26d1366b97b8/close/ Exception Value: 'bytes' object has no attribute 'encode'
attach_file(filename, 'text/csv')
. – Alasdair