· 7 years ago · Jul 30, 2018, 03:14 PM
1import json
2import boto3
3from boto3.session import Session
4from botocore.exceptions import ClientError
5def lambda_handler(event, context):
6 SENDER = "Jordy Yaroslab <jordy@yaroslab.com>"
7 RECIPIENT ="jordy.elosojudio@gmail.com"
8 RECIPIENT2="jordy@yaroslab.com"
9 # ConfigurationSetName=CONFIGURATION_SET argument below.
10 #CONFIGURATION_SET = "ConfigSet"
11 SUBJECT = "Reportes de Backup ERP"
12 # BODY_TEXT = ("Amazon SES Test (Python)\r\n"
13 # "This email was sent with Amazon SES using the "
14 # "AWS SDK for Python (Boto)."
15 # )
16 BODY_HTML = """<html>
17 <head></head>
18 <body>
19 <h3>BACKUPS FROM : PE_CC_VOICE </h3>
20 <p>This email was sent with
21 <a href='http://www.yaroslab.com/'> Consultoria Yaroslab S.A.C </a> </p>
22 </body>
23 </html>
24 """
25 CHARSET = "UTF-8"
26
27 lam=boto3.client("lambda")
28 aws_region = 'us-east-1'
29 access_key = 'AKIAJ3GDNC5G2TN5SRZQ'
30 secret_key = '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'
31 session = Session(aws_access_key_id=access_key,aws_secret_access_key=secret_key)
32 s3recurso = session.resource('s3')
33 your_bucket = s3recurso.Bucket('backupsyaros')
34 folder="cimacom/pe_cc_voice/"
35 payload = []
36 for s3_file in your_bucket.objects.filter(Prefix=folder):
37 mivar=str(s3_file.key)+" || "+str(s3_file.size)+" || "+str(s3_file.last_modified)+"<br>"
38# print mivar
39 payload.append(mivar)
40# Payload2=json.dumps(payload)
41# print Payload2
42
43 # Create a new SES resource and specify a region.
44 client = boto3.client('ses',region_name=aws_region)
45 # Try to send the email.
46 try:
47 #Provide the contents of the email.
48 response = client.send_email(
49 Destination={
50 'ToAddresses': [
51 RECIPIENT,RECIPIENT2
52 ],
53 },
54 Message={
55 'Body': {
56 'Html': {
57 'Charset': CHARSET,
58 # 'Data': BODY_HTML,
59 'Data': BODY_HTML+"<br/>"+str(payload)
60 }
61 # 'Text': {
62 # 'Charset': CHARSET,
63 # 'Data': BODY_TEXT,
64 # },
65 },
66 'Subject': {
67 'Charset': CHARSET,
68 'Data': SUBJECT,
69 },
70 },
71 Source=SENDER,
72 # If you are not using a configuration set, comment or delete the
73 # following line
74 #ConfigurationSetName=CONFIGURATION_SET,
75 )
76 # Display an error if something goes wrong.
77 except ClientError as e:
78 print(e.response['Error']['Message'])
79 else:
80 print("Email sent! Message ID:"),
81 print(response['MessageId'])
82
83# response=lam.invoke(FunctionName="send_mail_ls_bucketcimacom_pe_cc_voice",InvocationType="Event",Payload=json.dumps(payload))
84milambda=lambda_handler({"key3": "value3"},{"key2": "value2"})