-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshop_lambda_function.py
102 lines (90 loc) · 2.97 KB
/
shop_lambda_function.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from __future__ import print_function
import time
import boto3
from botocore.exceptions import ClientError
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
# Function to send email
def send_email(message):
SENDER = "[email protected]" # Must be verified in AWS SES Email
RECIPIENT = "[email protected]" # Must be verified in AWS SES Email
# Replace with the AWS Region you're using for Amazon SES.
AWS_REGION = "us-west-1"
# The subject line for the email.
SUBJECT = "This order is destined for shop1!!"
# The email body for recipients with non-HTML email clients.
BODY_TEXT = f"""Hey Hi...\r\n
Here is an order:\n {message} \n
Thank you!"""
# The HTML body of the email.
BODY_HTML = f"""
<html>
<head></head>
<body>
<h1>Hey Hi,</h1>
<p>Here is an order:</p>
<p><strong>{message}</strong></p>
<p>Thank you!</p>
</body>
</html>
"""
# The character encoding for the email.
CHARSET = "UTF-8"
# Create a new SES resource and specify a region.
client = boto3.client('ses', region_name=AWS_REGION)
# Check if the configuration set is set or create it
try:
response = client.create_configuration_set(
ConfigurationSet={
'Name': 'my-config-set'
}
)
except ClientError as e:
if e.response['Error']['Code'] == 'ConfigurationSetAlreadyExists':
print("Configuration set already exists.")
else:
print(f"Error creating configuration set: {e.response['Error']['Message']}")
return
except Exception as e:
print(f"Unexpected error: {str(e)}")
return
# Try to send the email.
try:
# Provide the contents of the email.
response = client.send_email(
Destination={
'ToAddresses': [
RECIPIENT,
],
},
Message={
'Body': {
'Html': {
'Charset': CHARSET,
'Data': BODY_HTML
},
'Text': {
'Charset': CHARSET,
'Data': BODY_TEXT
},
},
'Subject': {
'Charset': CHARSET,
'Data': SUBJECT
},
},
Source=SENDER,
ConfigurationSetName='my-config-set',
)
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:", response['MessageId'])
def lambda_handler(event, context):
for record in event['Records']:
payload = record["body"]
print("Payload:", payload)
send_email(payload)
print("Shop1 processing complete")
time.sleep(5)