fix handling of some exceptions for mail plugin
redid the flow to simplify and dedupe code, also now we capture extra exceptions previouslly causing traceback see https://groups.google.com/forum/?#!msg/ansible-project/qhalnkYZMfI/93n8hlEpBAAJ
This commit is contained in:
parent
886c4edfb9
commit
72b0ed8cb4
1 changed files with 21 additions and 33 deletions
|
@ -245,46 +245,29 @@ def main():
|
|||
if not body:
|
||||
body = subject
|
||||
|
||||
smtp = smtplib.SMTP(timeout=timeout)
|
||||
|
||||
if secure in ('never', 'starttls', 'try'):
|
||||
try:
|
||||
code, smtpmessage = smtp.connect(host, port=port)
|
||||
except smtplib.SMTPException as e:
|
||||
if secure == 'try':
|
||||
try:
|
||||
smtp = smtplib.SMTP_SSL(timeout=timeout)
|
||||
code, smtpmessage = smtp.connect(host, port=port)
|
||||
secure_state = True
|
||||
except ssl.SSLError as e:
|
||||
try:
|
||||
if secure != 'never':
|
||||
try:
|
||||
smtp = smtplib.SMTP_SSL(timeout=timeout)
|
||||
code, smtpmessage = smtp.connect(host, port=port)
|
||||
secure_state = True
|
||||
except ssl.SSLError as e:
|
||||
if secure == 'always':
|
||||
module.fail_json(rc=1, msg='Unable to start an encrypted session to %s:%s: %s' %
|
||||
(host, port, to_native(e)), exception=traceback.format_exc())
|
||||
else:
|
||||
module.fail_json(rc=1, msg='Unable to Connect to %s:%s: %s' %
|
||||
(host, port, to_native(e)), exception=traceback.format_exc())
|
||||
(host, port, to_native(e)), exception=traceback.format_exc())
|
||||
|
||||
if (secure == 'always'):
|
||||
try:
|
||||
smtp = smtplib.SMTP_SSL(timeout=timeout)
|
||||
if not secure_state:
|
||||
smtp = smtplib.SMTP(timeout=timeout)
|
||||
code, smtpmessage = smtp.connect(host, port=port)
|
||||
secure_state = True
|
||||
except ssl.SSLError as e:
|
||||
module.fail_json(rc=1, msg='Unable to start an encrypted session to %s:%s: %s' %
|
||||
(host, port, to_native(e)), exception=traceback.format_exc())
|
||||
|
||||
except smtplib.SMTPException as e:
|
||||
module.fail_json(rc=1, msg='Unable to Connect %s:%s: %s' % (host, port, to_native(e)), exception=traceback.format_exc())
|
||||
|
||||
if int(code) > 0:
|
||||
try:
|
||||
smtp.ehlo()
|
||||
except smtplib.SMTPException as e:
|
||||
module.fail_json(rc=1, msg='Helo failed for host %s:%s: %s' %
|
||||
(host, port, to_native(e)), exception=traceback.format_exc())
|
||||
|
||||
if secure in ('starttls', 'try'):
|
||||
if not secure_state and secure in ('starttls', 'try'):
|
||||
if smtp.has_extn('STARTTLS'):
|
||||
try:
|
||||
smtp.starttls()
|
||||
smtp.ehlo()
|
||||
auth_flag = smtp.has_extn('AUTH')
|
||||
secure_state = True
|
||||
except smtplib.SMTPException as e:
|
||||
module.fail_json(rc=1, msg='Unable to start an encrypted session to %s:%s: %s' %
|
||||
|
@ -292,6 +275,10 @@ def main():
|
|||
else:
|
||||
if secure == 'starttls':
|
||||
module.fail_json(rc=1, msg='StartTLS is not offered on server %s:%s' % (host, port))
|
||||
try:
|
||||
smtp.ehlo()
|
||||
except smtplib.SMTPException as e:
|
||||
module.fail_json(rc=1, msg='Helo failed for host %s:%s: %s' % (host, port, to_native(e)), exception=traceback.format_exc())
|
||||
|
||||
if username and password:
|
||||
if smtp.has_extn('AUTH'):
|
||||
|
@ -319,7 +306,7 @@ def main():
|
|||
h_key, h_val = hdr.split('=')
|
||||
h_val = to_native(Header(h_val, charset))
|
||||
msg.add_header(h_key, h_val)
|
||||
except:
|
||||
except Exception:
|
||||
module.warn("Skipping header '%s', unable to parse" % hdr)
|
||||
|
||||
if 'X-Mailer' not in msg:
|
||||
|
@ -375,5 +362,6 @@ def main():
|
|||
|
||||
module.exit_json(msg='Mail sent successfully', result=result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
Loading…
Reference in a new issue