Module: check_mk
Branch: master
Commit: 62c16a385d3074c347a2c9b91656ac46e780f915
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=62c16a385d3074…
Author: Óscar Nájera <on(a)mathias-kettner.de>
Date: Fri Sep 21 09:52:24 2018 +0200
Extract function for setting mail address headers
Change-Id: I919770d142d2cd58e9bb5950f112e8969f0226e6
---
cmk/notification_plugins/asciimail.py | 23 ++---------------------
cmk/notification_plugins/mail.py | 14 +-------------
cmk/notification_plugins/utils.py | 20 ++++++++++++++++++++
3 files changed, 23 insertions(+), 34 deletions(-)
diff --git a/cmk/notification_plugins/asciimail.py
b/cmk/notification_plugins/asciimail.py
index 0a769c4..fb7dd41 100644
--- a/cmk/notification_plugins/asciimail.py
+++ b/cmk/notification_plugins/asciimail.py
@@ -59,25 +59,6 @@ Handler output: $ALERTHANDLEROUTPUT$
tmpl_alerthandler_service_body = "Service: $SERVICEDESC$\n" +
tmpl_alerthandler_host_body
-def build_mail(target, subject, from_address, reply_to, content_txt):
- # The plain text part
- m = MIMEText(content_txt, 'plain', _charset='utf-8')
-
- m['Subject'] = subject
- m['To'] = target
-
- # Set a few configurable headers
- if from_address:
- m['From'] = from_address
-
- if reply_to:
- m['Reply-To'] = reply_to
- elif len(target.split(",")) > 1:
- m['Reply-To'] = target
-
- return m
-
-
def send_mail(m, target, from_address):
cmd = ["/usr/sbin/sendmail"]
if from_address:
@@ -271,8 +252,8 @@ def main():
# Create the mail and send it
from_address = context.get("PARAMETER_FROM")
reply_to = context.get("PARAMETER_REPLY_TO")
- m = build_mail(mailto, subject, from_address, reply_to, content_txt)
-
+ m = utils.set_mail_headers(mailto, subject, from_address, reply_to,
+ MIMEText(content_txt, 'plain',
_charset='utf-8'))
try:
sys.exit(send_mail(m, mailto, from_address))
except Exception, e:
diff --git a/cmk/notification_plugins/mail.py b/cmk/notification_plugins/mail.py
index 72bbfc0..a1f9f1f 100644
--- a/cmk/notification_plugins/mail.py
+++ b/cmk/notification_plugins/mail.py
@@ -514,19 +514,7 @@ def multipart_mail(target, subject, from_address, reply_to,
content_txt, content
part.add_header('Content-Disposition', how, filename=name)
m.attach(part)
- m['Subject'] = subject
- m['To'] = target
-
- # Set a few configurable headers
- if from_address:
- m['From'] = from_address
-
- if reply_to:
- m['Reply-To'] = reply_to
- elif len(target.split(",")) > 1:
- m['Reply-To'] = target
-
- return m
+ return utils.set_mail_headers(target, subject, from_address, reply_to, m)
def send_mail_sendmail(m, target, from_address):
diff --git a/cmk/notification_plugins/utils.py b/cmk/notification_plugins/utils.py
index 620e596..200be9f 100644
--- a/cmk/notification_plugins/utils.py
+++ b/cmk/notification_plugins/utils.py
@@ -102,3 +102,23 @@ def substitute_context(template, context):
# Remove the rest of the variables and make them empty
template = re.sub(r"\$[A-Z_][A-Z_0-9]*\$", "", template)
return template
+
+
+###############################################################################
+# Mail
+
+
+def set_mail_headers(target, subject, from_address, reply_to, mail):
+ mail['Subject'] = subject
+ mail['To'] = target
+
+ # Set a few configurable headers
+ if from_address:
+ mail['From'] = from_address
+
+ if reply_to:
+ mail['Reply-To'] = reply_to
+ elif len(target.split(",")) > 1:
+ mail['Reply-To'] = target
+
+ return mail