0

I'm trying to write an AWS Lambda function that periodically sends an email using Python's smtplib. This function works outside of AWS lambda, and I've verified that the environment variables are valid many times.

import os
import smtplib

def lambda_handler(event, context):
    """Function that runs to send the email."""
    otf_email = os.environ.get("OTF_EMAIL")
    my_email = os.environ.get("MY_EMAIL")
    pw = os.environ.get("GMAIL_PW")

    body = 'Subject:\nThis is a test from the AWS lambda function.'
    smtp_obj = smtplib.SMTP('smtp.gmail.com', 587)
    smtp_obj.ehlo()
    smtp_obj.starttls()
    smtp_obj.login(my_email, pw)
    smtp_obj.sendmail(my_email, otf_email, body)
    smtp_obj.sendmail(my_email, my_email, body)
    smtp_obj.quit()

The first part of the error:

"errorMessage": "(534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsR\\n5.7.14 vlSLqK014L_ddv0GicpBkQ1o229bk_zYZe8gMUGlddfJLox0EnXFwtUl9GpBygMxCzoATW\\n5.7.14 3UjdqLIvkTcUx6vGO09gE33_CMkdMaVK-F1d8FC4SypPh8n3ft6BaZubjr4b_M7FD2roiN\\n5.7.14 LyTNxCogmPGDqNQP8overGbbDNTZ7rdeEGBYqG9dExVjtqnRda6eEwC9e9Ib8zHfsjASRM\\n5.7.14 Zi8ShH9zxelYTJ-IhALwvPFV0pJIg> 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 u131sm4947518pgc.89 - gsmtp')"
Kurt Maurer
  • 425
  • 1
  • 5
  • 15

1 Answers1

0

AWS provides a service called SES.

You can send outbound email in a scalable way.

If you still want to send email through gmail enable less secure apps option discussed in detail here.

llrs
  • 3,308
  • 35
  • 68
Kannaiyan
  • 12,554
  • 3
  • 44
  • 83