1

Trying to automate a calendar notification by sending an outlook calendar invite via python. I would like to send the email from a separate email address. In python's email package, you can use sendmail() to specify the from_address and to_address; however, I cannot seem to figure out how to do this for an outlook invitation via win32com.client.

I had already tried using the icalendar package to automate this process, but the ics file that is attached to the email 'is unrecognized'.

Using win32com.client, I have been able to generate everything that I want in my invitation; however, I am still unable to figure out how to specify the sender.

import win32com.client as win32
from datetime import datetime
import pytz
tz = pytz.timezone("US/Pacific")

start_time = tz.localize(datetime(2018, 2, 01, 16))
subject = 'The Best Meeting Ever'
duration = 30
location = 'Home'

recipient = 'recipient@example.com'
sender = 'sender@example.com'

outlook = win32.Dispatch('outlook.application')
# CreateItem: 1 -- Outlook Appointment Item
appt = outlook.CreateItem(1) 

# set the parameters of the meeting
appt.Start = start_time
appt.Duration = duration
appt.Location = location
appt.Subject = subject

appt.MeetingStatus = 1 # this enables adding of recipients
appt.Recipients.Add(recipient)
appt.Organizer = sender
appt.ReminderMinutesBeforeStart = 15
appt.ResponseRequested = True
appt.Save()
appt.Send()

when I send the email to my coworker, even though the sender is not my email address, he is receiving the invitation from my personal email and not 'sender@example.com'

1 Answers1

0

Outlook/Exchange will not let you spoof the sender related properties - the meeting invitation will be sent as the current Outlook user.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
  • Is there a way I can send it from a secondary email? I have my primary account, but also get emails to a secondary one as well through Outlook. – RocketGirl87654321 Jan 31 '18 at 16:47
  • Do you mean a separate mailbox account? Or a delegate store linked to your primary Exchange mailbox? – Dmitry Streblechenko Jan 31 '18 at 17:55
  • A separate mailbox account – RocketGirl87654321 Jan 31 '18 at 18:16
  • Then you just need to create appointment in that mailbox - instead of Application.CreateItem, get the right store from the Namespace.Stores collection, open the default Calendar folder using Store.GetDefaultFolder, call MAPIFolder.Items.Add – Dmitry Streblechenko Jan 31 '18 at 20:07
  • Hi Dmitry, Thank you for following up again. Would you be able to post an example? I have been trying to figure this out, but have been unsuccessful thus far! – RocketGirl87654321 Jan 31 '18 at 22:37
  • mapi = outlook.GetNameSpace("MAPI") > DefaultFolder = mapi.stores[0].GetDefaultFolder(), however I am unsure how to understand what that is returning. For example, I would like to know the name of the default folder, that way when I do get to the MAPIFolderItems.Add step I understand what I am adding – RocketGirl87654321 Jan 31 '18 at 22:57
  • You don't need to know the folder name - GetDefaultFolder(olFolderCalendar) will give you the right folder no matter what the name is. Stores collection is 1 based, not 0. And why are you retrieving the very first store? – Dmitry Streblechenko Feb 01 '18 at 02:45
  • I am trying to use a specific inbox. I have three separate email inboxes in outlook and want the calendar invite to generate from one of those three. The default inbox is not the inbox I am trying to use. I am trying to use one of my secondary accounts. – RocketGirl87654321 Feb 01 '18 at 16:48
  • Yes, that is why you need to use Store.GetDefaultFolder, not Namespace,GetDefaultFolder. – Dmitry Streblechenko Feb 01 '18 at 21:25
  • But in Store.GetDefaultFolder how do I specify the specific email addresses inbox – RocketGirl87654321 Feb 01 '18 at 21:42
  • You don't - you first find the right store from the Namespace.Stores collection based on whatever criteria is appropriate, like who the owner is. – Dmitry Streblechenko Feb 01 '18 at 21:44
  • It works! Thank you!!! Now how to send HTML in the body of the calendar invite or an attachment with the 'insert as text' option... – RocketGirl87654321 Feb 16 '18 at 17:11
  • Do you know why the store number does not always remain the same for the specific inbox I am looking at? Sometimes it's outlook.Stores[1], others it's outlook.Stores[0] – RocketGirl87654321 Mar 26 '18 at 20:40
  • Stores do not have an intrinsic index and the Stores collection is not sorted in any particular way. – Dmitry Streblechenko Mar 27 '18 at 21:21