Files
fn_registry/python/functions/infra/smtp_send.md
T
egutierrez be081c68f2 feat: funciones email SMTP en Python (infra)
smtp_send: conecta+envia+cierra en un paso via smtplib (TLS/STARTTLS/plain).
email_build_html: construye EmailMessagePy frozen dataclass con cuerpo HTML.
Solo stdlib Python: smtplib, email.mime. Tests con mock SMTP server threading.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 02:03:12 +02:00

2.7 KiB

name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, params, output, tested, tests, test_file_path, file_path
name kind lang domain version purity signature description tags uses_functions uses_types returns returns_optional error_type imports params output tested tests test_file_path file_path
smtp_send function py infra 1.0.0 impure smtp_send(cfg: SMTPConfigPy, from_addr: str, to: list[str], subject: str, body_html: str = '', body_text: str = '', cc: list[str] | None = None, bcc: list[str] | None = None, attachments: list[EmailAttachmentPy] | None = None, headers: dict[str, str] | None = None) -> None Conecta al servidor SMTP, construye el mensaje MIME y envia el email en un solo paso. Soporta TLS directo (port 465), STARTTLS (port 587) y sin cifrado (port 25). Cierra la conexion automaticamente.
email
smtp
send
python
smtplib
mime
tls
false error_go_core
smtplib
email.mime.multipart
email.mime.text
email.mime.base
email.encoders
dataclasses
name desc
cfg configuracion SMTP: host, port, username, password, tls_mode ('tls', 'starttls' o '')
name desc
from_addr direccion del remitente
name desc
to lista de destinatarios principales
name desc
subject asunto del correo
name desc
body_html cuerpo HTML (opcional; puede estar vacio si body_text esta presente)
name desc
body_text cuerpo de texto plano (opcional; puede estar vacio si body_html esta presente)
name desc
cc lista de destinatarios en copia visible (opcional)
name desc
bcc lista de destinatarios en copia oculta (opcional)
name desc
attachments lista de EmailAttachmentPy con filename, content_type y data binarios (opcional)
name desc
headers diccionario de headers MIME adicionales como X-Mailer (opcional)
None si el envio fue exitoso; lanza RuntimeError con descripcion del fallo SMTP true
envia texto plano via mock smtpd
envia html via mock smtpd
envia con adjunto via mock smtpd
error si host no existe
python/functions/infra/smtp_send_test.py python/functions/infra/smtp_send.py

Ejemplo

from infra.smtp_send import smtp_send, SMTPConfigPy, EmailAttachmentPy

cfg = SMTPConfigPy(host="smtp.gmail.com", port=587, username="u@gmail.com", password="app-pw")
smtp_send(
    cfg,
    from_addr="u@gmail.com",
    to=["dest@example.com"],
    subject="Reporte",
    body_html="<h1>Hola</h1>",
    body_text="Hola",
)

Notas

Funcion impura — abre conexion TCP real. Usa solo stdlib Python (smtplib, email). Para TLS directo (port 465) usa SMTP_SSL; para STARTTLS (port 587) usa SMTP + starttls(). Los adjuntos se codifican en base64. BCC se incluye en sendmail pero no en las cabeceras MIME visibles.