-->

How can I get a meeting invitation to integrate pr

2019-04-06 20:24发布

问题:

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:rich@example.com
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:events@example.com
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'

回答1:

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.



回答2:

I had to play around with .ics files ages ago, and came up with a little helper app named django-cal, which simplifies the whole process.

It's not in active development anymore, but seems to still satisfy the need of a few people. Patches and improvements very welcome!