fix(crm): envío de cotización por correo con aiosmtplib.send (sin doble STARTTLS)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ernesto Herrera
2026-07-30 10:49:23 -06:00
parent 03055cd377
commit e269e46d88

View File

@@ -210,19 +210,25 @@ async def send_quote_email(
part.add_header("Content-Disposition", f'attachment; filename="cotizacion-{ref}.pdf"') part.add_header("Content-Disposition", f'attachment; filename="cotizacion-{ref}.pdf"')
msg.attach(part) msg.attach(part)
if not (cfg.SMTP_USER and cfg.SMTP_PASSWORD):
raise HTTPException(status_code=503, detail="El correo saliente (SMTP) no está configurado en el servidor.")
ctx = ssl.create_default_context() ctx = ssl.create_default_context()
ctx.check_hostname = False ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE ctx.verify_mode = ssl.CERT_NONE
try: try:
if cfg.SMTP_PORT == 465: # Puerto 465 = SSL implícito; los demás (587/2525/…) = STARTTLS.
async with aiosmtplib.SMTP(hostname=cfg.SMTP_HOST, port=cfg.SMTP_PORT, use_tls=True, tls_context=ctx) as smtp: await aiosmtplib.send(
await smtp.login(cfg.SMTP_USER, cfg.SMTP_PASSWORD) msg,
await smtp.send_message(msg) hostname=cfg.SMTP_HOST,
else: port=cfg.SMTP_PORT,
async with aiosmtplib.SMTP(hostname=cfg.SMTP_HOST, port=cfg.SMTP_PORT, tls_context=ctx) as smtp: username=cfg.SMTP_USER,
await smtp.starttls(tls_context=ctx) password=cfg.SMTP_PASSWORD,
await smtp.login(cfg.SMTP_USER, cfg.SMTP_PASSWORD) use_tls=(cfg.SMTP_PORT == 465),
await smtp.send_message(msg) start_tls=(cfg.SMTP_PORT != 465),
tls_context=ctx,
validate_certs=False,
timeout=30,
)
except Exception as exc: except Exception as exc:
logger.error("Error enviando cotización %s: %s", quote_id, exc) logger.error("Error enviando cotización %s: %s", quote_id, exc)
raise HTTPException(status_code=502, detail=f"No se pudo enviar el correo: {exc}") raise HTTPException(status_code=502, detail=f"No se pudo enviar el correo: {exc}")