Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions notify/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,45 @@ func SendEmail(subject, body string, cfg *config.Config) error {
// Armar mensaje
msg := buildMessage(subject, body, smtpCfg.From, to)

// TLS con timeout
dialer := &net.Dialer{Timeout: 5 * time.Second}
tlsConfig := &tls.Config{
InsecureSkipVerify: false,
ServerName: smtpCfg.Host,
}
conn, err := tls.DialWithDialer(dialer, "tcp", addr, tlsConfig)
if err != nil {
return fmt.Errorf("fallo al conectar con TLS: %w", err)
}

// Cliente SMTP
c, err := smtp.NewClient(conn, smtpCfg.Host)
if err != nil {
return fmt.Errorf("fallo al crear cliente SMTP: %w", err)
var c *smtp.Client

if smtpCfg.UseTLS {
conn, err := tls.DialWithDialer(dialer, "tcp", addr, tlsConfig)
if err != nil {
return fmt.Errorf("fallo al conectar con TLS: %w", err)
}

c, err = smtp.NewClient(conn, smtpCfg.Host)
if err != nil {
return fmt.Errorf("fallo al crear cliente SMTP: %w", err)
}
} else {
conn, err := dialer.Dial("tcp", addr)
if err != nil {
return fmt.Errorf("fallo al conectar: %w", err)
}

c, err = smtp.NewClient(conn, smtpCfg.Host)
if err != nil {
return fmt.Errorf("fallo al crear cliente SMTP: %w", err)
}

if smtpCfg.UseSTARTTLS {
if err := c.StartTLS(tlsConfig); err != nil {
return fmt.Errorf("fallo en STARTTLS: %w", err)
}
}
}
defer c.Quit()

// Cerrar la conexión limpiamente
defer c.Close()
defer c.Quit()

// Autenticación
auth := smtp.PlainAuth("", smtpCfg.Username, smtpCfg.Password, smtpCfg.Host)
Expand Down