From b9de989ad4cdf9b347f8da2cd1e15792fc2a45f7 Mon Sep 17 00:00:00 2001 From: Andrea Tartaglia Date: Mon, 11 Sep 2017 07:39:23 -0700 Subject: [PATCH] Fix mail module headers encoding (#29109) * Fixes encoding issue in Subject line * Use Header to correctly set header charset --- lib/ansible/modules/notification/mail.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/ansible/modules/notification/mail.py b/lib/ansible/modules/notification/mail.py index 8856b318198..29a955a1600 100644 --- a/lib/ansible/modules/notification/mail.py +++ b/lib/ansible/modules/notification/mail.py @@ -199,6 +199,7 @@ from email.utils import parseaddr, formataddr from email.mime.base import MIMEBase from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText +from email.header import Header from ansible.module_utils.basic import AnsibleModule from ansible.module_utils._text import to_native @@ -310,14 +311,16 @@ def main(): module.fail_json(rc=1, msg="No Authentication on the server at %s:%s" % (host, port)) msg = MIMEMultipart() - msg['Subject'] = subject - msg['From'] = formataddr((sender_phrase, sender_addr)) + msg['Subject'] = Header(subject, charset) + msg['From'] = Header(formataddr((sender_phrase, sender_addr)), charset) msg.preamble = "Multipart message" + msg.set_charset(charset) if headers is not None: for hdr in [x.strip() for x in headers.split('|')]: try: h_key, h_val = hdr.split('=') + h_val = to_native(Header(h_val, charset)) msg.add_header(h_key, h_val) except: pass