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

Sending Lambda generated analytics reports through email

0

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("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

2 Answers
1

Hello

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: https://docs.aws.amazon.com/AmazonS3/latest/userguide/NotificationHowTo.html

https://docs.aws.amazon.com/AmazonS3/latest/userguide/ShareObjectPreSignedURL.html

https://docs.aws.amazon.com/ses/latest/dg/send-email-raw.html

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 = 'abc@xyz.com', Destination = { 'ToAddresses': [ 'recipient@gmail.com' ] }, 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

1

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 https://aws.amazon.com/premiumsupport/knowledge-center/ec2-port-25-throttle/ . 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 https://aws.amazon.com/premiumsupport/knowledge-center/lambda-send-email-ses/

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