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) def send_contact_notification_to_admin(admin_email: str, customer_name: str, customer_email: str, subject: str, message: str, phone: str = None) -> bool: """ Send notification to admin when a customer submits a contact message. Args: admin_email: Admin's email address customer_name: Customer's full name customer_email: Customer's email subject: Message subject message: Message content phone: Customer's phone number (optional) Returns: bool: True if email sent successfully """ email_subject = f"New Contact Message from {customer_name}" # Plain text version body = f""" Hello Admin, You have received a new contact message from a customer. Customer Details: - Name: {customer_name} - Email: {customer_email} - Phone: {phone or 'Not provided'} Subject: {subject} Message: {message} Please log in to the admin dashboard to view and respond to this message. https://brand-master.dvirlabs.com/admin Best regards, Brand Master System """ # HTML version html_body = f"""

📧 New Contact Message

Hello Admin,

You have received a new contact message from a customer.

Customer Details

Name: {customer_name}

Email: {customer_email}

Phone: {phone or 'Not provided'}

Subject: {subject}

Message:

{message.replace(chr(10), '
')}

View in Admin Dashboard
""" return send_email(admin_email, email_subject, body, html_body) def send_admin_response_to_customer(customer_email: str, customer_name: str, original_subject: str, admin_notes: str) -> bool: """ Send admin's response to customer's contact message. Args: customer_email: Customer's email address customer_name: Customer's full name original_subject: Original message subject admin_notes: Admin's response/notes Returns: bool: True if email sent successfully """ email_subject = f"Re: {original_subject}" # Plain text version body = f""" Hello {customer_name}, Thank you for contacting Brand Master. We have reviewed your message and here is our response: {admin_notes} If you have any additional questions, please don't hesitate to contact us again or visit our website. Best regards, Brand Master Team https://brand-master.dvirlabs.com """ # HTML version html_body = f"""

Response to Your Message

Re: {original_subject}

Hello {customer_name},

Thank you for contacting Brand Master. We have reviewed your message and here is our response:

Our Response:

{admin_notes.replace(chr(10), '
')}

If you have any additional questions, please don't hesitate to contact us again.

Contact Us Again
""" return send_email(customer_email, email_subject, body, html_body)