2018-01-15 17:43:32 +00:00
|
|
|
#!/usr/local/lib/mailinabox/env/bin/python
|
2016-01-02 22:00:51 +00:00
|
|
|
|
|
|
|
# Reads in STDIN. If the stream is not empty, mail it to the system administrator.
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
2018-02-24 14:19:00 +00:00
|
|
|
import html
|
2016-01-02 22:00:51 +00:00
|
|
|
import smtplib
|
2018-02-24 14:19:00 +00:00
|
|
|
|
|
|
|
from email.mime.multipart import MIMEMultipart
|
|
|
|
from email.mime.text import MIMEText
|
|
|
|
|
2020-10-04 15:35:59 +00:00
|
|
|
from pgp import create_signature
|
|
|
|
|
2018-02-24 14:19:00 +00:00
|
|
|
# In Python 3.6:
|
|
|
|
#from email.message import Message
|
2016-01-02 22:00:51 +00:00
|
|
|
|
|
|
|
from utils import load_environment
|
|
|
|
|
|
|
|
# Load system environment info.
|
|
|
|
env = load_environment()
|
|
|
|
|
|
|
|
# Process command line args.
|
|
|
|
subject = sys.argv[1]
|
|
|
|
|
|
|
|
# Read in STDIN.
|
|
|
|
content = sys.stdin.read().strip()
|
|
|
|
|
|
|
|
# If there's nothing coming in, just exit.
|
|
|
|
if content == "":
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
# create MIME message
|
2018-02-24 14:19:00 +00:00
|
|
|
msg = MIMEMultipart('alternative')
|
|
|
|
|
|
|
|
# In Python 3.6:
|
|
|
|
#msg = Message()
|
|
|
|
|
2021-03-08 23:54:31 +00:00
|
|
|
noreply = "noreply-daemon@" + env['PRIMARY_HOSTNAME']
|
|
|
|
admin_addr = "administrator@" + env['PRIMARY_HOSTNAME']
|
|
|
|
|
2021-03-07 20:37:43 +00:00
|
|
|
msg['From'] = "\"%s\" <%s>" % ("System Management Daemon", "noreply-daemon@" + env['PRIMARY_HOSTNAME'])
|
|
|
|
msg['To'] = "administrator@" + env['PRIMARY_HOSTNAME']
|
2016-01-02 22:00:51 +00:00
|
|
|
msg['Subject'] = "[%s] %s" % (env['PRIMARY_HOSTNAME'], subject)
|
2018-02-24 14:19:00 +00:00
|
|
|
|
|
|
|
content_html = "<html><body><pre>{}</pre></body></html>".format(html.escape(content))
|
|
|
|
|
2021-03-29 23:03:21 +00:00
|
|
|
msg.attach(MIMEText(create_signature(content.encode()).decode(), 'plain'))
|
2018-02-24 14:19:00 +00:00
|
|
|
msg.attach(MIMEText(content_html, 'html'))
|
|
|
|
|
|
|
|
# In Python 3.6:
|
|
|
|
#msg.set_content(content)
|
|
|
|
#msg.add_alternative(content_html, "html")
|
2016-01-02 22:00:51 +00:00
|
|
|
|
|
|
|
# send
|
2016-05-06 13:06:52 +00:00
|
|
|
smtpclient = smtplib.SMTP('127.0.0.1', 25)
|
2016-01-02 22:00:51 +00:00
|
|
|
smtpclient.ehlo()
|
|
|
|
smtpclient.sendmail(
|
2021-03-08 23:54:31 +00:00
|
|
|
noreply, # MAIL FROM
|
2016-01-02 22:00:51 +00:00
|
|
|
admin_addr, # RCPT TO
|
|
|
|
msg.as_string())
|
|
|
|
smtpclient.quit()
|