2010-11-22 28 views
8

我使用Django和python-icalendar生成iCalendar文件,並且它們在Outlook(2010)中正確顯示爲會議邀請。在Gmail(Google Apps)中,我只看到一封空白的電子郵件。這是怎麼回事?下面是我的.ics文件一個樣子:如何獲得與Gmail/Google Apps正確集成的會議邀請?

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 

哦,我使用的Django的EmailMultiAlternatives到ICS內容連接,就像這樣:

if calendar: 
    message.attach_alternative(calendar.as_string(), "text/calendar; method=REQUEST; charset=\"UTF-8\"") 
    message.content_subtype = 'calendar' 
+4

查看相關[post](http://stackoverflow.com/questions/4397938/attaching-an-ical-file-to-a-django-email)。該解決方案使用「附件」而不是「替代」,看起來像它在谷歌上的作品。 – 2011-03-09 21:56:53

+0

@equinoxel但是由於使用「附件」而不是「替代」或使用`vobject`而不是`icalendar`。我非常喜歡Plone集體開發[icalendar](http://pypi.python.org/pypi/icalendar)。我總是喜歡它的API,而不是圍繞vobject形成的RFC的薄層。 – 2012-09-11 10:27:42

回答

0

我不得不玩的。 ics文件,並提出了一個名爲django-cal的小幫手應用程序,它簡化了整個過程。

它不在積極發展中,但似乎仍能滿足少數人的需求。修補程序和改進非常歡迎!

1

這可能是晚了一點,但這裏是我的執行情況在我的模型的輔助功能(這是一個「事件」模型,其中包含一個日期作爲其自身的屬性):

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() 

然後在發送該電子郵件的功能,我有:

# 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) 

即解決方案使用的iCalendar(我喜歡VOBJECT),並且它也使用attach_alternative()附加(字面)消息的替代版本。無論電子郵件客戶端選擇呈現的郵件的版本如何(請注意,我還爲其提供了「.ics」擴展名),attach()函數正用於在日曆文件中進行折騰。

我知道你正在使用python-icalendar,但attach()方法應該仍然可以工作。我只是決定也向你展示了一個生成iCal文件的替代實現。