-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathmail.py
64 lines (52 loc) · 1.81 KB
/
mail.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
61
62
63
64
import smtplib
import socket
from email.mime.text import MIMEText
import datetime
class Email(object):
def __init__(self, email_from, email_to, email_server, email_port, email_password,bot_name):
self.email_from = email_from
self.email_to = email_to
self.email_server = email_server
self.email_port = email_port
self.email_password = email_password
self.bot_name = bot_name
self._last_email_file = r"/tmp/oandabotemail.time"
def _saveLastEmailTimestamp(self):
try:
f = open(self._last_email_file, "w")
f.write(datetime.datetime.now().strftime("%s"))
f.close()
except:
pass
def _loadLastEmailTimestamp(self):
ts = 0
try:
f = open(self._last_email_file, "r")
ts = int(f.read())
f.close()
except:
pass
return ts
def _canEmail(self):
now = int(datetime.datetime.now().strftime("%s"))
then = self._loadLastEmailTimestamp()
if ( now - then <= 30 ):
return False
return True
def Send(self, text):
if not self._canEmail():
self._saveLastEmailTimestamp()
return
hostname = socket.gethostname()
txt = "This is a message from oandabot " + self.bot_name + "("+hostname+")"
txt += text
e = MIMEText(txt, 'plain')
e['Subject'] = "Oandabot " + self.bot_name + "("+hostname+")"
e['From'] = self.email_from
e['To'] = self.email_to
s = smtplib.SMTP(self.email_server,self.email_port)
s.starttls()
s.login(self.email_from,self.email_password)
s.sendmail(self.email_from,self.email_to,e.as_string())
s.quit()
self._saveLastEmailTimestamp()