I'm pretty new to Python and i'm trying to create a script which gathers data from our database and converts the data into sets. It then takes those sets of data and generates ICS (icalendar) objects (by using icalendar http://codespeak.net/icalendar/).
The problem I'm running into is the email part, I am able to send an email and attach the ICS file but when the email arrives it's just an email with an attachment. I was really hoping for the email to be viewed as a meeting invitation where you just hit "accept". Is there something I can do in order to send the ICS file as a meeting request?
Thanks
below is what worked for me sending invites via python over gmail (worked with google calendar, outlook and 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 = "[email protected]"
password = "yourpassword"
attendees = ["[email protected]", "[email protected]","[email protected]"]
organizer = "ORGANIZER;CN=organiser:mailto:first"+CRLF+" @gmail.com"
fro = "nickname <[email protected]>"
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()
What we did.
Create a meeting invitation
Email to myself. Be sure that it does all the desirable things. ("email to be viewed as a meeting invitation where you just hit 'accept'")
Save that email and the attachment. It's the benchmark.
Build the ICS file in Python that looks exactly like the MS-Office attachment. It's not completely standard. http://en.wikipedia.org/wiki/ICalendar
Be sure that the exact right MIME type is on the attachment, also. IIRC it's text/calendar
.
For any complex problem there's already a solution or a few. Do not compile ics file by hand, use the tools. 'icalendar' package is exactly for the purpose: https://pypi.org/project/icalendar/
Here's a good example of usage: https://www.baryudin.com/blog/entry/sending-outlook-appointments-python/
Also, 'exchangelib' is specifically for outlook: https://pypi.org/project/exchangelib/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With