By using AWS re:Post, you agree to the Terms of Use

Sending Lambda generated analytics reports through email


Hi, 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("", "", emailSubject , emailText, filename , 
  "", "587", email="", 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

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

    smtp = smtplib.SMTP(server, port)
    if isTls:
    smtp.sendmail(send_from, send_to, msg.as_string())

Regards, dbeings

2 Answers


Your design must be as Lambda => s3=> lambda => ses

BTW, you must consider using s3 pre signed url to allow the email destination access that covers file

Some references:

Wish that helps

answered 19 days ago
  • Hi Omid & Nitin,

    Thanks for your response. I have completed DKIM settings and now able to send email from Lambda. But just wanted some guidance on sending attachments from S3:

    • I am using ses.sendmail(...) to send mail without attachments. Is there a way to modify this (code below) to send mail with attachments or do I need to use ses.send_raw_email for attachments.
    • I am generating s3 file in lambda and then plan is to send it using SES. I have the S3 file and bucket name as part of the file generation code but do not have the ARN for generated S3 file. What reference would be required to mail S3 file from SES (ARN vs bucket/filename),
    • What is the limit to attachment size that I can send. Is there a limit per mail / cumulative size of mails sent from SES.

    def send_mail(subject,file): ses = boto3.client('ses') body = "<Mybody>" ses.send_email( Source = '', Destination = { 'ToAddresses': [ '' ] }, Message = { 'Subject': { 'Data': subject, 'Charset': 'UTF-8' }, 'Body': { 'Text':{ 'Data': body, 'Charset': 'UTF-8' } } } )

    return {
        'statusCode': 200,
        'body': json.dumps('Mail sent successfully')

    Regards, dbeings


From the lambda it may have not been allowed or blocked as an anti-spam measure, please check. You see a similar use case defined here . May be look into AWS Lambda to send email using Amazon Simple Email Service (Amazon SES), reference on how to configure can be found here

profile picture
answered 20 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions