53deb8e9a8
Tipos: EmailAttachment, EmailMessage, SMTPConfig. Puras: email_build_html, email_build_text, email_with_attachment, email_template_render. Impuras: smtp_connect (TLS/STARTTLS/plain), smtp_send (MIME multipart con adjuntos). Solo stdlib: net/smtp, crypto/tls, text/template, mime/multipart. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
47 lines
1.8 KiB
Markdown
47 lines
1.8 KiB
Markdown
---
|
|
name: smtp_send
|
|
kind: function
|
|
lang: go
|
|
domain: infra
|
|
version: "1.0.0"
|
|
purity: impure
|
|
signature: "func SMTPSend(client *smtp.Client, msg EmailMessage) error"
|
|
description: "Serializa un EmailMessage a MIME y lo envia usando el smtp.Client dado. Soporta texto plano, HTML, multipart/alternative y adjuntos (multipart/mixed)."
|
|
tags: [email, smtp, send, mime, multipart]
|
|
uses_functions: [smtp_connect_go_infra]
|
|
uses_types: [EmailMessage_go_infra]
|
|
returns: []
|
|
returns_optional: false
|
|
error_type: "error_go_core"
|
|
imports: ["bytes", "encoding/base64", "fmt", "mime", "mime/multipart", "net/smtp", "net/textproto", "strings", "time"]
|
|
params:
|
|
- name: client
|
|
desc: "cliente SMTP autenticado obtenido de SMTPConnect"
|
|
- name: msg
|
|
desc: "mensaje email con From, To, Subject y al menos un body (HTML o text)"
|
|
output: "nil si el envio fue exitoso; error con contexto si falla MAIL FROM, RCPT TO, DATA o la escritura MIME"
|
|
tested: true
|
|
tests:
|
|
- "envia mensaje texto plano via mock smtp"
|
|
- "envia mensaje html via mock smtp"
|
|
- "envia con adjunto via mock smtp"
|
|
test_file_path: "functions/infra/smtp_send_test.go"
|
|
file_path: "functions/infra/smtp_send.go"
|
|
---
|
|
|
|
## Ejemplo
|
|
|
|
```go
|
|
cfg := SMTPConfig{Host: "smtp.gmail.com", Port: 587, Username: "u@gmail.com", Password: "pw", TLSMode: "starttls"}
|
|
client, err := SMTPConnect(cfg)
|
|
if err != nil { log.Fatal(err) }
|
|
defer client.Quit()
|
|
|
|
msg := EmailBuildHTML("u@gmail.com", []string{"dest@example.com"}, "Asunto", "<b>Hola</b>")
|
|
if err := SMTPSend(client, msg); err != nil { log.Fatal(err) }
|
|
```
|
|
|
|
## Notas
|
|
|
|
Funcion impura — envia datos por red. El cliente debe estar autenticado (obtenido de `SMTPConnect`). BCC se incluye en RCPT TO pero no en las cabeceras MIME visibles. Los adjuntos se codifican en base64. Para HTML+texto usa `multipart/alternative`; con adjuntos usa `multipart/mixed`.
|