import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from typing import Optional from app.config import settings def send_email( to_email: str, subject: str, body: str, html_body: Optional[str] = None ) -> bool: """ Send an email using SMTP configuration from settings. Args: to_email: Recipient email address subject: Email subject body: Plain text body html_body: Optional HTML body (if provided, will be sent as multipart) Returns: bool: True if email sent successfully, False otherwise """ try: # Create message message = MIMEMultipart('alternative') if html_body else MIMEText(body, 'plain') if html_body: message['Subject'] = subject message['From'] = settings.smtp_from message['To'] = to_email # Attach plain text and HTML versions part1 = MIMEText(body, 'plain') part2 = MIMEText(html_body, 'html') message.attach(part1) message.attach(part2) else: message['Subject'] = subject message['From'] = settings.smtp_from message['To'] = to_email # Check if SMTP is configured if not settings.smtp_username or not settings.smtp_password: print(f"⚠️ SMTP not configured. Email would have been sent to: {to_email}") print(f"Subject: {subject}") print(f"Body:\n{body}") return False # Connect to SMTP server with timeout with smtplib.SMTP(settings.smtp_host, settings.smtp_port, timeout=10) as server: server.starttls() server.login(settings.smtp_username, settings.smtp_password) server.send_message(message) print(f"✅ Email sent successfully to {to_email}") return True except Exception as e: print(f"❌ Failed to send email to {to_email}: {str(e)}") return False def send_password_reset_pin(email: str, pin: str, expires_minutes: int = 15) -> bool: """ Send password reset PIN email. Args: email: User's email address pin: 6-digit PIN code expires_minutes: Minutes until PIN expires Returns: bool: True if email sent successfully """ subject = "Brand Master - Password Reset PIN" # Plain text version body = f""" Hello, You requested a password reset for your Brand Master account. Your 6-digit PIN code is: {pin} This PIN will expire in {expires_minutes} minutes. To reset your password: 1. Go to the login page 2. Click "Forgot Password" 3. Enter this PIN when prompted 4. Create your new password If you didn't request this reset, please ignore this email. Best regards, Brand Master Team """ # HTML version html_body = f"""

Password Reset Request

Hello,

You requested a password reset for your Brand Master account.

Your 6-digit PIN code is:

{pin}

Expires in {expires_minutes} minutes

To reset your password:

  1. Go to the login page
  2. Click "Forgot Password"
  3. Enter this PIN when prompted
  4. Create your new password

⚠️ If you didn't request this reset, please ignore this email.

""" return send_email(email, subject, body, html_body) def send_welcome_email(email: str, full_name: str) -> bool: """ Send welcome email to new users. Args: email: User's email address full_name: User's full name Returns: bool: True if email sent successfully """ subject = "Welcome to Brand Master!" body = f""" Hello {full_name}, Welcome to Brand Master! Your account has been created successfully. You can now: - Browse our collection of premium shoes and apparel - Add items to your wishlist - Place orders with secure checkout - Track your order history Visit us at: https://brand-master.dvirlabs.com Best regards, Brand Master Team """ html_body = f"""

Welcome to Brand Master!

Your premium fashion destination

Hello {full_name},

Welcome aboard! Your account has been created successfully.

What you can do:

  • ✅ Browse our collection of premium shoes and apparel
  • ✅ Add items to your wishlist
  • ✅ Place orders with secure checkout
  • ✅ Track your order history
Start Shopping
""" return send_email(email, subject, body, html_body)