Home > Net >  Python - Recurring Email with Attachment
Python - Recurring Email with Attachment

Time:03-29

Using Python, how do I send an recurring email (using outlook) say every Monday, that will include an attachment? Attachment would be located on my desktop.

file_path = r"U:\\t\\Analytics\\Finance\\20 FP&A\\22 Forecast File.xlsx"

import os
import win32com.client as win32

# construct Outlook application instance

 olApp = win32.Dispatch('Outlook.Application')
 olNS = olApp.GetNameSpace('MAPI')

# construct the email item object

 mailItem = olApp.CreateItem(0)
 mailItem.Subject = 'memo'
 mailItem.BodyFormat = 1
 mailItem.Body = "Hello World"
 mailItem.To = 'email example'

 mailItem.Attachments.Add(file_path)

 mailItem.Display()

CodePudding user response:

Unlike recurring appointments, there is no such thing as a recurring email. Your code would have to explicitly create, populate, and send an email every time it needs to be created, populated, and sent.

CodePudding user response:

The Outlook object model doesn't provide anything for sending recurring emails out of the box. You can use timers to run your job at some points of time periodically. See the Timers section in MSDN for Windows APIs available for that.

Also you may find the Outlook VBA - Run a code every half an hour thread which discusses how to run a code periodically. Even it is related to VBA, but the same approach can be used in Python. Windows API is the same for all kind of programming languages.

You may find the Python Timer Functions: Three Ways to Monitor Your Code article helpful.

  • Related