-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathreporter.py
39 lines (32 loc) · 1.34 KB
/
reporter.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
# -*- coding: utf-8 -*-
# 2018.11.23 11:07:07 by nobody
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class Reporter:
def __init__(self, email_from, smtp_server, smtp_port, email_username=None, email_password=None):
self.email_from = email_from
self.smtp_server = smtp_server
self.smtp_port = smtp_port
self.email_username = email_username
self.email_password = email_password
self.sent_emails_counter = 0
def _send_email(self, email, email_to_string):
mail_server = smtplib.SMTP(host=self.smtp_server, port=self.smtp_port)
if int(self.smtp_port) != 25:
mail_server.starttls()
if self.email_username is not None:
mail_server.login(self.email_username, self.email_password)
try:
mail_server.sendmail(self.email_from, email_to_string, email.as_string())
self.sent_emails_counter += 1
finally:
mail_server.close()
def alert(self, content, email_to_string):
email = MIMEMultipart('alternative')
email['Subject'] = "Github Monitor"
email['From'] = self.email_from
email['To'] = email_to_string
part_html = MIMEText(content, 'html', 'utf-8')
email.attach(part_html)
self._send_email(email, email_to_string)