-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_email.py
More file actions
176 lines (144 loc) · 5.65 KB
/
send_email.py
File metadata and controls
176 lines (144 loc) · 5.65 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
Creates an SMTP connection and sends an email.
"""
import os
import ast
import logging
from email.header import Header
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import smtplib
from smtplib import (
SMTPNotSupportedError,
SMTPAuthenticationError,
SMTPServerDisconnected,
SMTPSenderRefused,
SMTPConnectError,
)
from validate_email import validate_email
logger = logging.getLogger(__name__)
DEFAULT_EMAIL_CREDENTIALS = ast.literal_eval(os.getenv("DEFAULT_EMAIL_CREDENTIALS"))
class Email:
def __init__(self, credentials=DEFAULT_EMAIL_CREDENTIALS):
self.credentials = credentials
self.smtp_conn = self.connect()
self.msg = MIMEMultipart()
@property
def credentials(self):
return self._credentials
@credentials.setter
def credentials(self, codes):
if not codes:
raise ValueError("'Credentials' cannot be empty")
self._credentials = codes
def connect(self):
"""
Establishes an SMTP connection with the email server.
"""
con = None
use_tls = self.credentials.get("use_tls", True)
try:
con_method = smtplib.SMTP if use_tls else smtplib.SMTP_SSL
con = con_method(
self.credentials.get("host"),
self.credentials.get("port"),
)
con.login(self.credentials.get("user"), self.credentials.get("password"))
except (
SMTPNotSupportedError,
SMTPAuthenticationError,
SMTPServerDisconnected,
SMTPConnectError,
) as ex:
logger.exception("<%s>: %s", type(ex).__name__, ex)
return con
@staticmethod
def parse_attachments(attachments):
"""
Parse and prepare attachments for the email message.
:param attachments: List of file paths for attachments.
:return: List of MIMEBase attachment parts.
"""
if not isinstance(attachments, (list, tuple)):
raise ValueError("Attachments are provided in an invalid format.")
parts = []
for path in attachments:
part = MIMEBase("application", "octet-stream")
with open(path, "rb") as file:
part.set_payload(file.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition", f'attachment; filename="{os.path.basename(path)}"'
)
parts.append(part)
return parts
def init_msg(self, subject, body, attachments=None):
"""
Initialize the email message.
:param subject: The subject of the email.
:param body: The body/content of the email.
:param attachments: List of file paths for attachments (optional).
:return: None
"""
if not attachments:
attachments = []
self.msg["Subject"] = subject
self.msg.attach(MIMEText(body, "plain"))
attachment_parts = Email.parse_attachments(attachments)
for part in attachment_parts:
self.msg.attach(part)
def add_recipients(self, from_name, to_address, cc_address, bcc_address):
"""
Add recipients to the email message.
:param from_name: The name of the sender.
:param to_address: List of email addresses for the primary recipients.
:param cc_address: List of email addresses for the carbon copy recipients.
:param bcc_address: List of email addresses for the blind carbon copy recipients.
"""
def validate(emails_):
if emails_ is None:
return True
return isinstance(emails_, (list, tuple, set)) and all(
validate_email(k) for k in emails_
)
if not validate(to_address) or not validate(cc_address) or not validate(bcc_address):
raise ValueError("Email address is not in required format and/or is invalid.")
self.msg["From"] = (
formataddr((str(Header(from_name, "utf-8")), self.credentials.get("user")))
if from_name
else self.credentials.get("user")
)
self.msg["To"] = ", ".join(to_address) if to_address else None
self.msg["Cc"] = ", ".join(cc_address) if cc_address else None
self.msg["Bcc"] = ", ".join(bcc_address) if bcc_address else None
def send(
self,
to_address=None,
from_name=None,
cc_address=None,
bcc_address=None,
subject="",
body="",
attachments=None,
):
"""
Send an email.
:param to_address: List of email addresses for the primary recipients.
:param from_name: The name of the sender.
:param cc_address: List of email addresses for the carbon copy recipients.
:param bcc_address: List of email addresses for the blind carbon copy recipients.
:param subject: The subject of the email.
:param body: The body/content of the email.
:param attachments: List of file paths for attachments (optional).
"""
self.init_msg(subject, body, attachments)
self.add_recipients(from_name, to_address, cc_address, bcc_address)
try:
self.smtp_conn.send_message(self.msg)
except SMTPSenderRefused as ex:
logger.exception("<%s>: %s", type(ex).__name__, ex)
finally:
self.smtp_conn.quit()