iCalendar约会是不受支持的日历消息。'

时间:2018-01-31 19:31:58

标签: python-2.7 email outlook icalendar

尝试通过python自动发送Outlook日历邀请。我可以发送电子邮件并附上邀请;但是,邀请是一个不受支持的日历message.ics'。过去两周我一直试图弄清楚如何做到这一点。我甚至尝试过使用win32com.client;但该软件包不允许您像我posted about here那样从单独的帐户发送电子邮件。

以下是我用来生成和发送不支持的日历邮件的代码。':

# email related imports

import smptlib
import email
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email import Encoders

# calendar related imports

import icalendar
from icalendar import Calendar, Parameters, Todo
from icalendar.prop import vDatetime, vCalAddress, vText, vInt, vBoolean, vDDDTypes
import uuid

#other imports

import pytz
from datetime import datetime, date, time, timedelta


# set people parameters

sender = 'thing1@example.com'
recipients = 'thing2@example.com'

attendee = vCalAddress(recipients)
attendee.params['cn'] = vText('Thing 1')
attendee.params['RSVP'] = vText('TRUE')

organizer = vCalAddress(sender)
organizer.params['cn'] = vText('Thing 2')

the_sender = vCalAddress(sender)
the_sender.params['cn'] = vText('Thing 2')

# set timing parameters 

location = 'Home'
tz = pytz.timezone("US/Pacific") # timezone to use for our dates -- change 
as needed
appt_time = tz.localize(datetime(2018, 1, 31, 12, 0))
duration = 30
reminder_min = 15

# set info parameters

subject = 'My Cool Event'
description = 'this better work!'
summary = 'python calendar invite testing'


# create calendar object

cal = icalendar.Calendar()
cal.add('prodid', vText('-//Microsoft Corporation//Outlook 16.0 MIMEDIR//EN'))
cal.add('version', vInt(2.0))
cal.add('method', vText('REQUEST'))
cal.add('X-MS-OLK-FORCEINSPECTOROPEN', vBoolean(True)) # creates one instance of the event

# create the timezone 

tz = icalendar.Timezone()
tz.add('tzid', vText('Pacific Standard Time'))
cal.add_component(tz)

# create the calendar event

event = icalendar.Event()
event.add('method', vText('REQUEST'))
event.add('tzid', vText('Pacific Standard Time'))
event.add('attendee', attendee, encode=0)
event.add('class', vText('PUBLIC'))
event.add('created', vDDDTypes(datetime.now()))
event.add('description', vText(description))
event.add('dtend', vDDDTypes((appt_time + timedelta(minutes = duration))))
event.add('dtstart', vDDDTypes(appt_time))
event.add('location', vText(location))
event.add('from', the_sender)
event.add('organizer', the_sender)
event.add('priority', vInt(5))
event.add('sequence', vInt(0))
event.add('summary', vText(summary))
event.add('transp', vText('opaque')) # Specifies whether or not this appointment is intended to be visible in availability searches
event.add('uid',vText(uuid.uuid4()))
event.add('X-MICROSOFT-CDO-BUSYSTATUS', vText('BUSY')) # sets the busy status of the appointment to busy
event.add('X-MICROSOFT-CDO-IMPORTANCE', vInt(1)) # sets the importance of the appointment (0,1,2)
event.add('X-MICROSOFT-DISALLOW-COUNTER', vBoolean(False))
event.add('X-MS-OLK-APPTSEQTIME', vDDDTypes(datetime.now()))
event.add('X-MS-OLK-AUTOFILLLOCATION', vBoolean(False)) # specifies whether the location is being automatically populated with recipients of type RESOURCE.
event.add('X-MS-OLK-CONFTYPE', vInt(0)) # specifies the type of conferencing that is enabled on the appointment

# set an alarm for a reminder notice

alarm = icalendar.Alarm()
alarm.add("action", "DISPLAY")
alarm.add('description', "Reminder")
alarm.add("TRIGGER;RELATED=START", "-PT{0}M".format(reminder_min))
event.add_component(alarm)
cal.add_component(event)

# write calendar event to file

open('PythonCalendarEvent_1.ics', 'w').writelines(cal.to_ical())

# generate message with (Multi-Purpose Internet Mail Extensions)

msg = MIMEMultipart('mixed')
msg["Subject"] = subject
msg["From"] = sender
msg['To'] = recipients

# attach calendar invite to email message

filename = "PythonCalendarEvent_1.ics"

cal_part = MIMEBase('text', 'calendar', **{'method' : 'REQUEST', 'name' : filename})    
cal_part.set_payload(open(filename,"rb").read())
cal_part.set_type('text/calendar; charset=UTF-8; method=REQUEST; component = VEVENT')
email.Encoders.encode_base64(cal_part)
cal_part.add_header('Content-Type', 'text/calendar')
cal_part.add_header('charset', 'UTF-8')
cal_part.add_header('component', 'VEVENT')
cal_part.add_header('method', 'REQUEST')
cal_part.add_header('Content-class', 'urn:content-classes:appointment')
cal_part.add_header('Content-ID', 'calendar_message')
cal_part.add_header('Content Description', filename)
cal_part.add_header("Filename", filename)
cal_part.add_header("Path", filename)
msg.attach(cal_part)

# send the email at last

s =  smtplib.SMTP(_insert_host_as_string_here_)
s.sendmail(sender, recipients, msg.as_string())
s.quit()

另一个有趣的注意事项:如果转发邀请,则会识别ics。例如,我生成要发送给自己的电子邮件,并将其作为“不支持的日历邮件”进行接收。如果我然后将邀请转发给我的同事,他会将其视为常规日历邀请。我测试看看这是不是我的安装;然而,他复制了代码并遇到了同样的问题 - 第一条消息未被识别,但转发的邀请是我的。

如果我需要进一步澄清,请告诉我。

1 个答案:

答案 0 :(得分:0)

我在这个FOREVER中挣扎但是我想出了一个可以避免这个问题的黑客......

问题是当icalendar执行to_ical()函数时,TZ信息会受到严重破坏。

为了我们的例子,让我们说你的时区是' America / New_York'当您创建日期然后将它们提供给icalendar时,它最终会生成如下所示的DTSTART / DTEND:

DTSTART; TZID = EDT; VALUE = DATE-TIME:20180622T123000 DTEND; TZID = EDT; VALUE = DATE-TIME:20180622T133000

请注意,它有方便的"取代' America / New_York'用' EDT'。 Outlook认为这不是一件真实的事情。

这是我用来修复它的代码。而不是使用原始:

open('PythonCalendarEvent_1.ics', 'w').writelines(cal.to_ical())

请改用:

import re
tzname = 'America/New_York'
ical_string = cal.to_ical().decode('utf-8')
ical_string = re.sub(r"TZID=[^;]*", 'TZID='+tzname, ical_string)
ical_bytes = ical_string.encode('utf-8')
open('PythonCalendarEvent_1.ics', 'w').writelines(ical_bytes)

如果这不能解决您的问题,我会清理我的全部功能并发布...