-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmail.py
More file actions
96 lines (80 loc) · 3.29 KB
/
Email.py
File metadata and controls
96 lines (80 loc) · 3.29 KB
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
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 21 16:18:12 2018
This is the email sending class, it is called whenever a data update is made or an error occurs
@author: carrie
"""
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import boto3
from botocore.exceptions import ClientError
class SendEmail:
def send_email( self, subject, body_text, body_html):
author = 'fill in'
recipient = 'fill in'
try:
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['From'] = author
msg['To'] = recipient
msg['Subject'] = subject
# Create the body of the message (a plain-text and an HTML version).
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(body_text, 'plain')
part2 = MIMEText(body_html, 'html')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)
# Send the message via local SMTP server.
mail = smtplib.SMTP("smtp.outlook.office365.com", 587, timeout=20)
mail.starttls()
recepients = [recipient]
mail.login(author, "")
mail.sendmail(author, recepients, msg.as_string())
mail.quit()
except Exception as e:
raise e
def send_email_linux(self, subject, body_text, body_html):
# This address must be verified with Amazon SES.
SENDER = "fill in"
RECIPIENT = "fill in"
AWS_REGION = "fill in"
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
)
# Display an error if something goes wrong.
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:"),
print(response['ResponseMetadata']['RequestId'])