2

I am using go-smtp to attempt to send an email to myself: https://github.com/emersion/go-smtp

    auth := sasl.NewPlainClient("", USERNAME, PASSWORD)
    to := []string{USERNAME}

    msg := strings.NewReader("To: " + USERNAME + "\r\n" +
        "Subject: testing golang go-smtp!\r\n" +
        "\r\n" +
        "This is the email body.\r\n")

    err := smtp.SendMail(SERVER_HOST + ":" + SERVER_PORT, auth, USERNAME, to, msg)
    if err != nil {
        log.Fatal(err)
    }

The output is: EOF exit status 1

I am connecting to smtp.gmail.com:465 and can send email through thunderbird okay.

Thanks for your help.

Walter

2 Answers 2

6

Check out the "Configuration options" section here https://support.google.com/a/answer/176600?hl=en. It mentions that Gmail uses SSL for the SMTP server on port 465 and TLS for port 587. To resolve your issue, you could use port 587 because the smtp.SendMail() function internally makes a call to net.Dial() that uses plain TCP to send the mail traffic with a call to STARTTLS later in the process.

If you want to use port 465, here is a nice example that works for sending emails using SSL.

https://gist.github.com/chrisgillis/10888032

The main difference between the two methods as also mentioned in the above link is that in SSL, the TLS connection is established from the beginning whereas if you used port 587, the connection starts over plain TCP without encryption.

1
1

I also had this issue, go-smtp SendMail uses "localhost" under the covers in the client hello. I believe that the gmail server can reject "EHLO localhost" as a possible DOS attack. Internally you see the 1st error response to the EHLO is "Try again later", followed by EOF on the EHLO retry. Using a more unique name for the client hello, does not seem to get rejected. These means however, that you cannot rely on the canned "SendMail" function to relay off of smtp.gmail.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.