go-gomail / gomail

The best way to send emails in Go.
MIT License
4.3k stars 570 forks source link

tls: first record does not look like a TLS handshake #135

Open atdevp opened 5 years ago

lifang218c commented 3 years ago

i get this error too! how can i do with this error?

lifang218c commented 3 years ago

我看了他的gomail的源码,如果你使用的newDial方法,然后你的端口号刚好是465, 那么就默认开启了ssl,他的这个配置项就变了。你最好确认一下你的项目。 我后面的解决办法是自己直接 给gomail.Dialer 这个结构体赋值。你也自己看看吧!

aliforever commented 2 years ago

You should set SSL to false after initializing the Dialer using gomail.NewDialer(...).

Bsheepcoder commented 2 months ago

I did the following test with gomail, the details are in the comments, hope it helps

import (
    "crypto/tls"
    "gopkg.in/gomail.v2"
)

func main() {
    // Create a new email message
    m := gomail.NewMessage()

    // Set the sender of the email
    m.SetHeader("From", "example@example.com")

    // Set the recipient of the email
    m.SetHeader("To", "example@example.com")

    // Set the CC (carbon copy) of the email, if needed
    // m.SetHeader("Cc", "another@example.com")

    // Set the subject of the email
    m.SetHeader("Subject", "Hello from Go!")

    // Set the body content of the email
    m.SetBody("text/plain", "Hello,\n\nThis is a test email sent from a Go application using gomail.")

    // If there are attachments, you can add them like this
    // m.Attach("/path/to/file.pdf")

    // Configure the parameters for the SMTP server
    d := gomail.NewDialer("smtp.example.com", 578, "example@example.com", "example")

    // If the SMTP server requires a TLS connection, you can set it like this
    d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
    d.SSL = true

    // For port 25, set SSL = false
    // For port 465, set SSL = true (SMTPS)
    // For port 587, set SSL = true (with STARTTLS)

    // Send the email
    if err := d.DialAndSend(m); err != nil {
        panic(err)
    }
}