#!/usr/bin/env python3
import smtplib
import ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate, make_msgid

# ─── Configurare SMTP ────────────────────────────────────────
SMTP_HOST = "smtp.ym.163.com"
SMTP_PORT = 587
SMTP_USER = "info@ryukasou.com"
SMTP_PASS = "Ryukasou391"
USE_TLS   = True          # STARTTLS pe 587

# ─── Expeditor / Destinatar ──────────────────────────────────
FROM_EMAIL = "info@ryukasou.com"
FROM_NAME  = "Info"
TO_EMAIL   = "test@example.com"        # ← pune destinatarul aici
SUBJECT    = "账户服务通知"

# ─── Conținut HTML ───────────────────────────────────────────
HTML_BODY = """\
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"></head>
<body style="margin:0; padding:0; background:#ffffff; font-family:Arial,sans-serif; color:#333; font-size:15px; line-height:1.7;">
<div style="max-width:600px; margin:0 auto; padding:20px;">
  <h1 style="font-size:20px; font-weight:600; margin:25px 0 20px 0;">账户服务通知</h1>
  <p style="margin:0 0 14px 0;">您好：</p>
  <p style="margin:0 0 14px 0;">我们为您整理了本次账单的相关内容，请查阅以下信息。</p>
  <div style="background:#f7f7f7; padding:14px; margin:20px 0; border-left:3px solid #999;">
    <p style="margin:0; font-weight:600;">服务提醒</p>
    <p style="margin:6px 0 0 0; color:#555;">如需查看完整信息，请进入账户中心。</p>
  </div>
  <p style="margin:22px 0 0 0; color:#666; font-size:13px;">此致，</p>
  <p style="margin:0; color:#666; font-size:13px;">账户服务</p>
</div>
</body>
</html>
"""

TEXT_BODY = "账户服务通知\n\n您好：\n\n我们为您整理了本次账单的相关内容，请查阅以下信息。\n\n此致，\n账户服务"

# ─── Construire mesaj ────────────────────────────────────────
msg = MIMEMultipart("alternative")
msg["From"]       = f"{FROM_NAME} <{FROM_EMAIL}>"
msg["To"]         = TO_EMAIL
msg["Subject"]    = SUBJECT
msg["Date"]       = formatdate(localtime=True)
msg["Message-ID"] = make_msgid(domain=SMTP_HOST.split(".")[-2] + "." + SMTP_HOST.split(".")[-1])
msg["MIME-Version"] = "1.0"

msg.attach(MIMEText(TEXT_BODY, "plain", "utf-8"))
msg.attach(MIMEText(HTML_BODY, "html",  "utf-8"))

# ─── Trimitere ───────────────────────────────────────────────
try:
    print(f"📨 Conectare la {SMTP_HOST}:{SMTP_PORT} ...")
    context = ssl.create_default_context()

    with smtplib.SMTP(SMTP_HOST, SMTP_PORT, timeout=20) as server:
        server.set_debuglevel(0)          # pune 1 pentru debug detaliat
        server.ehlo()

        if USE_TLS:
            server.starttls(context=context)
            server.ehlo()

        server.login(SMTP_USER, SMTP_PASS)
        server.sendmail(FROM_EMAIL, [TO_EMAIL], msg.as_string())

    print("✅ Email trimis cu succes!")

except smtplib.SMTPAuthenticationError as e:
    print(f"❌ Eroare autentificare: {e}")
except smtplib.SMTPRecipientsRefused as e:
    print(f"❌ Destinatar refuzat: {e}")
except smtplib.SMTPSenderRefused as e:
    print(f"❌ Expeditor refuzat: {e}")
except smtplib.SMTPDataError as e:
    print(f"❌ Eroare date (probabil spam/conținut): {e}")
except smtplib.SMTPConnectError as e:
    print(f"❌ Nu mă pot conecta: {e}")
except smtplib.SMTPException as e:
    print(f"❌ Eroare SMTP: {e}")
except Exception as e:
    print(f"❌ Eroare generală: {e}")
