pythonemailgmailsmtplibsmtp-auth

Error sending email: raise SMTPAuthenticationError(code, resp)


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()

Solution

  • 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()