forked from Explore-AI/cloud-computing-predict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_emails_with_ses.py
111 lines (88 loc) · 3.4 KB
/
send_emails_with_ses.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
103
104
105
106
107
108
109
110
111
"""
Lambda Function used to send emails via Amazon SES.
Author: Explore Data Science Academy.
Note:
---------------------------------------------------------------------
The contents of this file should be added to a AWS Lambda function
created as part of the EDSA Cloud-Computing Predict.
For further guidance around this process, see the README instruction
file which sits at the root of this repo.
---------------------------------------------------------------------
"""
# Lambda dependencies
import boto3 # Python AWS SDK
import json # Used for handling API-based data.
import base64 # Needed to decode the incoming POST data
from botocore.exceptions import ClientError # Catch errors on client side
def lambda_handler(event, context):
# Perform JSON data decoding
body_enc = event['body']
dec_dict = json.loads(base64.b64decode(body_enc))
# Sample text that you would like to email to your recipient
# address from your sender address.
email_text = 'Hi, My Name is Rob'
# ** SES Functionality **
# Replace [email protected] with your "From" address.
# This address must be verified with Amazon SES.
# --- Insert your code here ---
SENDER = '[email protected]'
# -----------------------------
# Replace [email protected] with a "To" address. If your account
# is still in the sandbox, this address must be verified.
# --- Insert your code here ---
RECIPIENT = '[email protected]'
# -----------------------------
# The subject line for the email.
# --- DO NOT MODIFY THIS CODE ---
SUBJECT = f"Data Science Portfolio Project Website - Hello {dec_dict['name']}"
# -------------------------------
# The email body for recipients with non-HTML email clients
BODY_TEXT = (email_text)
# The character encoding for the email.
CHARSET = "UTF-8"
# Create a new SES service resource
client = boto3.client('ses')
# Try to send the email.
try:
#Provide the contents of the email.
ses_response = client.send_email(
Destination={
'ToAddresses': [
RECIPIENT,
# '[email protected]', # <--- Uncomment this line once you have successfully tested your predict end-to-end
],
},
Message={
'Body': {
'Text': {
'Charset': CHARSET,
'Data': BODY_TEXT,
},
},
'Subject': {
'Charset': CHARSET,
'Data': SUBJECT,
},
},
Source=SENDER,
)
# Display an error if something goes wrong.
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:"),
print(ses_response['MessageId'])
# ** Create a response object to inform the website
# that the workflow executed successfully. **
lambda_response = {
'statusCode': 200,
'body': json.dumps({
'Name': dec_dict['name'],
'Email': dec_dict['email'],
'Cell': dec_dict['phone'],
'Message': dec_dict['message'],
'SES_response': ses_response,
'Email_message': email_text
})
}
return lambda_response