django - 如何获得 session 邀请以与 Gmail/Google Apps 正确集成?

标签 django outlook gmail icalendar

我正在使用 Django 和 python-icalendar 生成 iCalendar 文件,它们在 Outlook (2010) 中正确显示为 session 邀请。在 Gmail(Google Apps)中,我只看到一封空白电子邮件。这是怎么回事?我的一个 .ics 文件如下所示:

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

哦,我正在使用 Django 的 EmailMultiAlternatives 附加 ics 内容,如下所示:

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

最佳答案

这可能有点晚了,但这是我在模型中作为辅助函数的实现(它是一个包含日期作为其自身属性的“事件”模型):

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()来附加(字面意思)消息的替代版本。 Attach() 函数用于扔入日历文件,无论电子邮件客户端选择呈现的消息版本如何(请注意,我还给了它一个“.ics”扩展名)。

我意识到您正在使用 python-icalendar,但 Attach() 方法应该仍然可以正常工作。我刚刚决定向您展示生成 iCal 文件的替代实现。

关于django - 如何获得 session 邀请以与 Gmail/Google Apps 正确集成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4251378/

相关文章:

css - 仅在没有 id 的情况下在 django 中显示某些照片库

outlook - Outlook Web 插件 Chrome M64 中阻止音频/视频录制

python - 在 python 的 Windows 上,可能使用 Outlook API,如何从用户的较小登录名获取用户的全名?

php - 为什么 Outlook 要删除以编程方式发送的当前 session 邀请?

email - Gmail 应用 (Android) 上的响应式电子邮件

c - 在 C 中访问 Gmail

python - 在 INSTALLED_APPS 中添加 "moderation"时出错

python - OneToOne 和 Django 中的模型子类化有什么区别

gmail - 如何使用JavaMailSender使用gmail的 "confidential mode"

python - Django 通用 View :How does DetailView automatically provides the variable to the template ? ?