Search code examples
pythonamazon-web-servicessmtplib

Gmail blocks python code trying to login from amazon server


I have a python code which crawls some websites and send me some emails. The code is working fine on my computer. Now I am trying to run the same code on an Amazon web server. However, google blocks the code. Is there any workaround?

import smtplib

# Send email to my personal email address
def send_email(subject, msg):
    try:
        server = smtplib.SMTP('smtp.gmail.com:587')
        server.ehlo()
        server.starttls()
        server.login('e******@gmail.com', 'S****')
        message = 'Subject: {}\n\n{}'.format(subject, msg)
        server.sendmail('e*@gmail.com', 'e*@gmail.com', message)
        server.quit()
        print("Success: Email sent!")
    except:
        print("Email failed to send.")

Solution

  • As answered in the comment section by Furas, Gmail may blocks access from untrusted programs and the programmer may have to create a separated password for these programs. Allow less secure apps to access your Gmail account