1

I'm making a email send package using: "gopkg.in/gomail.v2" So I create a file and the call it Everything work fine. The config is loaded fine but when i tried to send the email i get this erro: "An existing connection was forced to be terminated by the remote host"

I read some problems from others and reed about changes in the TLSConfig so i made it. But still doesn't work.

func ConnectSmtpServer(smtpConfig SmtpConfig) (gomail.Dialer, SmtpConfig) {
    // load en .env config
    smtp_config := LoadConfig()

    // connect to smtp server
    smtp_server := gomail.NewDialer(smtp_config.Host, smtp_config.Port, smtp_config.Username, smtp_config.Password)
    smtp_server.TLSConfig = &tls.Config{
        InsecureSkipVerify: true,
        // Configuracion de cifrado
        CipherSuites: []uint16{
            tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
            tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
        },
        PreferServerCipherSuites: true,
        MinVersion:               tls.VersionTLS11,
        MaxVersion:               tls.VersionTLS11, 
    }
    return *smtp_server, smtp_config
}

func SendEmail(to string, subject string, body string, from string, server gomail.Dialer) {
    // prepare email
    new_email := gomail.NewMessage()
    new_email.SetHeader("From", from)
    new_email.SetHeader("To", to)
    new_email.SetHeader("Subject", subject)
    new_email.SetBody("text/html", body)
    
    // send email
    if err := server.DialAndSend(new_email); err != nil {
        // print more information about the error
        fmt.Printf("%s", err)
        panic(err)
    
    } else {
        fmt.Println("Email enviado a: " + to)
    }
}
Steffen Ullrich
  • 114,247
  • 10
  • 131
  • 172
  • 3
    Please explain what the intend with this strange TLS config is. Why do you limit both ciphers and TLS version to weak versions? Also `PreferServerCipherSuites` has no meaning on the client side. Also, it is unclear if the connection close has even something to do with TLS. Many hosting providers don't allow to send mail directly, many mail servers reject mails from DSL, cable .. accounts. – Steffen Ullrich Jul 06 '23 at 17:11

0 Answers0