You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.4 KiB
81 lines
2.4 KiB
import smtplib, ssl, sys, requests, csv, time, zipfile, os, env
|
|
sys.path.append("./env.py")
|
|
from env import serveurMailPassword, serveurPort, serveurSmtp, serveurAdrresseMail, mailReception
|
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
from email.mime.text import MIMEText
|
|
from email.mime.application import MIMEApplication
|
|
|
|
|
|
# FONCTION ZIP
|
|
def zip_it():
|
|
zip = zipfile.ZipFile('./logs/stack-alert-logs.zip', mode='w')
|
|
zip.write('./logs/python-alert.log')
|
|
zip.close()
|
|
|
|
# FONCTION IMPORT URL FROM CSV + TEST REQUEST
|
|
def import_mails():
|
|
|
|
print(time.ctime())
|
|
mails = []
|
|
with open('./autres-destinataires.csv', newline='') as file:
|
|
reader = csv.reader(file)
|
|
for row in reader:
|
|
# NETTOYAGE
|
|
addr = str(row)
|
|
mailraw = addr.strip('[]\'')
|
|
mails.append(mailraw)
|
|
|
|
return mails
|
|
|
|
# FONCTION CLEAN OLD LOGS
|
|
def clean_logs():
|
|
if os.path.exists('./logs/python-alert.log'):
|
|
os.remove('/./logs/python-alert.log')
|
|
if os.path.exists('/./logs/stack-alert-logs.zip'):
|
|
os.remove('./logs/stack-alert-logs.zip')
|
|
|
|
# FONCTION SEND MAIL
|
|
def send_mail(mails):
|
|
|
|
# EN-TETE EMAIL
|
|
message = MIMEMultipart('mixed')
|
|
message['From'] = serveurAdrresseMail
|
|
message['To'] = mailReception
|
|
message['CC'] = ",".join(mails)
|
|
message['Subject'] = 'Rapport hebdo sites HS'
|
|
|
|
# CONTENU (TEXT)
|
|
contenu = '<h4>Bonjour, voici logs des sites HS de la semaine <br></h4>\n'
|
|
|
|
body = MIMEText(contenu, 'html')
|
|
message.attach(body)
|
|
|
|
# AJOUT PIECE JOINTE
|
|
attachmentPath = "/workspace/python-alert-stack/logs/stack-alert-logs.zip"
|
|
|
|
try:
|
|
with open(attachmentPath, "rb") as attachment:
|
|
p = MIMEApplication(attachment.read(),_subtype="zip")
|
|
p.add_header('Content-Disposition', "attachment; filename= %s" % attachmentPath.split("\\")[-1])
|
|
message.attach(p)
|
|
except Exception as e:
|
|
print(str(e))
|
|
|
|
# CONVERSION EN CHAINE DE CARACTERES
|
|
mail_str = message.as_string()
|
|
context = ssl.create_default_context()
|
|
|
|
# ENVOIE DU MAIL
|
|
with smtplib.SMTP_SSL(serveurSmtp, serveurPort, context=context) as server:
|
|
server.login(serveurAdrresseMail, serveurMailPassword)
|
|
server.sendmail(serveurAdrresseMail,
|
|
mails,
|
|
mail_str)
|
|
server.quit()
|
|
|
|
# MAIN
|
|
mails = import_mails()
|
|
zip_it()
|
|
send_mail(mails)
|
|
clean_logs() |