尝试编写python脚本以发送Outlook电子邮件邀请。使用过的说明:http://www.baryudin.com/blog/sending-outlook-appointments-python.html 但是,我一直得到一个不受支持的日历消息。#34;随身携带我的电子邮件。代码如下:
import random
import smtplib
import email.MIMEText
import email.MIMEBase
from datetime import datetime
from email.MIMEMultipart import MIMEMultipart
import icalendar
from pytz import timezone
LOCAL_TZ = timezone("US/Eastern")
def send_appointment(subject, description):
start_dt = LOCAL_TZ.localize(datetime(2016, 6, 3, 17, 0, 0))
end_dt = LOCAL_TZ.localize(datetime(2016, 6, 3, 18, 0, 0))
cal = icalendar.Calendar()
cal.add("prodid", "-//My oncall calendar application//test.com")
cal.add("version", "2.0")
cal.add("method", "REQUEST")
event = icalendar.Event()
event.add("attendee", MY_EMAIL)
event.add("organizer", MY_EMAIL)
event.add("status", "confirmed")
event.add("summary", subject)
event.add("description", description)
event.add("location", "my desk")
event.add("dtstart", start_dt)
event.add("dtend", end_dt)
event.add("dtstamp", start_dt)
event["uid"] = random.random()
event.add("priority", 5)
event.add("sequence", 1)
event.add("created", LOCAL_TZ.localize(datetime.now()))
alarm = icalendar.Alarm()
alarm.add("action", "DISPLAY")
alarm.add("description", "Reminder")
alarm.add("TRIGGER;RELATED=START", "-PT{0}H".format(1))
event.add_component(alarm)
cal.add_component(event)
msg = MIMEMultipart("alternative")
msg["Subject"] = subject
msg["From"] = MY_EMAIL
msg["To"] = MY_EMAIL
msg["Content-class"] = "urn:content-classes:calendarmessage"
msg.attach(email.MIMEText.MIMEText(description))
filename = "invite.ics"
part = email.MIMEBase.MIMEBase("text", "calendar", method="REQUEST", name=filename)
part.set_payload(cal.to_ical())
email.Encoders.encode_base64(part)
part.add_header("Content-Description", filename)
part.add_header("Content-class", "urn:content-classes:calendarmessage")
part.add_header("Filename", filename)
part.add_header("Path", filename)
msg.attach(part)
s = smtplib.SMTP("localhost")
s.sendmail(MY_EMAIL, MY_EMAIL, msg.as_string())
s.quit()
def main():
send_appointment("test", "desc")
if __name__=="__main__":
main()
注意:我定义了MY_EMAIL,但没有把它放在这里。
生成的.ics文件位于下方。
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//My oncall calendar application//test.com
METHOD:REQUEST
BEGIN:VEVENT
SUMMARY:test
DTSTART;TZID=US/Eastern;VALUE=DATE-TIME:20160603T170000
DTEND;TZID=US/Eastern;VALUE=DATE-TIME:20160603T180000
DTSTAMP;VALUE=DATE-TIME:20160603T210000Z
UID:0.669475599056
SEQUENCE:1
ATTENDEE:myemail@somewhere.com
CREATED;VALUE=DATE-TIME:20160603T204723Z
DESCRIPTION:desc
LOCATION:my desk
ORGANIZER:myemail@somewhere.com
PRIORITY:5
STATUS:confirmed
BEGIN:VALARM
ACTION:DISPLAY
DESCRIPTION:Reminder
TRIGGER;RELATED=START:-PT1H
END:VALARM
END:VEVENT
END:VCALENDAR
编辑:好的,我想出了问题。它只了解utc时区。因此,如果我将开始和结束日期时间更改为以下内容,那么我将获得正确的日历邀请。
start_dt = datetime(2016, 6, 3, 17, 0, 0, tzinfo=pytz.utc)
end_dt = datetime(2016, 6, 3, 18, 0, 0, tzinfo=pytz.utc)
不完全确定为什么它无法理解其他时区。查看.ics文件,日期时间字段如下所示。
DTSTART;VALUE=DATE-TIME:20160603T170000Z
DTEND;VALUE=DATE-TIME:20160603T180000Z
而不是
DTSTART;TZID=US/Eastern;VALUE=DATE-TIME:20160603T170000
DTEND;TZID=US/Eastern;VALUE=DATE-TIME:20160603T180000
现在的问题是为什么它无法处理其他时区,但我想这应该是另一个帖子。
答案 0 :(得分:2)
好的,我想出了问题。它只了解utc时区。因此,如果我将开始和结束日期时间更改为以下内容,那么我将获得正确的日历邀请。
start_dt = datetime(2016, 6, 3, 17, 0, 0, tzinfo=pytz.utc)
end_dt = datetime(2016, 6, 3, 18, 0, 0, tzinfo=pytz.utc)
不完全确定为什么它无法理解其他时区。查看.ics文件,日期时间字段如下所示。
DTSTART;VALUE=DATE-TIME:20160603T170000Z
DTEND;VALUE=DATE-TIME:20160603T180000Z
而不是
DTSTART;TZID=US/Eastern;VALUE=DATE-TIME:20160603T170000
DTEND;TZID=US/Eastern;VALUE=DATE-TIME:20160603T180000
答案 1 :(得分:0)
不确定你是否仍然需要这个,但我想出来了。请参阅我的回复over her e。简短的回答是,它实际上不是给你带来问题的时区。我遇到了同样的问题并做了你建议的改变。 Outlook确实识别了我的ics文件,但时间错了。郁闷了。我在另一篇文章中的回复显示我是如何为我修复的......