acupedia-api/app/features/email/__init__.py

96 lines
5.4 KiB
Python

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import os
def send_invoice_email(to: str, invoice_number: str, invoice_url: str):
smtp_host = os.getenv("SMTP_HOST", "localhost")
smtp_port = int(os.getenv("SMTP_PORT", 1025))
msg = MIMEMultipart('alternative')
msg['Subject'] = f"Your Panacea Tax Invoice Receipt - {invoice_number}"
msg['From'] = "Panacea <onboarding@panacea.com>"
msg['To'] = to
html_content = f"""
<div style="font-family: sans-serif; padding: 20px; color: #333; max-width: 600px; margin: 0 auto; border: 1px solid #e2e8f0; border-radius: 16px;">
<div style="text-align: center; margin-bottom: 24px;">
<h2 style="color: #6366f1; margin: 0;">Panacea Systems</h2>
<p style="font-size: 12px; color: #64748b; margin-top: 4px; uppercase; font-weight: bold; tracking-wider;">Tax Compliance Receipt</p>
</div>
<p>Dear Customer,</p>
<p>Thank you for your purchase. Your payment has been successfully processed and verified. Please click the button below to view or download your compliance tax invoice from Razorpay:</p>
<div style="text-align: center; margin: 32px 0;">
<a href="{invoice_url}" style="padding: 12px 24px; background-color: #6366f1; color: white; text-decoration: none; border-radius: 8px; font-weight: bold; display: inline-block; box-shadow: 0 4px 6px -1px rgba(99, 102, 241, 0.2);">Download Invoice PDF</a>
</div>
<div style="background-color: #f8fafc; border: 1px solid #f1f5f9; padding: 16px; border-radius: 12px; font-size: 12px; line-height: 1.5; color: #475569;">
<strong>Invoice Number:</strong> {invoice_number}<br/>
<strong>Hosted URL:</strong> <a href="{invoice_url}" style="color: #6366f1; word-break: break-all;">{invoice_url}</a>
</div>
<p style="font-size: 12px; color: #94a3b8; margin-top: 32px; text-align: center; border-t: 1px solid #e2e8f0; pt: 16px;">
Integrated compliance notification. If you did not make this purchase, please contact support.
</p>
</div>
"""
msg.attach(MIMEText(html_content, 'html'))
try:
with smtplib.SMTP(smtp_host, smtp_port) as server:
server.sendmail(msg['From'], [to], msg.as_string())
print(f"[Mailhog] Email sent successfully to {to}")
return True
except Exception as e:
print(f"[Mailhog] Error sending email: {e}")
return False
def send_password_reset_email(to: str, otp_code: str):
"""Sends a 6-digit OTP verification code for password reset via SMTP/MailHog."""
smtp_host = os.getenv("SMTP_HOST", "localhost")
smtp_port = int(os.getenv("SMTP_PORT", 1025))
msg = MIMEMultipart('alternative')
msg['Subject'] = f"Acupedia Password Reset Code: {otp_code}"
msg['From'] = "Acupedia Security <security@acupedia.in>"
msg['To'] = to
html_content = f"""
<div style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; padding: 28px; color: #1e293b; max-width: 560px; margin: 0 auto; border: 1px solid #e2e8f0; border-radius: 20px; background-color: #ffffff;">
<div style="text-align: center; margin-bottom: 24px;">
<h2 style="color: #4f46e5; margin: 0; font-size: 24px; font-weight: 800; letter-spacing: -0.5px;">Acupedia</h2>
<p style="font-size: 11px; color: #64748b; margin-top: 4px; text-transform: uppercase; font-weight: 700; letter-spacing: 1px;">Security & Account Recovery</p>
</div>
<p style="font-size: 15px; line-height: 1.5; color: #334155; margin-bottom: 20px;">Hello,</p>
<p style="font-size: 14px; line-height: 1.6; color: #475569; margin-bottom: 24px;">
We received a request to reset the password for your Acupedia account. Use the 6-digit verification code below to complete the reset process:
</p>
<div style="text-align: center; margin: 28px 0; padding: 20px; background: #f8fafc; border: 2px dashed #cbd5e1; border-radius: 14px;">
<span style="font-size: 36px; font-weight: 800; letter-spacing: 8px; color: #4f46e5; font-family: monospace;">{otp_code}</span>
<p style="margin: 8px 0 0 0; font-size: 12px; color: #64748b; font-weight: 600;">Code expires in 15 minutes</p>
</div>
<p style="font-size: 13px; line-height: 1.5; color: #64748b; margin-bottom: 24px;">
If you did not request this password reset, please ignore this email or contact support if you suspect unauthorized access. Your password will not change until you verify this code.
</p>
<div style="border-top: 1px solid #f1f5f9; padding-top: 16px; text-align: center;">
<p style="font-size: 11px; color: #94a3b8; margin: 0;">
Acupedia Clinical Knowledge Systems &bull; Automated Security Service
</p>
</div>
</div>
"""
msg.attach(MIMEText(html_content, 'html'))
try:
with smtplib.SMTP(smtp_host, smtp_port) as server:
server.sendmail(msg['From'], [to], msg.as_string())
print(f"[Mailhog] Password reset OTP sent successfully to {to}")
return True
except Exception as e:
print(f"[Mailhog] Error sending password reset email: {e}")
return False