import os from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import aiosmtplib import logging logger = logging.getLogger(__name__) SMTP_HOST = os.environ.get('SMTP_HOST', 'smtp.gmail.com') SMTP_PORT = int(os.environ.get('SMTP_PORT', 587)) SMTP_USERNAME = os.environ.get('SMTP_USERNAME', '') SMTP_PASSWORD = os.environ.get('SMTP_PASSWORD', '') SMTP_FROM_EMAIL = os.environ.get('SMTP_FROM_EMAIL', 'noreply@membership.com') SMTP_FROM_NAME = os.environ.get('SMTP_FROM_NAME', 'Membership Platform') FRONTEND_URL = os.environ.get('FRONTEND_URL', 'http://localhost:3000') async def send_email(to_email: str, subject: str, html_content: str): """Send an email using SMTP""" try: message = MIMEMultipart('alternative') message['From'] = f"{SMTP_FROM_NAME} <{SMTP_FROM_EMAIL}>" message['To'] = to_email message['Subject'] = subject html_part = MIMEText(html_content, 'html') message.attach(html_part) # For development/testing, just log the email if not SMTP_USERNAME or not SMTP_PASSWORD: logger.info(f"[EMAIL] To: {to_email}") logger.info(f"[EMAIL] Subject: {subject}") logger.info(f"[EMAIL] Content: {html_content}") return True # Send actual email await aiosmtplib.send( message, hostname=SMTP_HOST, port=SMTP_PORT, username=SMTP_USERNAME, password=SMTP_PASSWORD, start_tls=True ) 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}: {str(e)}") return False async def send_verification_email(to_email: str, token: str): """Send email verification link""" verification_url = f"{FRONTEND_URL}/verify-email?token={token}" subject = "Verify Your Email Address" html_content = f"""

Welcome to Our Community!

Thank you for registering with us. We're excited to have you join our community.

Please click the button below to verify your email address:

Verify Email

Or copy and paste this link into your browser:

{verification_url}

This link will expire in 24 hours.

If you didn't create an account, please ignore this email.

""" return await send_email(to_email, subject, html_content) async def send_approval_notification(to_email: str, first_name: str): """Send notification when user is approved""" login_url = f"{FRONTEND_URL}/login" subject = "Your Membership Application Has Been Approved!" html_content = f"""

Congratulations, {first_name}!

Great news! Your membership application has been approved.

You now have full access to all member features and events.

Login to Your Account

We look forward to seeing you at our events!

""" return await send_email(to_email, subject, html_content) async def send_payment_prompt_email(to_email: str, first_name: str): """Send payment prompt email after admin approval""" payment_url = f"{FRONTEND_URL}/plans" subject = "Complete Your LOAF Membership - Payment Required" html_content = f"""

🎉 You're Approved, {first_name}!

Great news! Your LOAF membership application has been approved by our admin team.

To activate your membership and gain full access, please complete your annual membership payment:

Complete Payment →

Once payment is processed, you'll have immediate access to:

  • Members-only events and gatherings
  • Community directory and networking
  • Exclusive member benefits and discounts
  • LOAF newsletter and updates

We're excited to have you join the LOAF community!

Questions? Contact us at support@loaf.org

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