用Python发送会议邀请
我刚接触Python,正在尝试写一个脚本,从我们的数据库中获取数据,并把这些数据转换成集合。然后,它会把这些数据集合生成ICS(即日历文件)对象(使用的是icalendar这个库,链接在这里:http://codespeak.net/icalendar/)。
我遇到的问题是关于邮件的部分,我可以发送邮件并附上ICS文件,但当邮件到达时,它只是一个带附件的普通邮件。我其实希望这个邮件能被视为会议邀请,收件人只需点击“接受”就可以了。有没有什么办法可以把ICS文件作为会议请求发送呢?
谢谢!
3 个回答
对于任何复杂的问题,通常已经有现成的解决方案了。不要手动编写ics文件,使用工具就可以了。'icalendar'这个包就是为这个目的而设计的:https://pypi.org/project/icalendar/
这里有一个很好的使用示例:https://www.baryudin.com/blog/entry/sending-outlook-appointments-python/
另外,'exchangelib'是专门为Outlook准备的:https://pypi.org/project/exchangelib/
我们做了什么。
创建一个会议邀请。
给自己发一封邮件。确保它能做到所有想要的功能。(“邮件要能被视作会议邀请,点击‘接受’就可以了”)
保存那封邮件和附件。这是我们的基准。
用Python构建一个ICS文件,确保它看起来完全和MS-Office的附件一样。这个格式并不是完全标准的。http://en.wikipedia.org/wiki/ICalendar
确保附件上的MIME类型完全正确。我记得是
text/calendar
。
下面是我用Python通过Gmail发送邀请的成功方法(适用于谷歌日历、Outlook和outlook.com(live/hotmail)):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email.Utils import COMMASPACE, formatdate
from email import Encoders
import os,datetime
CRLF = "\r\n"
login = "yourloging@googlemail.com"
password = "yourpassword"
attendees = ["first@gmail.com", "second@example.com","third@hotmail.com"]
organizer = "ORGANIZER;CN=organiser:mailto:first"+CRLF+" @gmail.com"
fro = "nickname <first@gmail.com>"
ddtstart = datetime.datetime.now()
dtoff = datetime.timedelta(days = 1)
dur = datetime.timedelta(hours = 1)
ddtstart = ddtstart +dtoff
dtend = ddtstart + dur
dtstamp = datetime.datetime.now().strftime("%Y%m%dT%H%M%SZ")
dtstart = ddtstart.strftime("%Y%m%dT%H%M%SZ")
dtend = dtend.strftime("%Y%m%dT%H%M%SZ")
description = "DESCRIPTION: test invitation from pyICSParser"+CRLF
attendee = ""
for att in attendees:
attendee += "ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ- PARTICIPANT;PARTSTAT=ACCEPTED;RSVP=TRUE"+CRLF+" ;CN="+att+";X-NUM-GUESTS=0:"+CRLF+" mailto:"+att+CRLF
ical = "BEGIN:VCALENDAR"+CRLF+"PRODID:pyICSParser"+CRLF+"VERSION:2.0"+CRLF+"CALSCALE:GREGORIAN"+CRLF
ical+="METHOD:REQUEST"+CRLF+"BEGIN:VEVENT"+CRLF+"DTSTART:"+dtstart+CRLF+"DTEND:"+dtend+CRLF+"DTSTAMP:"+dtstamp+CRLF+organizer+CRLF
ical+= "UID:FIXMEUID"+dtstamp+CRLF
ical+= attendee+"CREATED:"+dtstamp+CRLF+description+"LAST-MODIFIED:"+dtstamp+CRLF+"LOCATION:"+CRLF+"SEQUENCE:0"+CRLF+"STATUS:CONFIRMED"+CRLF
ical+= "SUMMARY:test "+ddtstart.strftime("%Y%m%d @ %H:%M")+CRLF+"TRANSP:OPAQUE"+CRLF+"END:VEVENT"+CRLF+"END:VCALENDAR"+CRLF
eml_body = "Email body visible in the invite of outlook and outlook.com but not google calendar"
eml_body_bin = "This is the email body in binary - two steps"
msg = MIMEMultipart('mixed')
msg['Reply-To']=fro
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = "pyICSParser invite"+dtstart
msg['From'] = fro
msg['To'] = ",".join(attendees)
part_email = MIMEText(eml_body,"html")
part_cal = MIMEText(ical,'calendar;method=REQUEST')
msgAlternative = MIMEMultipart('alternative')
msg.attach(msgAlternative)
ical_atch = MIMEBase('application/ics',' ;name="%s"'%("invite.ics"))
ical_atch.set_payload(ical)
Encoders.encode_base64(ical_atch)
ical_atch.add_header('Content-Disposition', 'attachment; filename="%s"'%("invite.ics"))
eml_atch = MIMEBase('text/plain','')
Encoders.encode_base64(eml_atch)
eml_atch.add_header('Content-Transfer-Encoding', "")
msgAlternative.attach(part_email)
msgAlternative.attach(part_cal)
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(login, password)
mailServer.sendmail(fro, attendees, msg.as_string())
mailServer.close()