-
Notifications
You must be signed in to change notification settings - Fork 0
/
Codes.txt
75 lines (60 loc) · 2.29 KB
/
Codes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Lambda Function Python Code
import boto3
import csv
# Initialize the boto3 client
s3_client = boto3.client('s3')
ses_client = boto3.client('ses')
def lambda_handler(event, context):
# Specify the S3 bucket name
bucket_name = 'bucket-email-marketing'
try:
# Retrieve the CSV file from S3
csv_file = s3_client.get_object(Bucket=bucket_name, Key='contacts.csv')
lines = csv_file['Body'].read().decode('utf-8').splitlines()
# Retrieve the HTML email template from S3
email_template = s3_client.get_object(Bucket=bucket_name, Key='email_template.html')
email_html = email_template['Body'].read().decode('utf-8')
# Parse the CSV file
contacts = csv.DictReader(lines)
for contact in contacts:
# Replace placeholders in the email template with contact information
personalized_email = email_html.replace('{{FirstName}}', contact['FirstName'])
# Send the email using SES
response = ses_client.send_email(
Source='[email protected]', # Replace with your source email address
Destination={'ToAddresses': [contact['Email']]},
Message={
'Subject': {'Data': 'AWS News!', 'Charset': 'UTF-8'},
'Body': {'Html': {'Data': personalized_email, 'Charset': 'UTF-8'}}
}
)
print(f"Email sent to {contact['Email']}: Response {response}")
except Exception as e:
print(f"An error occurred: {e}")
# Lambda Function Test Event
{
"comment": "Generic test event for scheduled Lambda execution. The function does not use this event data.",
"test": true
}
# IAM Policy for SES and S3 permissions
# Update the ARN to use your S3 bucket
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject"
],
"Resource": "arn:aws:s3:::bucket-email-marketing/*"
},
{
"Effect": "Allow",
"Action": [
"ses:SendEmail",
"ses:SendRawEmail"
],
"Resource": "*"
}
]
}