diff --git a/CHANGELOG.md b/CHANGELOG.md index 5101a9d..6d2aeca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG In Development -------------- +* A report of box activity, including sent/received mail totals and logins by user, is now emailed to the box's administrator user each week. * Update Roundcube to version 1.3.4 and Z-Push to version 2.3.9. * The undocumented feature for proxying web requests to another server now sets X-Forwarded-For. diff --git a/management/daily_tasks.sh b/management/daily_tasks.sh index c0e88a8..b5b628c 100755 --- a/management/daily_tasks.sh +++ b/management/daily_tasks.sh @@ -9,6 +9,12 @@ export LC_ALL=en_US.UTF-8 export LANG=en_US.UTF-8 export LC_TYPE=en_US.UTF-8 +# On Mondays, i.e. once a week, send the administrator a report of total emails +# sent and received so the admin might notice server abuse. +if [ `date "+%u"` -eq 1 ]; then + management/mail_log.py -t week | management/email_administrator.py "Mail-in-a-Box Usage Report" +fi + # Take a backup. management/backup.py | management/email_administrator.py "Backup Status" diff --git a/management/email_administrator.py b/management/email_administrator.py index 9e65c92..8ed6e2a 100755 --- a/management/email_administrator.py +++ b/management/email_administrator.py @@ -4,8 +4,14 @@ import sys +import html import smtplib -from email.message import Message + +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText + +# In Python 3.6: +#from email.message import Message from utils import load_environment @@ -26,11 +32,23 @@ if content == "": sys.exit(0) # create MIME message -msg = Message() +msg = MIMEMultipart('alternative') + +# In Python 3.6: +#msg = Message() + msg['From'] = "\"%s\" <%s>" % (env['PRIMARY_HOSTNAME'], admin_addr) msg['To'] = admin_addr msg['Subject'] = "[%s] %s" % (env['PRIMARY_HOSTNAME'], subject) -msg.set_payload(content, "UTF-8") + +content_html = "
{}".format(html.escape(content)) + +msg.attach(MIMEText(content, 'plain')) +msg.attach(MIMEText(content_html, 'html')) + +# In Python 3.6: +#msg.set_content(content) +#msg.add_alternative(content_html, "html") # send smtpclient = smtplib.SMTP('127.0.0.1', 25) diff --git a/management/mail_log.py b/management/mail_log.py index 0c9955a..106d70c 100755 --- a/management/mail_log.py +++ b/management/mail_log.py @@ -76,7 +76,8 @@ def scan_files(collector): tmp_file = tempfile.NamedTemporaryFile() shutil.copyfileobj(gzip.open(fn), tmp_file) - print("Processing file", fn, "...") + if VERBOSE: + print("Processing file", fn, "...") fn = tmp_file.name if tmp_file else fn for line in reverse_readline(fn): @@ -119,8 +120,8 @@ def scan_mail_log(env): except ImportError: pass - print("Scanning from {:%Y-%m-%d %H:%M:%S} back to {:%Y-%m-%d %H:%M:%S}".format( - START_DATE, END_DATE) + print("Scanning logs from {:%Y-%m-%d %H:%M:%S} to {:%Y-%m-%d %H:%M:%S}".format( + END_DATE, START_DATE) ) # Scan the lines in the log files until the date goes out of range @@ -138,8 +139,8 @@ def scan_mail_log(env): # Print Sent Mail report if collector["sent_mail"]: - msg = "Sent email between {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}" - print_header(msg.format(END_DATE, START_DATE)) + msg = "Sent email" + print_header(msg) data = OrderedDict(sorted(collector["sent_mail"].items(), key=email_sort)) @@ -173,8 +174,8 @@ def scan_mail_log(env): # Print Received Mail report if collector["received_mail"]: - msg = "Received email between {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}" - print_header(msg.format(END_DATE, START_DATE)) + msg = "Received email" + print_header(msg) data = OrderedDict(sorted(collector["received_mail"].items(), key=email_sort)) @@ -202,8 +203,8 @@ def scan_mail_log(env): # Print login report if collector["logins"]: - msg = "User logins per hour between {:%Y-%m-%d %H:%M:%S} and {:%Y-%m-%d %H:%M:%S}" - print_header(msg.format(END_DATE, START_DATE)) + msg = "User logins per hour" + print_header(msg) data = OrderedDict(sorted(collector["logins"].items(), key=email_sort))