@@ -33,24 +33,45 @@ func SendEmail(subject, body string, cfg *config.Config) error {
3333 // Armar mensaje
3434 msg := buildMessage (subject , body , smtpCfg .From , to )
3535
36- // TLS con timeout
3736 dialer := & net.Dialer {Timeout : 5 * time .Second }
3837 tlsConfig := & tls.Config {
3938 InsecureSkipVerify : false ,
4039 ServerName : smtpCfg .Host ,
4140 }
42- conn , err := tls .DialWithDialer (dialer , "tcp" , addr , tlsConfig )
43- if err != nil {
44- return fmt .Errorf ("fallo al conectar con TLS: %w" , err )
45- }
4641
47- // Cliente SMTP
48- c , err := smtp .NewClient (conn , smtpCfg .Host )
49- if err != nil {
50- return fmt .Errorf ("fallo al crear cliente SMTP: %w" , err )
42+ var c * smtp.Client
43+
44+ if smtpCfg .UseTLS {
45+ conn , err := tls .DialWithDialer (dialer , "tcp" , addr , tlsConfig )
46+ if err != nil {
47+ return fmt .Errorf ("fallo al conectar con TLS: %w" , err )
48+ }
49+
50+ c , err = smtp .NewClient (conn , smtpCfg .Host )
51+ if err != nil {
52+ return fmt .Errorf ("fallo al crear cliente SMTP: %w" , err )
53+ }
54+ } else {
55+ conn , err := dialer .Dial ("tcp" , addr )
56+ if err != nil {
57+ return fmt .Errorf ("fallo al conectar: %w" , err )
58+ }
59+
60+ c , err = smtp .NewClient (conn , smtpCfg .Host )
61+ if err != nil {
62+ return fmt .Errorf ("fallo al crear cliente SMTP: %w" , err )
63+ }
64+
65+ if smtpCfg .UseSTARTTLS {
66+ if err := c .StartTLS (tlsConfig ); err != nil {
67+ return fmt .Errorf ("fallo en STARTTLS: %w" , err )
68+ }
69+ }
5170 }
52- defer c .Quit ()
71+
72+ // Cerrar la conexión limpiamente
5373 defer c .Close ()
74+ defer c .Quit ()
5475
5576 // Autenticación
5677 auth := smtp .PlainAuth ("" , smtpCfg .Username , smtpCfg .Password , smtpCfg .Host )
0 commit comments