3
def send_email(user, pwd, recipient, subject, body):
    import smtplib

    gmail_user = user
    gmail_pwd = pwd
    FROM = user
    TO = ['dhe*****@***.com']
    SUBJECT = subject
    TEXT = body

    # Prepare actual message
    message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.ehlo()
    server.starttls()
    server.login(gmail_user, gmail_pwd)
    server.sendmail(FROM, TO, message)
    server.close()
    print 'successfully sent the mail'

if __name__ == '__main__':
    send_email('abc@gmail.com', 'password', '', 'Hello', 'hello')

I am using this script to send email from abc@gmail.com. (hardcoded the recipient for now)

But I am getting

smtplib.SMTPAuthenticationError: (534, '5.7.14 https://accounts.google.com/signin/ ..... 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 dl17sm699785obc.9 - gsmtp')

Access for Less secure apps is already turned on for that account (abc@gmail.com). There is also NO 2-factor authentication for that account.

I can't login through a web-browser on that machine because it is an EC2 Instance. How do I proceed here?

0 Answers0