python - CSV File Attachment Error: 'bytes' object has no attribute 'encode' -


i need create csv file , send email attachment. want create file in memory not occupy space in file system. seem have working except error when try send email: 'bytes' object has no attribute 'encode' ideas what's going wrong?

    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()     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 csv attachment     subject='data'     to=["hello@me.com",]     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 @ /poll/81e21783370b46218d6e26d1366b97b8/close/ exception value: 'bytes' object has no attribute 'encode'

not elegant solution works:

    import csv, io     csvfile = io.stringio()     fieldnames = ['user_id', 'user_name', 'user_forecast', 'is_correct', 'correct_answer',         'forecast_to_event_seconds', 'answer_types']     writer = csv.dictwriter(csvfile, fieldnames=fieldnames)      writer.writeheader()     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(), 'answer_types': poll.answer_type})      # send email csv attachment     subject='data'     to=["hello@me.com",]     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(' ', '-'))     csv_file = csvfile.getvalue()     msg.attach(file_name, csv_file,'text/csv')     msg.content_subtype = "html"     msg.send() 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -