#!/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 = "mx.ym.163.com"
SMTP_PORT = 465              # ✅ SSL direct
SMTP_USER = "info@findbest.jp"
SMTP_PASS = "findbest2020"   # ⚠️ trebuie să fie CODUL DE AUTORIZARE, nu parola contului

# ─── Expeditor / Destinatar ──────────────────────────────────
FROM_EMAIL = "info@findbest.jp"
FROM_NAME  = "Info"
TO_EMAIL   = "test@example.com"       # ← schimbă destinatarul
SUBJECT    = "账户服务通知"

# ─── Conținut ────────────────────────────────────────────────
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 = """账户服务通知

您好：

我们为您整理了本次账单的相关内容，请查阅以下信息。

此致，
账户服务"""

# ─── 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="findbest.jp")
msg["MIME-Version"] = "1.0"

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

# ─── Context SSL (fix pentru Python 3.13 + NetEase) ──────────
context = ssl.create_default_context()
context.verify_flags &= ~ssl.VERIFY_X509_STRICT

# ─── Trimitere prin SMTP_SSL (port 465) ──────────────────────
try:
    print(f"📨 Conectare la {SMTP_HOST}:{SMTP_PORT} (SSL) ...")

    with smtplib.SMTP_SSL(SMTP_HOST, SMTP_PORT, timeout=20, context=context) as server:
        server.set_debuglevel(0)   # pune 1 ca să vezi conversația SMTP
        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"❌ Autentificare eșuată: {e}")
    print("   → Folosește CODUL DE AUTORIZARE (授权码), nu parola contului.")
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 conținut (probabil spam): {e}")
except smtplib.SMTPServerDisconnected as e:
    print(f"❌ Serverul a închis conexiunea: {e}")
    print("   → Verifică că IP-ul tău nu e blocat de NetEase.")
except ssl.SSLError as e:
    print(f"❌ Eroare SSL: {e}")
except Exception as e:
    print(f"❌ Eroare generală: {e}")
