I have a Lambda function which returns email addresses into the function log of lambda and i have another lambda function that sends scheduled emails.
I am trying to pass the result from the email addresses function into the second scheduled email function to be used as a variable for the recipients of the scheduled emails.
Here is the code for anyone wondering:
This Function retrieves the email/'s from the database
import pymysql
# RDS config
endpoint = '*******'
username = '*******'
password = '*******'
database_name = '******'
#connection config
connection = pymysql.connect(host=endpoint,user=username,passwd=password,db=database_name)
def handler(event, context):
cursor = connection.cursor()
cursor.execute('SELECT `Presenters`.Email FROM `Main` INNER JOIN `Presenters` ON `Main`.`PresenterID` = `Presenters`.`PresentersID` WHERE `Main`.`Read Day` ="Tuesday"')
rows = cursor.fetchall()
for row in rows:
print("{0}".format(row[0]))
This Second Function sends emails using Python
import os
import smtplib
from email.message import EmailMessage
def lambda_handler(event, context):
EMAIL_ADDRESS = "**********"
EMAIL_PASSWORD = os.environ.get('EMAIL_PASSWORD')
msg = EmailMessage()
msg['Subject'] = "*********"
msg['From'] = EMAIL_ADDRESS
msg['To'] = ['************']
msg.set_content('Hi everyone, a new read timetable has been posted for next week so be sure to check it and keep up to date on your reads, Thank you!')
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)