【问题标题】:Sending Meeting Invitations With Python使用 Python 发送会议邀请
【发布时间】:2011-06-16 22:50:35
【问题描述】:

我对 Python 很陌生,我正在尝试创建一个脚本,该脚本从我们的数据库中收集数据并将数据转换为集合。然后它获取这些数据集并生成 ICS (icalendar) 对象(通过使用 icalendar http://codespeak.net/icalendar/)。

我遇到的问题是电子邮件部分,我可以发送电子邮件并附加 ICS 文件,但是当电子邮件到达时,它只是一封带有附件的电子邮件。我真的希望将电子邮件视为您只需点击“接受”的会议邀请。为了将 ICS 文件作为会议请求发送,我可以做些什么吗?

谢谢

【问题讨论】:

    标签: python icalendar


    【解决方案1】:

    我们做了什么。

    1. 创建会议邀请

    2. 给我自己发电子邮件。确保它完成了所有令人满意的事情。 (“电子邮件被视为会议邀请,您只需点击“接受”即可”)

    3. 保存该电子邮件和附件。这是基准。

    4. 用 Python 构建 ICS 文件,该文件看起来完全类似于 MS-Office 附件。这不是完全标准的。 http://en.wikipedia.org/wiki/ICalendar

    5. 确保准确正确的 MIME 类型也在附件上。 IIRC 是 text/calendar

    【讨论】:

      【解决方案2】:

      以下是我通过 gmail 通过 python 发送邀请的方法(使用 google 日历、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()
      

      【讨论】:

      • Python3 需要更改导入包的名称并更改 MIMEBase('text/plain','') 行。在这里查看我的答案:stackoverflow.com/a/62255629/706751
      • @AuberonVacher ical_atcheml_atch 的目的是什么?您似乎没有对他们做任何事情。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多