-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmailClass.py
82 lines (75 loc) · 2.59 KB
/
EmailClass.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
import datetime
import os
import dotenv
import boto3
from botocore.exceptions import ClientError
dotenv.load_dotenv()
class email_sender:
def __init__(self):
self.date = datetime.datetime.now()
self.sender = os.getenv("AUTOMACAO_EMAIL")
self.receiver = os.getenv("MAIN_EMAIL")
self.subject = "automação da publicação no Anchor"
self.message = f"Olá! o episódio do dia { self.date.now().day } foi publicado com sucesso."
def send_sucesso(self):
data = {
"from": {"email": self.sender},
"to": [{"email": self.receiver}],
"subject": self.subject,
"html": f"<strong>{self.message}</strong>",
"category": "automação",
}
self.send_api_AWS_SES(data)
def send_falha(self):
self.receiver = (
[
os.getenv("EMAIL"),
os.getenv("SECUNDARY_EMAIL"),
]
if os.getenv("SECUNDARY_EMAIL") is not None
else [{"email": os.getenv("EMAIL")}]
)
self.replace_mensage("foi publicado com sucesso", "não foi publicado")
data = {
"from": self.sender,
"to": self.receiver,
"subject": self.subject,
"html": f"<strong>{self.message}</strong>",
"category": "automação",
}
self.send_api_AWS_SES(data)
def replace_mensage(self, oldStr, newStr):
if self.message is not None:
self.message = self.message.replace(oldStr, newStr)
def send_api_AWS_SES(self, data):
res = boto3.client(
"ses",
region_name="us-east-1",
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
)
try:
res.send_email(
Destination={
"ToAddresses": data["to"],
},
Message={
"Body": {
"Html": {
"Charset": "UTF-8",
"Data": data["html"],
},
"Text": {
"Charset": "UTF-8",
"Data": data["html"],
},
},
"Subject": {
"Charset": "UTF-8",
"Data": data["subject"],
},
},
Source=data["from"],
)
except ClientError as e:
print(e.response["Error"]["Message"])