-
Notifications
You must be signed in to change notification settings - Fork 3
/
email_helper.py
99 lines (88 loc) · 2.98 KB
/
email_helper.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
import boto3
from botocore.exceptions import ClientError
def send_email(sndr=None, rcpt=None, subj=None, b_text='', b_html=None, body_header=''):
# Replace [email protected] with your "From" address.
# This address must be verified with Amazon SES.
if sndr:
SENDER = sndr
else:
SENDER = "[email protected]"
# Replace [email protected] with a "To" address. If your account
# is still in the sandbox, this address must be verified.
if rcpt:
RECIPIENT = rcpt
else:
RECIPIENT = "[email protected]"
# Specify a configuration set. If you do not want to use a configuration
# set, comment the following variable, and the
# ConfigurationSetName=CONFIGURATION_SET argument below.
# CONFIGURATION_SET = "ConfigSet"
# If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES.
AWS_REGION = "us-east-2"
# The subject line for the email.
if subj:
SUBJECT = subj
else:
SUBJECT = "Amazon SES Test (SDK for Python)"
# The email body for recipients with non-HTML email clients.
if b_text:
BODY_TEXT = b_text
else:
BODY_TEXT = ("Amazon SES Test (Python)\r\n"
"This email was sent with Amazon SES using the "
"AWS SDK for Python (Boto)."
)
# The HTML body of the email.
if b_html:
BODY_HTML = b_html
else:
BODY_HTML = f"""<html>
<head></head>
<body>
<h1>{body_header}</h1>
<p>{b_text}</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)
# 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,
# If you are not using a configuration set, comment or delete the
# following line
# ConfigurationSetName=CONFIGURATION_SET,
)
# Display an error if something goes wrong.
except ClientError as e:
print(e.response['Error']['Message'])
return['Error sending email']
else:
print("Email sent! Message ID:"),
print(response['MessageId'])
return response['MessageId']