1

I use Python 3.5.2.

I am trying to use my yahoo account to send email. I use the yahoo SMTP server domain name smtp.mail.yahoo.com according to this website `http://neerajbyte.com/send-email-through-python-console-with-gmail-hotmail-and-yahoo/'. However I got an error message (below). Normally for security reasons google, I tried it, would send me an email notifying me of an application trying to access my account and I have to click on a link to allow it. But I didn't get an email from yahoo but just this error message, not sure why.

this is my code:

>>> import smtplib
>>> conn = smtplib.SMTP('smtp.mail.yahoo.com', 587)
>>> type(conn)
<class 'smtplib.SMTP'>
>>> conn
<smtplib.SMTP object at 0x02AD9A70>
>>> conn.ehlo()
(250, b'smtp.mail.yahoo.com\nPIPELINING\nSIZE 41697280\n8 BITMIME\nSTARTTLS')
>>> conn.starttls
<bound method SMTP.starttls of <smtplib.SMTP object at 0x02AD9A70>>
>>> conn.login('j@yahoo.com', 'j2')

this is my error message:

_Traceback (most recent call last):
  File "<pyshell#6>", line 1, in <module>
    conn.login('j@yahoo.com', 'j2')
  File "C:\Users\J\AppData\Local\Programs\Python\Python35-32\lib\smtplib.py", line 696, in login
    "SMTP AUTH extension not supported by server.")
smtplib.SMTPNotSupportedError: SMTP AUTH extension not supported by server.
_
Johnseito
  • 315
  • 1
  • 9
  • 24

3 Answers3

3

To my knowledge, Yahoo and Gmail expect 2-factor authentication when trying to login from apps. These are the steps:

  1. In Yahoo, you can go to Account Info.

  2. Click on the Account Security tab on the left. Enable 2-step verification. I then first time you try this you will get options to select the method of verification. Usually I choose OTP via SMS.

  3. Then you can Manage App Passwords. This will bring up a popup to generate a 16-character password. This is what you can use from your app when logging into your mail.

Below is a sample screenshot: enter image description here

coder.in.me
  • 1,048
  • 9
  • 19
  • How do you choose OTP via SMS ? I follow what you mentioned and can't access still. I enable allow apps that use less secure sign-in. I click on Manage app passwords, and enter Python smtplib which generates a 16 letter password. I enter this 16 letter password into this code (below) and still have the same error message. `conn.login('j@yahoo.com', 'jgjt apbo btwj qcwq')` – Johnseito Dec 01 '16 at 19:22
  • No spaces in the password. Remove the spaces and try. – coder.in.me Dec 02 '16 at 04:20
  • Not sure. What was ur second auth factor if not OTP/SMS? Perhaps you need to logout, clear browser cache and try again. – coder.in.me Dec 02 '16 at 05:24
  • How do I know what my second auth factor is ? And I logged out and clear browser and tried again, same error message. – Johnseito Dec 02 '16 at 06:19
  • Unable to help any further. It works for me. It's strange you are getting the `smtplib.SMTPNotSupportedError` error. – coder.in.me Dec 02 '16 at 07:43
  • 1
    Ok it works for me in gmail but not yahoo. I will do some investigation into this when I get some time. Thanks – Johnseito Dec 02 '16 at 20:25
  • Okay, I figured it out and it has worked for me now. Thank you ! – Johnseito May 15 '17 at 02:35
  • Please share how you solved it. It might help others. – coder.in.me May 15 '17 at 04:14
  • yup, I signed into my yahoo account. then click on the gear to the left. then click on account info then click on account security and on the bottom enable two step verification and type a name, it can be any that you want. After that, it generates 16 character password. Use this password into your code for this email app access. – Johnseito May 15 '17 at 19:04
0

follow the Article https://support.google.com/accounts/answer/185833?hl=en

Create & use App Passwords for gmail notification using ansible mail module.

generate Apps password, and use same 16 digit app password in your anisble playbook

     - name: 'ansible mail module to gmail notification'
      mail:
        host: smtp.gmail.com
        subtype: html
        port: 587
        password: **************
        to: XXXXXXXXXX@gmail.com
        from: YYYYYYYYYY.YYYY@gmail.com
        username: YYYYYYYYYY.YYYY@gmail.com
        subject: User details report
        attach: /tmp/data_details/data_details.csv
        body:  {{ lookup('file', '/tmp/data_details/data_details.csv') }} 
      delegate_to: localhost

now try to send email it should work. i have tested successfully and it's working.

enter image description here

0

another best way to use ansible vault password string variable while writing playbook for mail module. do not compromise security risk.

i have implemented same for sending email using mail module and it's working as expected.

ansible-vault encrypt_string yourgmailapppassword --name gmail_password

use above method to encrypt gmail app password using ansible vault string option and define encrypted variable into the playbook.

cat fetch-users-deatils.yml

    - name: Linux servers user audit report preparation
      hosts: "{{ HOSTS }}"
      roles:
        - user-collections
    
    - name: Refreshing user Dashboard & sending email from localhost
      hosts: localhost
      become: false
      vars:
       - gmail_password: !vault |
              $ANSIBLE_VAULT;1.1;AES256
              62613232383962323430633831113465356231563163366235353034393230656331663436646233
              3266353862303738303737383530313664356135336661390a336562613436626665333833323030
              61393135643433313930643337363465343332353716333831222766376137396430426361663633
              6233313433633231320a663435636230636431643731333166366435346564316331323361633566
              38622138392437888466666535323432653034323936353961646233613437343831
      tasks:
        - name: Collecting the user details information and recreating the users dashboard
          script: dashboard_user.sh
          tags: user_dashboard
    
    
        - name: User Audit data output file stored on below location
          debug:
            msg:
             /tmp/user_collection/user_details.csv
    
        - name: 'Sending Ansible users report email'
          mail:
            host: smtp.gmail.com
            subtype: html
            port: 587
            password: "{{ gmail_password }}"
            to: abcdefghijkl@gmail.com
            from: abcdefghijkl@gmail.com
            username: abcdefghijkl@gmail.com
            subject: User details report
            attach: /tmp/user_collection/user_details.csv
            body: <pre> {{ lookup('file', '/tmp/user_collection/user_details.csv') }} </pre>
          delegate_to: localhost

below is ansible playbook execution command

ansible-playbook fetch-users-deatils.yml -e "HOSTS=all" --ask-vault-pass