File size: 968 Bytes
08b74f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Email utilities
import smtplib
import logging
from email.mime.text import MIMEText
import random
import string

from utils.config import Config

def generate_otp(length=6):
    return ''.join(random.choices(string.digits, k=length))

def generate_random_password(length=12):
    chars = string.ascii_letters + string.digits
    return ''.join(random.choices(chars, k=length))

def send_email(to_email, subject, body):
    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['From'] = Config.EMAIL_ADDRESS
    msg['To'] = to_email
    try:
        with smtplib.SMTP(Config.SMTP_SERVER, Config.SMTP_PORT) as server:
            server.starttls()
            server.login(Config.EMAIL_ADDRESS, Config.EMAIL_PASSWORD)
            server.send_message(msg)
        logging.info(f"Email đã gửi tới {to_email}")
        return True
    except Exception as e:
        logging.error(f"Lỗi khi gửi email: {e}")
        return False