Finalize alerting function

This commit is contained in:
Tristan Le Chanony 2020-01-03 13:20:34 +01:00
parent 8a94ebcd87
commit c0d1391c92
1 changed files with 22 additions and 14 deletions

View File

@ -16,14 +16,15 @@ log_filepath = get_parameter("log_filepath", config_file)
log = init_log(log_filepath)
mastodon = init_mastodon(config_file, secrets_filepath)
email_host = get_parameter('email_host', config_file)
email_port = get_parameter('email_port', config_file)
email_from = get_parameter('email_from', config_file)
email_fromname = get_parameter('email_fromname', config_file)
email_username = get_parameter('email_username', config_file)
email_password = get_parameter('email_password', config_file)
pprint(email_fromname)
email = {
'host': get_parameter('email_host', config_file),
'port': get_parameter('email_port', config_file),
'from': get_parameter('email_from', config_file),
'fromname': get_parameter('email_fromname', config_file),
'username': get_parameter('email_username', config_file),
'password': get_parameter('email_password', config_file),
'encryption': get_parameter('email_encryption', config_file)
}
class bcolors:
HEADER = '\033[95m'
@ -64,9 +65,9 @@ def alert_accounts(days):
today = datetime.now(timezone.utc)
if today - last_activity > timedelta(int(days)):
log.info("User "+username+" https://miaou.drycat.fr/admin/accounts/"+str(uid)+" will be delete")
sender = email_from
sender = email['from']
receivers = [''+mail+'']
message = """From: """+email_fromname+""" <"""+email_from+""">
message = """From: """+email['fromname']+""" <"""+email['from']+""">
To: """+username+""" <"""+mail+""">
Subject: Your account has been inactive for more than 365 days
@ -82,7 +83,7 @@ Sinon, connectez vous et par messure de sécurité, envoyez un message privée
Sans action de votre part, votre compte sera clôturé sous 7 jours.
Cordialement
L'équipe d'administration de """+email_fromname+""".
L'équipe d'administration de """+email['fromname']+""".
--------
Hello
@ -93,12 +94,19 @@ Otherwise, log in and by security measure, send a private message to yourself.
Without action on your part, your account will be closed within 7 days.
Cordially
The """+email_fromname+""" administration team.
The """+email['fromname']+""" administration team.
"""
try:
smtpObj = smtplib.login(email_username, email_password)
smtpObj = smtplib.SMTP('127.0.0.1', '1025')
smtpObj = smtplib.SMTP(email['host'], email['port'])
smtpObj.ehlo()
if email['encryption'] == "SSL":
smtpObj.ssl()
elif email['encryption'] == "STARTTLS":
smtpObj.starttls()
else:
log.warning("No encryption select. It's dangerous")
smtpObj.login(email['username'], email['password'])
smtpObj.sendmail(sender, receivers, message.replace("\xe9", "").encode("ascii", errors="ignore"))
log.info("Successfully sent email")
except smtplib.SMTPException as e: