-
Notifications
You must be signed in to change notification settings - Fork 65
/
py_teamsreporter.py
70 lines (44 loc) · 1.69 KB
/
py_teamsreporter.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
65
66
67
68
69
70
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class NoTeamsForYou(Exception):
pass
class TeamsAPI(object):
__mapping = {
'ChannelName':'[email protected]',
'AnotherChannelName':'[email protected]',
'your default channel':'[email protected]'
}
__From = '<mail address that specifies the sender>'
__SmtpNode = 'youmailserver.company.domain'
__SmtpPort = 25
__smtp_endpoint = None
def __init__(self):
self.__smtp_endpoint = smtplib.SMTP(self.__SmtpNode,self.__SmtpPort)
def sendMessage(self, channel = 'your default channel', subject = 'your default subject', message = None):
if message is None:
raise NoTeamsForYou("No message specified, please try again!")
if not channel in self.__mapping.keys():
raise NoTeamsForYou("You have specified an invalid channel!")
if len(message.split('\n')) > 1:
MailMessage = '<HTML><BODY>'
for line in message.slit('\n'):
MailMessage += "<p>{}</p>".format(line)
MailMessage = '</BODY></HTML>'
else:
MailMessage = '''
<HTML><BODY>
<p>%s</p>
</BODY></HTML>
''' % (message)
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = self.__From
msg['To'] = self.__mapping[channel]
msg.attach(MIMEText(MailMessage,'html'))
self.__smtp_endpoint.sendmail(self.__From,self.__mapping[channel],msg.as_string())
self.__smtp_endpoint.quit()
if __name__ == '__main__':
TAPI = TeamsAPI()
TAPI.sendMessage(message = 'Test message to channel')
TAPI.sendMessage(message = 'Test message to channel',channel = 'your default channel')