Created
May 20, 2018 15:00
-
-
Save ynadeem/d8caee0b62ccc95ff9e0dabbe2da7919 to your computer and use it in GitHub Desktop.
Automatically Send Email in Outlook With Optional Attachment (Windows)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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