import smtplib import ssl from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from app.utils.logger import logger def send_email_google(to_email, subject, html_content, text_content=None): SMTP_SERVER = "smtp.gmail.com" PORT = 587 FROM_EMAIL = "support@mermsemr.com" APP_PASSWORD = "" # Set via env var: os.getenv("GMAIL_APP_PASSWORD") msg = MIMEMultipart("alternative") msg["Subject"] = subject msg["From"] = FROM_EMAIL msg["To"] = to_email if text_content: msg.attach(MIMEText(text_content, "plain")) msg.attach(MIMEText(html_content, "html")) context = ssl.create_default_context() try: with smtplib.SMTP(SMTP_SERVER, PORT) as server: server.ehlo() server.starttls(context=context) server.ehlo() server.login(FROM_EMAIL, APP_PASSWORD) server.sendmail(FROM_EMAIL, to_email, msg.as_string()) logger.info(f"Google email sent to {to_email}") except smtplib.SMTPAuthenticationError: logger.error("Google SMTP auth failed: check FROM_EMAIL and APP_PASSWORD") raise except smtplib.SMTPConnectError as e: logger.error(f"Google SMTP connection error: {e}") raise except Exception as e: logger.error(f"Google SMTP send failed: {e}") raise