-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsendEmail.py
60 lines (52 loc) · 1.46 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
60
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from os import stat
import os.path
def sendEmail(myEmail,myPass,subject):
contacts = []
message = MIMEMultipart()
if(stat("contacts.txt").st_size == 0):
print("contacts.txt is empty")
else:
# store contacts list
with open("contacts.txt") as getContacts:
contacts = [line.strip() for line in getContacts]
if(stat("msg.txt").st_size == 0):
print("msg.txt is empty")
else:
with open("msg.txt","r") as messageFile:
myMessage = messageFile.read()
print("Do you want to include an image : ? y/n")
answ = input()
if(answ == "y"):
print("Enter image's name :")
imageName = input()
if(os.path.isfile(imageName)):
print("File exists")
fp = open(imageName,"rb")
imageName = MIMEImage(fp.read())
fp.close()
message.attach(imageName)
elif(not os.path.isfile(imageName)):
print("File:",imageName," does not exist")
print("Send email without image?: y/n")
answ = input()
if(answ == "n"):
exit()
else:
print("Could not find image with the name :",imageName)
else:
print("No attachment")
message["Subject"] = subject
message["From"] = myEmail
message["To"] = ", ".join(contacts)
message.attach(MIMEText(myMessage))
server = smtplib.SMTP("smtp.gmail.com",587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(myEmail,myPass)
server.send_message(message)
server.quit()