66 lines
1.8 KiB
Python
66 lines
1.8 KiB
Python
import smtplib
|
|
from smtplib import *
|
|
from email.message import EmailMessage
|
|
from random import randrange
|
|
import sqlite3
|
|
from datetime import datetime, timedelta
|
|
from verification import otp
|
|
|
|
class mail:
|
|
|
|
def __init__(self, email):
|
|
self.email = email
|
|
|
|
def add_db(email, code):
|
|
conn = sqlite3.connect('otp.db')
|
|
cursor = conn.cursor()
|
|
|
|
insert_email = "INSERT OR IGNORE INTO sessions (email) VALUES (?)"
|
|
insert_code = "UPDATE sessions SET code = ? WHERE email = ?"
|
|
insert_datedel = "UPDATE sessions SET datedel = ? WHERE email = ?"
|
|
|
|
cursor.execute(insert_email, (email, ))
|
|
cursor.execute(insert_code, (code, email, ))
|
|
cursor.execute(insert_datedel, (datetime.now() + timedelta(minutes=5), email, ))
|
|
|
|
|
|
conn.commit()
|
|
conn.close()
|
|
return True
|
|
|
|
def gen_code(email):
|
|
if (otp.check_code(email)):
|
|
code = randrange(1000, 9999)
|
|
mail.add_db(email, code)
|
|
return code
|
|
else:
|
|
print("Code already in progress!")
|
|
|
|
def send(email, code):
|
|
try:
|
|
smtpObj = smtplib.SMTP_SSL('mail.__server__.com', 465)
|
|
|
|
# Identify yourself to an ESMTP server using EHLO
|
|
smtpObj.ehlo()
|
|
|
|
# Login to the server (if required)
|
|
smtpObj.login('username', 'passwd')
|
|
|
|
# Send an email
|
|
msg = EmailMessage()
|
|
msg.set_content(f'You have requested a verification code. Your code is {code}. This code expires in 5 minutes.')
|
|
|
|
msg['Subject'] = 'UWP Community Verification Code'
|
|
msg['From'] = ""
|
|
msg['To'] = email
|
|
|
|
smtpObj.send_message(msg)
|
|
|
|
# Quit the SMTP session
|
|
smtpObj.quit()
|
|
except SMTPResponseException as e:
|
|
print(e)
|
|
|
|
|
|
|