2

This one has been baffling me for a while. Can anyone see where i'm going wrong? This code works fine in Python 2.7; however, it breaks when running through a cron (Python 2.6).

def send_email (message, status):
    fromaddr = 'sam@gmail.com'
    toaddrs = 'sam@gmail.com'
    server = SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login('username-example', 'password-example')
    server.sendmail(fromaddr, toaddrs, 'Subject: %s\r\n%s' % (status, message))
    server.quit()

send_email('message text','subject text')

Through the cron it produces this error:

Traceback (most recent call last):
  File "/home/user/weather-script.py", line 30, in <module>
    send_email('message text', 'subject text')
  File "/home/user/weather-script.py", line 11, in send_email
    server.login('username-example', 'password-example')
  File "/usr/lib64/python2.6/smtplib.py", line 589, in login
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, '5.7.14 <https://accounts.google.com    /ContinueSignIn?sarp=1&scc=1&plt=AKgnsbsZn\n5.7.14 KZ5OF6iSxSCasonEce6H27TM-l4ithBaTtxpg8GbcEzJ522-_MUlYZJWIbc-ZwVnuslJOQ\n5.7.14 _Fu7bpO9-xOfqDi-eiSAPRw8_QLth-Z9ytfeWYIJi0Ez8F_p5joplfR7IoXw4V8VisI7pq\n5.7.14 8NTPoVFqvUEldEI5wL8AukhoPpVfDiX25557ky_W7N6UZLb3efGuvnbhrBsmg5gvlzj1DG\n5.7.14 1GchFIA> Please log in via your web browser and then try again.\n5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787\n5.7.14 54 xv2sm104667531pbb.39 - gsmtp')

Thanks for your time. Any help will be greatly appreciated.

Sam.

Sam Perry
  • 2,554
  • 3
  • 28
  • 29
  • Did you follow the instructions in the error message? – Martijn Pieters Nov 30 '13 at 00:12
  • 1
    Thanks for responding, Martijn Peters. I don't understand the error provided. I followed the links: the first asked me to sign into Google, which I did. I got the same error when the cron ran again. The second link was just a blank page. – Sam Perry Nov 30 '13 at 00:29
  • @SamPerry Were you able to solve this? I'm experiencing the exact same problem – sgarza62 Dec 06 '13 at 21:44
  • Ah, nevermind, I found the problem. Gmail was preventing the login for security reasons and sent an email to my secondary email address with instructions on how to clear the security block. – sgarza62 Dec 06 '13 at 23:19
  • @sgarza62, give my answer below a try. – Sam Perry Dec 08 '13 at 09:24

1 Answers1

1

I've finally got it to work. It turns out I just needed to add a couple of extra lines:

server.ehlo()
server.starttls()
server.ehlo()

Therefore, the final script goes:

def send_email (message, status):
    fromaddr = 'sam@gmail.com'
    toaddrs = 'sam@gmail.com'
    server = SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login('username-example', 'password-example')
    server.sendmail(fromaddr, toaddrs, 'Subject: %s\r\n%s' % (status, message))
    server.quit()

send_email('message text','subject text')
Sam Perry
  • 2,554
  • 3
  • 28
  • 29
  • having the same issue this exact error and code? can you explain the .ehlo() method or where you found this? – Dap Jul 09 '14 at 15:38