7

Im trying to send emails with smtp module, but Im having an error:

File "/usr/lib/python2.7/smtplib.py", in login    
 raise SMTPAuthenticationError(code, resp) smtplib.SMTPAuthenticationError: (534, '5.7.14)...

Someone already had this error? Do you know how to fix?

The code:

def sendNotification():
    recepients_list = "emailsmtplibtest@gmail.com"
    subject = 'Subject'
    message = "Message" 
    sendemail(recepients_list,subject,message)

def sendemail(to_addr_list, subject, message):
    username = 'emailsmtplibtest@gmail.com'
    password = 'passtest'   
    from_addr = 'emailsmtplibtest@gmail.com'    
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login(username,password)
    newmessage = '\r\n'.join([
              'To: %s' %recepient_list,
               'From: %s' % from_addr,
                'Subject: %s' %subject,
                '',
                message
                ])
    try:    
        server.sendemail(from_addr, to_addr_list,newmessage)
        print 'notification sent'
    except:
        print 'error sending notification'
    server.quit()


sendNotification()
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
UserX
  • 1,295
  • 7
  • 25
  • 39
  • If I had to guess, it would be some kind of TLS/SSL issue. Have you went this route yet? – Ryan O'Donnell Mar 30 '15 at 16:58
  • 1
    You can try disabling CAPTCHA or registering an application-specific password in your gmail account: http://joequery.me/guides/python-smtp-authenticationerror/ – Joe Young Mar 30 '15 at 17:09
  • Thanks for your tips, but still dont work! – UserX Mar 30 '15 at 17:33
  • I don't think it would cause the specific error you're reporting, but there is a typo in your code above. I think you should be calling server.sendmail(), not server.sendemail() (Notice the "e" before "mail"). On another note, I just tried sending a mail through gmail using smtplib with essentially the same steps as you listed and it worked for me. I still think the problem lies in the your google account's security settings. – Joe Young Mar 30 '15 at 18:46

4 Answers4

10

Go to Google's Account Security Settings: www.google.com/settings/security

Find the field "Access for less secure apps". Set it to "Allowed".

Try your script again, changing server.sendemail() to server.sendmail()

Joe Young
  • 5,749
  • 3
  • 28
  • 27
  • 3
    Even after enabling for less secured devices, i am not able to login. My password is correct and I am using the same code as mentioned in websites to be correct – proprius Feb 04 '16 at 10:46
  • @proprius same for me. did you figure it out? – Anupam Dec 18 '17 at 07:34
  • use this link that directly landed you on the desired page: [https://www.google.com/settings/security/lesssecureapps](https://www.google.com/settings/security/lesssecureapps) – Aashish Kumar Apr 10 '18 at 13:38
  • @Anupam same for me. Did you figure it out? – Nityesh Agarwal Aug 12 '18 at 16:13
  • @NityeshAgarwal If I remember correctly, I found it was automatically setting it to disabled even when I enabled it. I think I just tried a couple of times and it worked – Anupam Aug 12 '18 at 17:06
  • Actually I found that I was making an extremely stupid mistake by entering my email address as `www.emailaddress@gmail.com` and not `emailaddress@gmail.com`. Thanks anyway @Anupam :) – Nityesh Agarwal Aug 12 '18 at 17:31
1

(534, b'5.7.14 Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 h16sm7090987wrc.89 - gsmtp')

try https://myaccount.google.com/security#connectedapps

Allow less secure apps: ON Some apps and devices use less secure sign-in technology, which could leave your account vulnerable. You can turn off access for these apps (which we recommend) or choose to use them despite the risks.

xiyu liu
  • 21
  • 1
0

None of the less-secure options worked for me. What worked in the end was setting up 2-factor authentication, and then generating an app-specific password. Instructions here: https://support.google.com/domains/answer/9437157

foges
  • 1,239
  • 1
  • 12
  • 16
-1

I had exactly the same problem. Yes, it worked. By enabling your gmail account security setting -> Allow less secure app, I was able to send a simple email from one gmail to another gmail account.

WARNING: Allowing low security for apps accessing your Google account is not recommended by google. It can be a security threat. Turn it OFF after the experiment.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97