Repository: ansible/ansible

Issue:
mail sending with STARTTLS broken because auth_flag variable is not updated after the starttls connection
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report

##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
mail
##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.3.1.0
  config file = /home/my/path/ansibles/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.9 (default, Jun 29 2016, 13:08:31) [GCC 4.9.2]

```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

##### OS / ENVIRONMENT
<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
Also mention the specific version of what you are trying to control,
e.g. if this is a network bug the version of firmware on the network device.
-->
Debian 8
##### SUMMARY
<!--- Explain the problem briefly -->
unable to  connect to mail server in ansible version 2.3.1, but it does work with 2.2.1.0
after some debug it seems that the **auth_flag** variable is not updated after the *STARTTLS* connection is established, [here](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/notification/mail.py#L308)
i have tried the connection in a python console, and  `smtp.has_extn('AUTH')` will return *False* before the *smtp.starttls()* method is called, but *True* after it. However, the variable is not updated, though *smtp.ehlo()* is called again.
this makes that when the variable is checked [here](https://github.com/ansible/ansible/blob/devel/lib/ansible/modules/notification/mail.py#L324), the **No Authentication on the server** exception is raised.

##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->
Reproducible with a simple mail task
<!--- Paste example playbooks or commands between quotes below -->
```yaml
  mail:
    host: 'mail.server.net'
    port: 587
    username: "{{from_mail}}"
    password: '************'
    from: "{{from_mail}}"
    to: "{{email}}"
    subject: 'Hi there...'
    charset: 'utf8'
```

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Mail sent
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
fatal: [etc.is]: FAILED! => {"changed": false, "failed": true, "msg": "No Authentication on the server at mail.server.net:587", "rc": 1}

<!--- Paste verbatim command output between quotes below -->
```

```



Discussion:

Resolving patch:
diff --git a/lib/ansible/modules/notification/mail.py b/lib/ansible/modules/notification/mail.py
--- a/lib/ansible/modules/notification/mail.py
+++ b/lib/ansible/modules/notification/mail.py
@@ -312,6 +312,7 @@ def main():
                 try:
                     smtp.starttls()
                     smtp.ehlo()
+                    auth_flag = smtp.has_extn('AUTH')
                     secure_state = True
                 except smtplib.SMTPException:
                     e = get_exception()


Test patch: