Sending AWS Lambda generated reports through email

Viewed 23

I have a Lambda function which generates a csv and stores it in S3 each day. I want to send this across to some recipients on daily basis. The below Python code works to send a file on my local machine but I am facing challenges in getting reference to the generated file in S3 in my send mail method. Request for any pointers on this.

Also, would be great if someone can guide if there any particular challenges / limits with this approach compared to using Amazon SES. I do not want to send bulk mails and would have daily deliveries to about 10-15 recipients.

def generateFile&Send():
  ...
  s3 = boto3.client('s3')
  fileName = 's3://mybukcet/myfile' + mydatestr + '.csv'
  wr.s3.to_csv(final_all_data, fileName, index=False)
  
   send_mail("mymail@gmail.com", "recipient@gmail.com", emailSubject , emailText, filename , 
  "smtpddomain.com", "587", email="mymail.com", password="mypassword", isTls=True)
}

def send_mail(send_from,send_to,subject,text,file,server,port,email='',password='',isTls=True):
    msg = MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = send_to
    msg['Date'] = formatdate(localtime = True)
    msg['Subject'] = subject
    msg.attach(MIMEText(text))

    part = MIMEBase('application', "octet-stream")
    part.set_payload(open(file, "rb").read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename=file')
    msg.attach(part)

    smtp = smtplib.SMTP(server, port)
    if isTls:
        smtp.starttls()
    smtp.login(email,password)
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.quit()

Regards, dbeings

0 Answers
Related