From 8f40cbc8678cc0fbeaecb214ed547cc4468aa36e Mon Sep 17 00:00:00 2001 From: Ben Copeland Date: Tue, 29 Apr 2014 16:20:57 +0100 Subject: [PATCH 1/8] Adding username/password for smtp ppansible mail --- library/notification/mail | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/library/notification/mail b/library/notification/mail index 56211c6807e723..9c28f2135cdf97 100644 --- a/library/notification/mail +++ b/library/notification/mail @@ -142,6 +142,8 @@ def main(): module = AnsibleModule( argument_spec = dict( + username = dict(default=None), + password = dict(default=None), host = dict(default='localhost'), port = dict(default='25'), sender = dict(default='root', aliases=['from']), @@ -155,7 +157,9 @@ def main(): charset = dict(default='us-ascii') ) ) - + + username = module.params.get('username') + password = module.params.get('password') host = module.params.get('host') port = module.params.get('port') sender = module.params.get('sender') @@ -174,7 +178,7 @@ def main(): body = subject try: - smtp = smtplib.SMTP(host, port=int(port)) + smtp = smtplib.SMTP_SSL(host, port=int(port)) except Exception, e: module.fail_json(rc=1, msg='Failed to send mail to server %s on port %s: %s' % (host, port, e)) @@ -239,6 +243,8 @@ def main(): composed = msg.as_string() try: + if username is not None: + smtp.login(username, password) smtp.sendmail(sender_addr, set(addr_list), composed) except Exception, e: module.fail_json(rc=1, msg='Failed to send mail to %s: %s' % (", ".join(addr_list), e)) From 11d133e61dd856c7d8fd7609157d12125bbaaa4e Mon Sep 17 00:00:00 2001 From: Ben Copeland Date: Tue, 29 Apr 2014 16:39:51 +0100 Subject: [PATCH 2/8] remove ssl --- library/notification/mail | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/notification/mail b/library/notification/mail index 9c28f2135cdf97..1f9d5a91350c1a 100644 --- a/library/notification/mail +++ b/library/notification/mail @@ -157,7 +157,7 @@ def main(): charset = dict(default='us-ascii') ) ) - + username = module.params.get('username') password = module.params.get('password') host = module.params.get('host') @@ -178,7 +178,7 @@ def main(): body = subject try: - smtp = smtplib.SMTP_SSL(host, port=int(port)) + smtp = smtplib.SMTP(host, port=int(port)) except Exception, e: module.fail_json(rc=1, msg='Failed to send mail to server %s on port %s: %s' % (host, port, e)) From 0c5d03eca9a4adf3b222b3a2ac56e801f3715dd1 Mon Sep 17 00:00:00 2001 From: bhcopeland Date: Wed, 30 Apr 2014 11:49:19 +0100 Subject: [PATCH 3/8] enable ssl with a switcher --- library/notification/mail | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/library/notification/mail b/library/notification/mail index 1f9d5a91350c1a..086ccb582d0b9d 100644 --- a/library/notification/mail +++ b/library/notification/mail @@ -69,6 +69,15 @@ options: - The body of the email being sent. default: $subject required: false + username: + description: + - If the SMTP requires username + default: null + required: false + password: + - If the SMTP requires password + default: null + required: false host: description: - The mail server @@ -80,6 +89,11 @@ options: default: '25' required: false version_added: "1.0" + ssl: + description: + - Where SSL is required from the beginning of the connection. + default: null + required: false attach: description: - A space-separated list of pathnames of files to attach to the message. @@ -146,6 +160,7 @@ def main(): password = dict(default=None), host = dict(default='localhost'), port = dict(default='25'), + ssl = dict(default=None), sender = dict(default='root', aliases=['from']), to = dict(default='root', aliases=['recipients']), cc = dict(default=None), @@ -162,6 +177,7 @@ def main(): password = module.params.get('password') host = module.params.get('host') port = module.params.get('port') + ssl = module.params.get('ssl') sender = module.params.get('sender') recipients = module.params.get('to') copies = module.params.get('cc') @@ -171,14 +187,16 @@ def main(): attach_files = module.params.get('attach') headers = module.params.get('headers') charset = module.params.get('charset') - sender_phrase, sender_addr = parseaddr(sender) if not body: body = subject try: - smtp = smtplib.SMTP(host, port=int(port)) + if ssl is not None: + smtp = smtplib.SMTP_SSL(host, port=int(port)) + else: + smtp = smtplib.SMTP(host, port=int(port)) except Exception, e: module.fail_json(rc=1, msg='Failed to send mail to server %s on port %s: %s' % (host, port, e)) @@ -255,4 +273,4 @@ def main(): # import module snippets from ansible.module_utils.basic import * -main() +main() \ No newline at end of file From ac29d88829e8d124bf64150a738230da2a46687a Mon Sep 17 00:00:00 2001 From: bhcopeland Date: Wed, 30 Apr 2014 12:57:53 +0100 Subject: [PATCH 4/8] improved ssl logic --- library/notification/mail | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/library/notification/mail b/library/notification/mail index 086ccb582d0b9d..a1a0a490c7355f 100644 --- a/library/notification/mail +++ b/library/notification/mail @@ -69,15 +69,6 @@ options: - The body of the email being sent. default: $subject required: false - username: - description: - - If the SMTP requires username - default: null - required: false - password: - - If the SMTP requires password - default: null - required: false host: description: - The mail server @@ -89,11 +80,6 @@ options: default: '25' required: false version_added: "1.0" - ssl: - description: - - Where SSL is required from the beginning of the connection. - default: null - required: false attach: description: - A space-separated list of pathnames of files to attach to the message. @@ -193,10 +179,17 @@ def main(): body = subject try: - if ssl is not None: + if port == '465': smtp = smtplib.SMTP_SSL(host, port=int(port)) + elif port == '25' or port is None: + smtp = smtplib.SMTP(host) else: smtp = smtplib.SMTP(host, port=int(port)) + smtp.ehlo() + if smtp.has_extn('STARTTLS') and port is not '465': + smtp.starttls() + smtp.ehlo() + smtp.login(username, password) except Exception, e: module.fail_json(rc=1, msg='Failed to send mail to server %s on port %s: %s' % (host, port, e)) From fb65f0bbe4327146002c72e91968f232b91df5c4 Mon Sep 17 00:00:00 2001 From: bhcopeland Date: Wed, 30 Apr 2014 13:04:54 +0100 Subject: [PATCH 5/8] oops deleted my description --- library/notification/mail | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/notification/mail b/library/notification/mail index a1a0a490c7355f..b906551569d7ca 100644 --- a/library/notification/mail +++ b/library/notification/mail @@ -69,6 +69,15 @@ options: - The body of the email being sent. default: $subject required: false + username: + description: + - If the SMTP requires username + default: null + required: false + password: + - If the SMTP requires password + default: null + required: false host: description: - The mail server @@ -80,6 +89,11 @@ options: default: '25' required: false version_added: "1.0" + ssl: + description: + - Where SSL is required from the beginning of the connection. + default: null + required: false attach: description: - A space-separated list of pathnames of files to attach to the message. From 86c146aa1c6a74bd7e5046cf97900328361f1b4d Mon Sep 17 00:00:00 2001 From: bhcopeland Date: Thu, 1 May 2014 14:11:55 +0100 Subject: [PATCH 6/8] re-written ssl module --- library/notification/mail | 40 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/library/notification/mail b/library/notification/mail index b906551569d7ca..9fe012e32f6b16 100644 --- a/library/notification/mail +++ b/library/notification/mail @@ -71,11 +71,11 @@ options: required: false username: description: - - If the SMTP requires username + - If SMTP requires username default: null required: false password: - - If the SMTP requires password + - If SMTP requires password default: null required: false host: @@ -89,11 +89,6 @@ options: default: '25' required: false version_added: "1.0" - ssl: - description: - - Where SSL is required from the beginning of the connection. - default: null - required: false attach: description: - A space-separated list of pathnames of files to attach to the message. @@ -136,6 +131,8 @@ EXAMPLES = ''' import os import sys import smtplib +#from ssl import SSLError +import ssl try: from email import encoders @@ -160,7 +157,6 @@ def main(): password = dict(default=None), host = dict(default='localhost'), port = dict(default='25'), - ssl = dict(default=None), sender = dict(default='root', aliases=['from']), to = dict(default='root', aliases=['recipients']), cc = dict(default=None), @@ -177,7 +173,6 @@ def main(): password = module.params.get('password') host = module.params.get('host') port = module.params.get('port') - ssl = module.params.get('ssl') sender = module.params.get('sender') recipients = module.params.get('to') copies = module.params.get('cc') @@ -193,20 +188,17 @@ def main(): body = subject try: - if port == '465': - smtp = smtplib.SMTP_SSL(host, port=int(port)) - elif port == '25' or port is None: - smtp = smtplib.SMTP(host) - else: - smtp = smtplib.SMTP(host, port=int(port)) - smtp.ehlo() - if smtp.has_extn('STARTTLS') and port is not '465': + smtp = smtplib.SMTP_SSL(host, port=int(port)) + except (smtplib.SMTPException, ssl.SSLError): + smtp = smtplib.SMTP(host, port=int(port)) + smtp.ehlo() + if username and password: + if smtp.has_extn('STARTTLS'): smtp.starttls() - smtp.ehlo() - smtp.login(username, password) - except Exception, e: - module.fail_json(rc=1, msg='Failed to send mail to server %s on port %s: %s' % (host, port, e)) - + try: + smtp.login(username, password) + except smtplib.SMTPAuthenticationError: + module.fail_json(msg="Authentication to %s:%s failed, please check your username and/or password" % (host, port)) msg = MIMEMultipart() msg['Subject'] = subject @@ -268,8 +260,6 @@ def main(): composed = msg.as_string() try: - if username is not None: - smtp.login(username, password) smtp.sendmail(sender_addr, set(addr_list), composed) except Exception, e: module.fail_json(rc=1, msg='Failed to send mail to %s: %s' % (", ".join(addr_list), e)) @@ -280,4 +270,4 @@ def main(): # import module snippets from ansible.module_utils.basic import * -main() \ No newline at end of file +main() From ca619695578d68df4f66ed4f1a66ab8016b8334e Mon Sep 17 00:00:00 2001 From: bhcopeland Date: Thu, 1 May 2014 14:18:37 +0100 Subject: [PATCH 7/8] removed out a commented out block. --- library/notification/mail | 1 - 1 file changed, 1 deletion(-) diff --git a/library/notification/mail b/library/notification/mail index 9fe012e32f6b16..8b40aed6251fc9 100644 --- a/library/notification/mail +++ b/library/notification/mail @@ -131,7 +131,6 @@ EXAMPLES = ''' import os import sys import smtplib -#from ssl import SSLError import ssl try: From 19da4315227c6959fb11b2d0205dff0e8963d712 Mon Sep 17 00:00:00 2001 From: bhcopeland Date: Tue, 6 May 2014 21:03:26 +0100 Subject: [PATCH 8/8] added version_added --- library/notification/mail | 2 ++ 1 file changed, 2 insertions(+) diff --git a/library/notification/mail b/library/notification/mail index 8b40aed6251fc9..9d6b646ef78f64 100644 --- a/library/notification/mail +++ b/library/notification/mail @@ -74,10 +74,12 @@ options: - If SMTP requires username default: null required: false + version_added: "1.6" password: - If SMTP requires password default: null required: false + version_added: "1.6" host: description: - The mail server