generated from oceanhackweek/ohwyy_proj_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AWS-SES-SendEmail.py
66 lines (57 loc) · 2.13 KB
/
AWS-SES-SendEmail.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
import boto3
from botocore.exceptions import ClientError
def create_email_content(new_data_available, record_broken, temperature_value, summary, location, depth):
subject = location + ": New Data Update and Record Broken!"
body = (
f"Hello,\n\n"
f"You told us to message you when it is hot. And it is!\n"
f"We added new data to our website and it tells us a new record has been broken for {location} at {depth}m!\n"
f"Temperature Anomaly Record: {temperature_value}°C\n\n"
f"Please read the summary below: {summary}. (In the future, we can add some images to it and increment the wording.)"
f"Best regards,\nYour Message Me When It's Hot Team"
)
return subject, body
temperature_value = "5"
location='Maria Island'
depth = '21'
summary = "A new heatwave has led to unprecedented temperatures."
subject, body = create_email_content(True, True, temperature_value, summary, location, depth)
def send_email():
# Create an SES client
ses_client = boto3.client('ses', region_name='ap-southeast-2') # Change the region if needed
# Email settings
SENDER = "[email protected]"
RECIPIENT = "[email protected]"
SUBJECT = subject
BODY_TEXT = body
CHARSET = "UTF-8"
# Try to send the email.
try:
response = ses_client.send_email(
Destination={
'ToAddresses': [
RECIPIENT,
],
},
Message={
'Body': {
'Text': {
'Charset': CHARSET,
'Data': BODY_TEXT,
},
},
'Subject': {
'Charset': CHARSET,
'Data': SUBJECT,
},
},
Source=SENDER,
)
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:"),
print(response['MessageId'])
# Call the function to send an email
send_email()