Commit 1493903b authored by Colomban Wendling's avatar Colomban Wendling

Allow to configure SMTP host and From address

parent bce8206f
......@@ -8,6 +8,10 @@ config.base_tick = 60
# Email addresses to send to when an alert is triggered
config.emails.to.append('john@localhost')
# The From: address
#config.emails.addr_from = 'Picomon <picomon@domain.tld>
# The SMTP host, with optional :port suffix
#config.emails.smtp_host = 'localhost:25'
# Subject template for state change email notifications
# available substitutions:
......
import socket
from .attrtree import AttrTree
from .checks import Checks
......@@ -13,6 +14,11 @@ config.install_attr('base_tick', 60)
# Email addresses to send to when an alert is triggered
config.install_attr('emails.to', [])
# The From: address
config.install_attr('emails.addr_from',
'Picomon <picomon@%s>' % socket.getfqdn())
# The SMTP host, with optional :port suffix
config.install_attr('emails.smtp_host', 'localhost:25')
# Subject template for state change email notifications
# available substitutions:
......
import socket
import smtplib
from email.mime.text import MIMEText
from collections import defaultdict
......@@ -11,8 +10,6 @@ email.charset.add_charset('utf-8', email.charset.QP, email.charset.QP, 'utf-8')
def send_email_for_check(check):
from . import config
addr_from = "Picomon <picomon@%s>" % socket.getfqdn()
# ensure we do not traceback with unknown substitutions
subject = config.emails.subject_tpl.format_map(
defaultdict(lambda: "<no substitution>",
......@@ -29,13 +26,13 @@ def send_email_for_check(check):
# http://bugs.python.org/issue16948
msg = MIMEText(msg_text.encode('utf-8').decode('latin1'), 'plain', 'utf-8')
msg['Subject'] = subject
msg['From'] = addr_from
msg['From'] = config.emails.addr_from
msg['To'] = ", ".join(config.emails.to)
try:
server = smtplib.SMTP('localhost')
server = smtplib.SMTP(config.emails.smtp_host)
# server.set_debuglevel(1)
server.sendmail(addr_from, config.emails.to, msg.as_string())
server.sendmail(config.emails.addr_from, config.emails.to, msg.as_string())
server.quit()
except Exception as e:
print("Couldn't send email: %s" % str(e), file=stderr)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment