""" Email Service for PromptTech Solutions Handles email verification, notifications, and password resets """ import smtplib import os from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage from pathlib import Path import logging logger = logging.getLogger(__name__) # Email configuration from environment SMTP_HOST = os.environ.get('SMTP_HOST', 'smtp.gmail.com') SMTP_PORT = int(os.environ.get('SMTP_PORT', 587)) SMTP_USER = os.environ.get('SMTP_USER', '') SMTP_PASSWORD = os.environ.get('SMTP_PASSWORD', '') FROM_EMAIL = os.environ.get('FROM_EMAIL', SMTP_USER) FRONTEND_URL = os.environ.get('FRONTEND_URL', 'http://localhost:5300') def send_email(to_email: str, subject: str, html_content: str, text_content: str = None): """ Send an email using SMTP Args: to_email: Recipient email address subject: Email subject html_content: HTML content of the email text_content: Plain text fallback (optional) """ if not SMTP_USER or not SMTP_PASSWORD: logger.warning("SMTP credentials not configured. Email not sent.") return False try: # Create message message = MIMEMultipart('alternative') message['Subject'] = subject message['From'] = f"PromptTech Solutions <{FROM_EMAIL}>" message['To'] = to_email # Add text/plain part (fallback) if text_content: text_part = MIMEText(text_content, 'plain') message.attach(text_part) # Add text/html part html_part = MIMEText(html_content, 'html') message.attach(html_part) # Send email with smtplib.SMTP(SMTP_HOST, SMTP_PORT) as server: server.starttls() server.login(SMTP_USER, SMTP_PASSWORD) server.send_message(message) logger.info(f"Email sent successfully to {to_email}") return True except Exception as e: logger.error(f"Failed to send email to {to_email}: {e}") return False def send_verification_email(to_email: str, first_name: str, verification_token: str): """Send email verification link to new user""" verification_link = f"{FRONTEND_URL}/verify-email?token={verification_token}" subject = "Verify your PromptTech Solutions account" html_content = f"""

Welcome to PromptTech Solutions!

Hi {first_name},

Thank you for creating an account with PromptTech Solutions. To complete your registration and verify your email address, please click the button below:

Verify Email Address

Or copy and paste this link into your browser:

{verification_link}

This link will expire in 24 hours.

If you didn't create this account, you can safely ignore this email.

Best regards,
The PromptTech Solutions Team

""" text_content = f""" Hi {first_name}, Thank you for creating an account with PromptTech Solutions. To complete your registration and verify your email address, please visit: {verification_link} This link will expire in 24 hours. If you didn't create this account, you can safely ignore this email. Best regards, The PromptTech Solutions Team """ return send_email(to_email, subject, html_content, text_content) def send_welcome_email(to_email: str, first_name: str): """Send welcome email after successful verification""" subject = "Welcome to PromptTech Solutions!" html_content = f"""

🎉 Account Verified!

Hi {first_name},

Your email has been successfully verified! You now have full access to your PromptTech Solutions account.

What's Next?

Shop Now View Services

Need help? Contact us at prompttechbz@gmail.com or call (501) 638-6318

Best regards,
The PromptTech Solutions Team

""" return send_email(to_email, subject, html_content) def send_password_reset_email(to_email: str, first_name: str, reset_token: str): """Send password reset link""" reset_link = f"{FRONTEND_URL}/reset-password?token={reset_token}" subject = "Reset your PromptTech Solutions password" html_content = f"""

🔒 Password Reset Request

Hi {first_name},

We received a request to reset your password. Click the button below to create a new password:

Reset Password

Or copy and paste this link into your browser:

{reset_link}

This link will expire in 1 hour.

If you didn't request a password reset, please ignore this email and your password will remain unchanged.

Best regards,
The PromptTech Solutions Team

""" return send_email(to_email, subject, html_content)