gpt4 book ai didi

python - 从 Python 获取带有 outlook 电子邮件邀请的 "not supported calendar message.ics"附件

转载 作者:太空狗 更新时间:2023-10-30 01:20:40 31 4
gpt4 key购买 nike

尝试编写一个 python 脚本来发送 outlook 电子邮件邀请。此处使用的说明:http://www.baryudin.com/blog/sending-outlook-appointments-python.html但是,我的电子邮件中不断收到“不支持的日历 message.ics”附件。代码如下:

    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

现在的问题是为什么它不能处理其他时区,但我想那应该是另一篇文章。

最佳答案

好的,我想出了问题。它只理解 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

关于python - 从 Python 获取带有 outlook 电子邮件邀请的 "not supported calendar message.ics"附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37622750/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com