Question

I am getting AttributeError: 'HttpResponse' object has no attribute 'encode' when I attach a ics calender invite to an email to send in Django. This is only if I attached the ical invite, if I don't atttach it everything is fine.

I am not sure why it's looking for a encode attribute on the HttpResponse object or what to do about it.

def email_invite(ics_form, user_assigned):
    subject = 'You Have Been Assigned Schuduled Maintance/Calendar invite'
    from_address = 'foo@no-reply.com'
    body = "This is a invite"
    recip = [user_assigned]
    email = EmailMessage(subject, body, from_address, recip)
    email.attach(ics_form['Filename'], ics_form, 'text/calendar')
    email.send()


def create_ics(data):
    start1 = data['date_due']
    utc = vobject.icalendar.utc
    start2 = datetime.datetime(start1.year, start1.month, start1.day, tzinfo = utc)
    start3 = data['action']
    cal = vobject.iCalendar()
    cal.add('method').value = 'PUBLISH'
    vevent = cal.add('vevent')
    vevent.add('dtstart').value = start2
    vevent.add('dtend').value = start2
    vevent.add('dtstamp').value = datetime.datetime.now()
    vevent.add('summary').value = data['action'].name
    response = HttpResponse(cal.serialize(), content_type='text/calendar')
    response['Filename'] = 'filename.ics'
    response['Content-Disposition'] = 'attachment; filename=filename.ics'
    return response

 File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/django/core/mail/message.py", line 280, in _create_message
    return self._create_attachments(msg)
  File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/django/core/mail/message.py", line 293, in _create_attachments
    msg.attach(self._create_attachment(*attachment))
  File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/django/core/mail/message.py", line 320, in _create_attachment
    attachment = self._create_mime_attachment(content, mimetype)
  File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/django/core/mail/message.py", line 303, in _create_mime_attachment
    attachment = SafeMIMEText(content, subtype, encoding)
  File "/home/one/.virtualenvs/chronos/local/lib/python2.7/site-packages/django/core/mail/message.py", line 126, in __init__
    MIMEText.__init__(self, text, subtype, charset)
  File "/usr/lib/python2.7/email/mime/text.py", line 30, in __init__
    self.set_payload(_text, _charset)
  File "/usr/lib/python2.7/email/message.py", line 226, in set_payload
    self.set_charset(charset)
  File "/usr/lib/python2.7/email/message.py", line 268, in set_charset
    cte(self)
  File "/usr/lib/python2.7/email/encoders.py", line 73, in encode_7or8bit
    orig.encode('ascii')
AttributeError: 'HttpResponse' object has no attribute 'encode'
[15/Jan/2014 08:30:36] "POST /modify/2/ HTTP/1.1" 500 167107
Not Found: /favicon.ico
Was it helpful?

Solution

You haven't pasted all the code but it sounds like you're doing something like:

ical = create_ics(thedata)
email_invite(ical, theuser)

This raises the exception you see because ics_form is an HttpResponse object... the attach method is expecting instead the contents of the file to attach, as a string.

Maybe you should have the create_ics function just return the ical content, and do the returning of an HttpResponse in a separate view function. Something like:

def email_invite(filename, ics, user_assigned):
    subject = 'You Have Been Assigned Schuduled Maintance/Calendar invite'
    from_address = 'foo@no-reply.com'
    body = "This is a invite"
    recip = [user_assigned]
    email = EmailMessage(subject, body, from_address, recip)
    email.attach(filename, ics, 'text/calendar')
    email.send()

def create_ics(data):
    start1 = data['date_due']
    utc = vobject.icalendar.utc
    start2 = datetime.datetime(start1.year, start1.month, start1.day, tzinfo = utc)
    start3 = data['action']
    cal = vobject.iCalendar()
    cal.add('method').value = 'PUBLISH'
    vevent = cal.add('vevent')
    vevent.add('dtstart').value = start2
    vevent.add('dtend').value = start2
    vevent.add('dtstamp').value = datetime.datetime.now()
    vevent.add('summary').value = data['action'].name
    return cal.serialize()

def download_ics(request):
    # do whatever you do to prepare thedata
    ics = create_ics(thedata)
    response = HttpResponse(ics, content_type='text/calendar')
    response['Filename'] = 'filename.ics'
    response['Content-Disposition'] = 'attachment; filename=filename.ics'
    return response

ical = create_ics(thedata)
email_invite('filename.ics', ical, theuser)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top