From a13541299e055d17c2762b78dd7bd4e19d7af9c4 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Fri, 24 Sep 2021 07:41:21 +0200 Subject: [PATCH] mail: adding capability to specify ehlo hostname (#3425) (#3427) * Adding capability to specify ehlo hostname * Fixing default for ehlohost the CI did not like "None" as default in the documentation, judging from the rest of the definitions omitting that seems to be right * Update plugins/modules/notification/mail.py Proper spelling in documentation Co-authored-by: Ajpantuso * Adding Changelog Fragment * Update changelogs/fragments/3425-mail_add_configurable_ehlo_hostname.yml Proper phrasing in changelog Co-authored-by: Ajpantuso * integration-test for ehlohost parameter * proper description, increased async-time changed body of ehlohost-mail to reflect ehlohost increased async from 30 to 45 as CI failed because smtp was already down. Co-authored-by: Ajpantuso (cherry picked from commit ae6cbc2d8235a4ef5e8b7b50cb45a397891f0fe7) Co-authored-by: Hartwig Hauschild --- ...25-mail_add_configurable_ehlo_hostname.yml | 2 ++ plugins/modules/notification/mail.py | 25 ++++++++++++++++--- tests/integration/targets/mail/tasks/main.yml | 12 ++++++++- 3 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/3425-mail_add_configurable_ehlo_hostname.yml diff --git a/changelogs/fragments/3425-mail_add_configurable_ehlo_hostname.yml b/changelogs/fragments/3425-mail_add_configurable_ehlo_hostname.yml new file mode 100644 index 0000000000..dbc9cfb276 --- /dev/null +++ b/changelogs/fragments/3425-mail_add_configurable_ehlo_hostname.yml @@ -0,0 +1,2 @@ +minor_changes: + - mail - added the ``ehlohost`` parameter which allows for manual override of the host used in SMTP EHLO (https://github.com/ansible-collections/community.general/pull/3425). diff --git a/plugins/modules/notification/mail.py b/plugins/modules/notification/mail.py index 2f03f8c239..82ca6d52b2 100644 --- a/plugins/modules/notification/mail.py +++ b/plugins/modules/notification/mail.py @@ -125,6 +125,11 @@ options: - Sets the timeout in seconds for connection attempts. type: int default: 20 + ehlohost: + description: + - Allows for manual specification of host for EHLO. + type: str + version_added: 3.8.0 ''' EXAMPLES = r''' @@ -189,6 +194,16 @@ EXAMPLES = r''' subject: Ansible-report body: System {{ ansible_hostname }} has been successfully provisioned. secure: starttls + +- name: Sending an e-mail using StartTLS, remote server, custom EHLO + community.general.mail: + host: some.smtp.host.tld + port: 25 + ehlohost: my-resolvable-hostname.tld + to: John Smith + subject: Ansible-report + body: System {{ ansible_hostname }} has been successfully provisioned. + secure: starttls ''' import os @@ -215,6 +230,7 @@ def main(): password=dict(type='str', no_log=True), host=dict(type='str', default='localhost'), port=dict(type='int', default=25), + ehlohost=dict(type='str', default=None), sender=dict(type='str', default='root', aliases=['from']), to=dict(type='list', elements='str', default=['root'], aliases=['recipients']), cc=dict(type='list', elements='str', default=[]), @@ -235,6 +251,7 @@ def main(): password = module.params.get('password') host = module.params.get('host') port = module.params.get('port') + local_hostname = module.params.get('ehlohost') sender = module.params.get('sender') recipients = module.params.get('to') copies = module.params.get('cc') @@ -259,9 +276,9 @@ def main(): if secure != 'never': try: if PY3: - smtp = smtplib.SMTP_SSL(host=host, port=port, timeout=timeout) + smtp = smtplib.SMTP_SSL(host=host, port=port, local_hostname=local_hostname, timeout=timeout) else: - smtp = smtplib.SMTP_SSL(timeout=timeout) + smtp = smtplib.SMTP_SSL(local_hostname=local_hostname, timeout=timeout) code, smtpmessage = smtp.connect(host, port) secure_state = True except ssl.SSLError as e: @@ -273,9 +290,9 @@ def main(): if not secure_state: if PY3: - smtp = smtplib.SMTP(host=host, port=port, timeout=timeout) + smtp = smtplib.SMTP(host=host, port=port, local_hostname=local_hostname, timeout=timeout) else: - smtp = smtplib.SMTP(timeout=timeout) + smtp = smtplib.SMTP(local_hostname=local_hostname, timeout=timeout) code, smtpmessage = smtp.connect(host, port) except smtplib.SMTPException as e: diff --git a/tests/integration/targets/mail/tasks/main.yml b/tests/integration/targets/mail/tasks/main.yml index dbde6743d2..11c810da30 100644 --- a/tests/integration/targets/mail/tasks/main.yml +++ b/tests/integration/targets/mail/tasks/main.yml @@ -26,7 +26,7 @@ # This would require either dumping the content, or registering async task output - name: Start test smtpserver shell: '{{ ansible_python.executable }} {{ remote_tmp_dir }}/smtpserver.py 10025:10465' - async: 30 + async: 45 poll: 0 register: smtpserver @@ -88,3 +88,13 @@ - fail: msg: Send mail using TLS failed. when: smtpd_tls is succeeded and tls_support is failed and starttls_support is succeeded + +- name: Send a test-mail with body, specific recipient and specific ehlohost + mail: + port: 10025 + ehlohost: some.domain.tld + from: ansible@localhost + to: root@localhost + subject: Test mail 6 (smtp + body + ehlohost) + body: Test body 6 + secure: never