forked from 1a1a11a/taskSubmitter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilEmail.py
More file actions
executable file
·221 lines (166 loc) · 8.46 KB
/
utilEmail.py
File metadata and controls
executable file
·221 lines (166 loc) · 8.46 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
import os
import sys
import time
import configparser
import smtplib
import subprocess
class configClass:
"""
a class for config, currently gives a default account, please don't change the password
"""
def __init__(self):
self.receiver = None
self.sender = "tasksubmitter0817@gmail.com"
self.sender_pass = "IamTaskSubmitter"
self.sender_name = "taskSubmitter"
self.telegram_api_address = "https://integram.org/caLeNAvl6fq"
# currently only support gmail
self.email_service = "gmail"
self.load_config()
def load_config(self):
"""
load config from ~/.taskSubmitter_config or /.taskSubmitter_config,
user level config is preferred
"""
# gmail may not allow this by default, see https://support.google.com/accounts/answer/6010255?hl=en
if os.path.exists(os.path.expanduser("~/.taskSubmitter_config")):
self.config_loc = os.path.expanduser("~/.taskSubmitter_config")
elif os.path.exists("/.taskSubmitter_config"):
self.config_loc = "/.taskSubmitter_config"
else:
err_msg = '''
you didn't provide config file, please copy and edit the following lines,
then save it to $HOME/.taskSubmitter_config or /.taskSubmitter_config,
remember to change the permission of the file to 600, so others won't see your password
optionally you can use an email dedicatd to sending email
[info]
receiver=PUT_THE_EMAIL_ADDRESS_YOU_WANT_TO_RECEIVE_EMAIL_NOTIFICATION
sender=PUT_YOUR_EMAIL_ADDRESS_HERE(it needs to support smtp, currently only support gmail)
sender_pass=YOUR PASSWORD
sender_name=OPTIONAL
'''
raise RuntimeError(err_msg)
self.cparser = configparser.ConfigParser()
self.cparser.read(self.config_loc)
#assert "receiver" in self.cparser["info"], "Please provide reciever email address in config"
if "receiver" in self.cparser["info"] and len(self.cparser["info"]["receiver"]):
self.receiver = self.cparser["info"]["receiver"]
if "sender" in self.cparser["info"] and len(self.cparser["info"]["sender"]):
self.sender = self.cparser["info"]["sender"]
self.sender_pass = self.cparser["info"]["sender_pass"]
if "sender_name" in self.cparser["info"] and len(self.cparser["info"]["sender_name"]):
self.sender_name = self.cparser["info"]["sender_name"]
if "receiver" in self.cparser["info"] and len(self.cparser["info"]["receiver"]):
self.receiver = self.cparser["info"]["receiver"]
if "email_service" in self.cparser["info"] and len(self.cparser["info"]["email_service"]):
self.email_service = self.cparser["info"]["email_service"]
if "telegram_api_address" in self.cparser["info"] and len(self.cparser["info"]["telegram_api_address"]):
self.telegram_api_address = self.cparser["info"]["telegram_api_address"]
def __str__(self):
return "sender_addr: {}\npass: {}\nname: {}\nreceiver: {}".format(self.sender,
self.sender_pass, self.sender_name, self.receiver)
def __repr__(self):
return self.__str__()
class emailConst:
""" some constants and templates used in the module
"""
smtp_server_gmail = "smtp.gmail.com"
smtp_port_gmail = 587
message_template = "From: From {sender_name} <{sender}>\r\n"\
"To: To {receiver} <{receiver}>\r\n"\
"MIME-Version: 1.0\r\n"\
"Content-type: text/html\r\n"\
"Subject: {topic}\r\n\r\n{message}"
class emailClient:
def __init__(self, smtp_server,
login_username, login_password, sender_name=None,
smtp_port=25, use_SSL=False, use_TLS=False):
"""
initialize a class for sending emails
"""
self.smtp_server = smtp_server
self.smtp_port = smtp_port
self.login_username = login_username
self.login_password = login_password
if sender_name is None:
self.sender_name = self.login_username
else:
self.sender_name = sender_name
self.use_SSL = use_SSL
self.use_TLS = use_TLS
if use_SSL:
self.email_server = smtplib.SMTP_SSL(smtp_server, smtp_port)
else:
self.email_server = smtplib.SMTP(smtp_server, smtp_port)
if use_TLS:
self.email_server.starttls()
try:
# server.ehlo()
# server.starttls()
self.email_server.login(login_username, login_password)
except Exception as e:
print("failed to initialize email client: {}".format(e), file=sys.stderr)
exit(1)
def send_email(self, message, receiver=None, topic="No topic"):
if self.config.email_service == "gmail":
if receiver is None:
assert self.receiver is not None, "please provide receiver email"
receiver = self.receiver
try:
self.email_server.sendmail(self.login_username, receiver,
emailConst.message_template.format(sender=self.login_username
, sender_name=self.sender_name
, receiver=receiver
, topic=topic
, message=message))
except Exception as e:
print("ERROR: failed to send email {}".format(e), file=sys.stderr)
exit(1)
if self.config.email_service == "telegram":
try:
send_command = 'curl -d \'{"text":"' + '*⚞' + topic + ':⚟*<br/>' + message + '"}\' -H "Content-Type: application/json" -X POST ' + self.telegram_api_address
subprocess.call(send_command, shell=True)
print(send_command)
except Exception as e:
print("ERROR: failed to send telegram {}".format(e), file=sys.stderr)
exit(1)
def close(self):
self.email_server.quit()
def __exit__(self):
self.close()
class gmailClient(emailClient):
def __init__(self, login_username, login_password, sender_name=None):
super(gmailClient, self).__init__(emailConst.smtp_server_gmail,
login_username,
login_password,
sender_name,
smtp_port=emailConst.smtp_port_gmail,
use_SSL=False, use_TLS=True)
class defaultEmailClient(emailClient):
def __init__(self):
super(defaultEmailClient, self).__init__(emailConst.smtp_server_gmail,
"tasksubmitter0817@gmail.com",
"IamTaskSubmitter",
sender_name="email_sender",
smtp_port=emailConst.smtp_port_gmail,
use_SSL=False, use_TLS=True)
class configEmailClient(emailClient):
def __init__(self):
self.config = configClass()
print(self.config)
#assert self.config.email_service == "gmail", "only gmail is supported for now"
if self.config.email_service == "gmail":
super(configEmailClient, self).__init__(emailConst.smtp_server_gmail,
self.config.sender,
self.config.sender_pass,
sender_name=self.config.sender_name,
smtp_port=emailConst.smtp_port_gmail,
use_SSL=False, use_TLS=True)
self.receiver = self.config.receiver
if self.config.email_service == "telegram":
self.telegram_api_address = self.config.telegram_api_address
if __name__ == "__main__":
client = defaultEmailClient()
client.send_email("peter.waynechina@gmail.com", sys.argv[1])
client.close()