-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsend_report
48 lines (38 loc) · 1.56 KB
/
send_report
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
import os
import sys
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
SMTP_SERVER = os.environ.get('SMTP_SERVER', 'smtp.gmail.com')
SMTP_PORT = int(os.environ.get('SMTP_PORT', '465'))
MAIL_USER = os.environ.get('MAIL_USER', None)
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD', None)
TARGET_RESULTS = os.environ.get('TARGET_RESULTS', None)
BRANCH = os.environ.get('BRANCH', None)
if not MAIL_USER or not MAIL_PASSWORD or not TARGET_RESULTS or not BRANCH:
print('Please set the MAIL_USER, MAIL_PASSWORD, TARGET_RESULTS and BRANCH')
print('environment variables before going on')
sys.exit(1)
server = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT)
server.login(MAIL_USER, MAIL_PASSWORD)
with open('%s.eml' % TARGET_RESULTS, 'r') as f:
contents = f.read()
message = MIMEMultipart('mixed')
message.attach(MIMEText(contents))
errors = False
for error_message in os.listdir(TARGET_RESULTS):
with open(os.path.join(TARGET_RESULTS, error_message), 'r') as f:
data = f.read()
part = MIMEApplication(data)
part.add_header('Content-Disposition', 'attachment',
filename=error_message)
message.attach(part)
errors = True
message['From'] = '[email protected]'
message['To'] = '[email protected]'
message['Subject'] = 'Tests for %s : %s' % (BRANCH, 'KO' if errors else 'OK')
message['Charset'] = 'UTF-8'
message['Content-Type'] = 'text/plain; charset=UTF-8'
server.sendmail(message['From'], message['To'], message.as_string())
server.quit()