-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgmail_service.py
More file actions
40 lines (33 loc) · 1.2 KB
/
Copy pathgmail_service.py
File metadata and controls
40 lines (33 loc) · 1.2 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
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from dotenv import load_dotenv
import os
current_window = None
load_dotenv(override=True)
EMAIL_ADDRESS = os.getenv("ADMIN_EMAIL")
EMAIL_PASSWORD = os.getenv("EMAIL_PASSWORD")
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
def send_email(to_email, subject, message, attachment=None):
try:
msg = MIMEMultipart()
msg['From'] = EMAIL_ADDRESS
msg['To'] = to_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
if attachment:
with open(attachment, 'rb') as f:
part = MIMEApplication(f.read(), Name=os.path.basename(attachment))
part['Content-Disposition'] = f'attachment; filename="{os.path.basename(attachment)}"'
msg.attach(part)
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.ehlo()
server.starttls()
server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
server.sendmail(EMAIL_ADDRESS, to_email, msg.as_string())
server.quit()
return True
except Exception as e:
return str(e)