-
Notifications
You must be signed in to change notification settings - Fork 0
/
email_sender.py
48 lines (40 loc) · 1.68 KB
/
email_sender.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
import smtplib
from linker import Linker
from email.mime.multipart import MIMEMultipart
from constants import HOST, PORT, SENDER_EMAIL, PASSWORD, HTML_CONTENT_PATH, IMAGES_PATH, SUBJECT, EMAIL
class Email_Sender:
def __init__(self, configuration: dict):
self.host = configuration[HOST]
self.port = int(configuration[PORT])
self.sender_email = configuration[SENDER_EMAIL]
self.password = configuration[PASSWORD]
self.html_content_path = configuration[HTML_CONTENT_PATH]
self.images_path = configuration[IMAGES_PATH]
self.subject = configuration[SUBJECT]
def compose_message(self, data: dict):
linker = Linker(self.images_path)
html_content = ''
with open(self.html_content_path) as html_content_file:
html_content = html_content_file.read()
html_content, used_images = linker.link_html(html_content, data)
message = MIMEMultipart('related')
message['Subject'] = self.subject
message['From'] = self.sender_email
message['To'] = data[EMAIL]
message.preamble = 'This is a multi-part message in MIME format.'
message = linker.attach_html_content(message, html_content)
message = linker.attach_images(message, used_images)
return message
def send_emails(self, data_reader):
smtp = smtplib.SMTP(self.host, self.port)
smtp.starttls()
smtp.login(self.sender_email, self.password)
for data in data_reader:
message = self.compose_message(data)
email = data[EMAIL]
self.send_email(email, message, smtp)
smtp.quit()
print('Emails sent.')
def send_email(self, email: str, message: MIMEMultipart(), smtp: smtplib.SMTP()):
smtp.sendmail(self.sender_email, email, message.as_string())
print('Email sent to :', email)