-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendEmail.py
59 lines (49 loc) · 1.36 KB
/
sendEmail.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import win32com.client as win32
import win32api
from datetime import datetime
import os
def readFile(textPath):
file = open(textPath, 'r')
# read the file line by line save to listText
listText = []
for line in file:
listText.append(line)
# replace '\n' with '<br>'
listText[-1] = listText[-1].replace('\n', '<br>')
# print join the listText with '<br>'
text = ''.join(listText)
# print(listText)
return text
def sendEmail():
# Open the Outlook
outlook = win32.Dispatch('outlook.application')
# Create the email
mail = outlook.CreateItem(0)
# Set the email subject
mail.Subject = 'Subject ' + str(date)
# Set the receiver email
mail.To = "emailTo@example.com"
mail.CC = "emailCC@example.com"
# Add an image or file
mail.Attachments.Add(os.path.join(os.getcwd(), filePath))
# Set the email body
text = readFile(textPath)
# Write the email content
mail.HTMLBody = r"""
<h1>""" + str(date) + """</h1>
<hr>
""" + text + """
"""
# Send the email
mail.Send()
print('The Email is sent.')
def main():
# readFile(textPath)
sendEmail()
if __name__ == '__main__':
textPath = 'D:\\Code\\automation\\savedFile\\20220601.txt'
# customizable file path
filePath = textPath
date = 20220601
# date = datetime.now()
main()