-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_sender.py
41 lines (32 loc) · 947 Bytes
/
email_sender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
def outlook_is_running():
import win32ui
try:
win32ui.FindWindow(None, "Microsoft Outlook")
return True
except win32ui.error:
return False
def open_outlook():
try:
os.startfile("outlook")
except:
print("Outlook didn't open successfully")
def send_email(emailTo, subject, body, attachment, shouldSend=True):
import win32com.client
from win32com.client import Dispatch
if not outlook_is_running():
open_outlook()
outlook = win32com.client.Dispatch("Outlook.Application")
email = outlook.CreateItem(0x0)
email.Subject = subject
email.BodyFormat = 2
email.HTMLBody = body
email.To = emailTo
if attachment:
email.Attachments.Add(attachment)
if shouldSend:
email.Send()
else:
email.display()
if __name__ == "__main__":
send_email("[email protected]", "Test Subject", "Test Body", None, False)