Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a meeting invitation to integrate properly with Gmail/Google Apps?

I am generating iCalendar files with Django and python-icalendar, and they correctly show up in Outlook (2010) as meeting invitations. In Gmail (Google Apps), I just see a blank email. What's the deal? Here's what one of my .ics files looks like:

BEGIN:VCALENDAR
METHOD:REQUEST
PRODID:-//My Events App//example.com//
VERSION:2.0
BEGIN:VEVENT
ATTENDEE;CN=Richard;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:[email protected]
CREATED;VALUE=DATE:20101122T183813
DESCRIPTION:Phone number: (212)-123-4567\n\nThis is a test description
 for the conference call.
DTEND;VALUE=DATE:20101127T131802Z
DTSTAMP;VALUE=DATE:20101127T121802Z
DTSTART;VALUE=DATE:20101127T121802Z
LAST-MODIFIED;VALUE=DATE:20101122T183813
ORGANIZER;CN=Example.com:[email protected]
SEQUENCE:1
SUMMARY:Conference call about GLD
UID:example.com.20
END:VEVENT
END:VCALENDAR

Oh, and I'm using Django's EmailMultiAlternatives to attach the ics content, like so:

if calendar:
    message.attach_alternative(calendar.as_string(), "text/calendar; method=REQUEST; charset=\"UTF-8\"")
    message.content_subtype = 'calendar'
like image 904
rfrankel Avatar asked Nov 22 '10 23:11

rfrankel


1 Answers

This may be a little late, but here is my implementation as a helper function in my model (it's an "event" model that contains a date as a property of itself):

from icalendar import Calendar, Event as ICalEvent
...
class Event(models.Model):
...
    def generate_calendar(self):
        cal = Calendar()
        site = Site.objects.get_current()

        cal.add('prodid', '-//{0} Events Calendar//{1}//'.format(site.name,
                                                                 site.domain))
        cal.add('version', '2.0')

        ical_event = ICalEvent()
        ical_event.add('summary', self.title)
        ical_event.add('dtstart', self.start_date)
        ical_event.add('dtend', self.end_date)
        ical_event.add('dtstamp', self.end_date)
        ical_event['uid'] = str(self.id)

        cal.add_component(ical_event)
        return cal.to_ical()

And then in the function that sends the email, I have:

# This one has the plain text version of the message
msg = EmailMultiAlternatives('Event Confirmation', text_email,
                             FROM_EMAIL, [self.user.email])
# This one has the HTML version of the message
msg.attach_alternative(html_email, 'text/html')
# Now to attach the calendar
msg.attach("{0}.ics".format(self.event.slug),
           self.event.generate_calendar(), 'text/calendar')
msg.send(fail_silently=True)

That solution uses icalendar (which I prefer to vobject), and it also uses attach_alternative() to attach (literally) an alternative version of the message. The attach() function is being used to toss in the calendar file, regardless of the version of the message that the email client chooses to render (note that I also gave it an ".ics" extension).

I realize that you are using python-icalendar, but the attach() method should still work about the same. I just decided to also show you an alternative implementation to generating iCal files.

like image 181
JoeLinux Avatar answered Nov 10 '22 13:11

JoeLinux