-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmail.py
More file actions
executable file
·60 lines (51 loc) · 1.92 KB
/
gmail.py
File metadata and controls
executable file
·60 lines (51 loc) · 1.92 KB
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
#!/bin/python3.6
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class Gmail():
def sendFile(self, subject, file):
try:
filetxt = open(file, "r")
msgtext = MIMEText(filetxt.read(), "plain")
to = "Alerts"
recipients = ["@gmail.com"]
msg = MIMEMultipart()
msg['To'] = to
msg['From'] = "Alerts"
msg['Subject'] = ' %s ' % subject
msg.attach(msgtext)
filetxt.close()
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("@gmail.com", "xxx") # Add Google App password to login method
server.sendmail("@gmail.com", recipients, msg.as_string())
server.quit()
print("Gmail sendFile completed successfully.\n")
return True
except Exception as e:
mess = "Error! - Gmail.sendFile() through an exception " + str(e)
print(mess)
self.sendText(mess, mess)
return False
def sendText(self, subject, message):
try:
msgtext = MIMEText(message, "plain")
to = "Alerts"
recipients = ["@gmail.com"]
msg = MIMEMultipart()
msg['To'] = to
msg['From'] = "Alerts"
msg['Subject'] = ' %s ' % subject
msg.attach(msgtext)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("@gmail.com", "xxx")
server.sendmail("@gmail.com", recipients, msg.as_string())
server.quit()
print("Gmail sendText completed successfully.\n")
return True
except Exception as e:
mess = "Error! - Gmail.sendText() through an exception " + str(e)
print(mess)
self.sendText(mess, mess)
return False