-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmail.py
65 lines (46 loc) · 1.46 KB
/
mail.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import sys
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib
import json
# Read and eval config file
with open("config.json", "r") as f:
config = json.loads(f.read())
try:
int(config["SMTP_PORT"])
except ValueError:
print("SMTP port must be integer")
def send_email(to_email, subject, message):
try:
server = smtplib.SMTP(config["SMTP_HOST"], config["SMTP_PORT"])
server.starttls()
server.login(config["SMTP_USER"], config["SMTP_PASS"])
msg = MIMEMultipart()
msg['From'] = config["SMTP_USER"]
msg['To'] = to_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
server.sendmail(config["SMTP_USER"], to_email, msg.as_string())
server.quit()
except Exception as e:
print("Error sending email: ", e, file=sys.stderr)
return "Failed to send email"
return None
def confirm_email(to_email, link):
subject = "Email Confirmation"
message = f"""Dear User,
Thank you for creating an account with us. Please click on the link below to confirm your email address:
{link}
Best regards,
The StudSec Team
"""
return send_email(to_email, subject, message)
def forgot_password(to_email, link):
subject = "Password Reset"
message = f"""Dear User,
Please click on the link below to reset your password:
{link}
Best regards,
The StudSec Team
"""
return send_email(to_email, subject, message)