-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemailapi.py
More file actions
38 lines (28 loc) · 1.08 KB
/
emailapi.py
File metadata and controls
38 lines (28 loc) · 1.08 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
import smtplib
from email.message import EmailMessage
import os
class EmailAPI:
def __init__(self):
path = None
def send_email_with_attachment(self, file_path:str):
sender_email = os.getenv("EMAIL_SENDER")
receiver = os.getenv("EMAIL_RECEIVER")
password = os.getenv("EMAIL_PASS")
message_object = EmailMessage()
message_object['Subject'] = 'Update on Registration'
message_object['To'] = receiver
message_object['From'] = sender_email
message_object.set_content('Here is a new upload in registration attached ')
with open(file_path,'rb') as file:
file_data = file.read()
file_name = file.name
message_object.add_attachment(
file_data,
maintype = 'application',
subtype = 'vnd.openxmlformats-officedocument.spreadsheetml.sheet',
filename = file_name
)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(sender_email, password)
server.send_message(message_object)