-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_sender.py
34 lines (28 loc) · 1.26 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
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from email.mime.text import MIMEText
class EmailSender:
def __init__(self, sender_email, sender_password):
self.sender_email = sender_email
self.sender_password = sender_password
async def send_email(self, recipient_email, subject, message, attachments=None):
msg = MIMEMultipart()
msg['From'] = self.sender_email
msg['To'] = recipient_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
if attachments:
for attachment_path in attachments:
attachment = open(attachment_path, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', f"attachment; filename= {attachment_path}")
msg.attach(part)
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls()
server.login(self.sender_email, self.sender_password)
text = msg.as_string()
server.sendmail(self.sender_email, recipient_email, text)