Skip to content

Instantly share code, notes, and snippets.

@ynadeem
Created May 20, 2018 15:00
Show Gist options
  • Save ynadeem/d8caee0b62ccc95ff9e0dabbe2da7919 to your computer and use it in GitHub Desktop.
Save ynadeem/d8caee0b62ccc95ff9e0dabbe2da7919 to your computer and use it in GitHub Desktop.
Automatically Send Email in Outlook With Optional Attachment (Windows)
import win32com.client as win32
def send_email(recipientList, mailSubject, mailBody, attachmentPath = None):
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = recipientList
mail.Subject = mailSubject
mail.body = mailBody
if attachmentPath:
mail.Attachments.Add(Source = attachmentPath)
print(f"Sending email with {attachmentPath} attached")
else:
print("Nothing attached: Sending email without attachment.")
mail.send
### EXAMPLE
# recipientList = '[email protected]; [email protected]'
# mailSubject = 'Test Auto Email'
# mailBody = 'This is an auto email.'
# attachmentPath = 'C:/Users/me/Desktop/The Spreadsheet.xlsx'
# send_email(recipientList, mailSubject, mailBody, attachmentPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment