go-gomail / gomail

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

Cannot send email from docker container #57

Closed cescoferraro closed 8 years ago

cescoferraro commented 8 years ago

dialandsend return an error when inside a privileged docker container When inside a kubernetes, the error does not seem to happen, but the email do not get sended.

web_1 | /api/register
web_1 | 172.18.0.1 - - [10/Apr/2016:20:51:59 +0000] "POST /api/register HTTP/1.1" 200 78
web_1 | http://localhost:9001/api/confirm/$2a$04$6Dad45u6SlTB7rWTNz2GzOc3j9jWOjT.1oTjfuvO5Bd8gKGwnvX56
web_1 | panic: x509: failed to load system roots and no roots provided
web_1 | 
web_1 | goroutine 49 [running]:
web_1 | panic(0x9f49c0, 0xe2e628)
web_1 |         /usr/lib/go/src/runtime/panic.go:464 +0x3e6
web_1 | bitbucket.org/cescoferraro/templatego/src/backend/api.SendRegisterEmail(0xc820210b60, 0x0, 0x0, 0xc8201f99d0, 0x9, 0xc8201fd540, 0x1b, 0xc820200b00, 0x3c, 0x0, ...)
web_1 |         /home/cesco/code/go/src/bitbucket.org/cescoferraro/templatego/src/backend/api/register.go:85 +0x7af
web_1 | created by bitbucket.org/cescoferraro/templatego/src/backend/api.RegisterEndPoint
web_1 |         /home/cesco/code/go/src/bitbucket.org/cescoferraro/templatego/src/backend/api/register.go:55 +0x656
templatego_web_1 exited with code 2
alexcesaro commented 8 years ago

What's inside the Docker container? You should try doing something like apt-get install -y ca-certificates if it is an Ubuntu / Debian container.

alexcesaro commented 8 years ago

It is not an issue specific to Gomail, it is an issue with your Docker image. Just look on Google: https://github.com/docker/docker/issues/3825 https://github.com/docker/docker/issues/5157 https://github.com/zenazn/goji/issues/126 http://blog.cloud66.com/x509-error-when-using-https-inside-a-docker-container/

cescoferraro commented 8 years ago

@alexcesaro its a busybox container. WIll dig into busybox ca-certificates now

cescoferraro commented 8 years ago

thanks

cescoferraro commented 8 years ago
FROM progrium/busybox
RUN  opkg-install ca-certificates
cescoferraro commented 8 years ago

Still not able to send email inside a kubernetes cluster. It works inside a container on my computer. Its so weird! @alexcesaro

alexcesaro commented 8 years ago

What error do you get?

cescoferraro commented 8 years ago

None, e-mails just do not get sended Any chance my digital ocean nodes Ips are black listed by Google? Is there a way To check it out? Ive had some issues like this with Java on heroku.

cescoferraro commented 8 years ago

Here its the code

    go SendRegisterEmail(r, user)
    SendApiResponse(w, http.StatusOK, true, "User Added")

}

func SendRegisterEmail(r *http.Request, user models.Users) {
    m := gomail.NewMessage()
    m.SetHeader("From", "arduinodocesco@gmail.com")
    m.SetHeader("To", user.Email)
    m.SetHeader("Subject", "Hello!")
    body , _ := bcrypt.GenerateFromPassword([]byte(user.Email), bcrypt.MinCost)
    pathinho := "api/confirm/" + string(body)
    u, _ := url.Parse("http://" + string(r.Host))
    u.Path = path.Join(u.Path, pathinho)
    s := u.String()
    fmt.Println(s)
    m.SetBody("text/html", s)
    d := gomail.NewDialer("smtp.gmail.com", 587, "arduinodocesco", "mypassword")
    if err := d.DialAndSend(m); err != nil {
        panic(err)
        fmt.Println("inside the error")
    }
    fmt.Println("after all")
}

Here are the logs


2016-04-11T12:27:36.853629184Z 10.100.13.4 - - [11/Apr/2016:12:27:36 +0000] "POST /api/register HTTP/1.0" 200 78
2016-04-11T12:27:36.872609688Z http://templatego.cescoferraro.xyz/api/confirm/$2a$04$DovdcQpiEVT8Zq.TaNHqYuiOwRhP3.phyigIBMXEvGjf.zTTfRZsO
alexcesaro commented 8 years ago

Does it prints "after all"? You should try removing the go on the first line to help debugging.

cescoferraro commented 8 years ago

it does not. Just tried without the gouroutine and I got the same result, expect that now I do not emit a response because the SendRegisterEmail stucks. "After all" does not get printed. It hangs on the DialAndSend call.

alexcesaro commented 8 years ago

It looks like a network error. You should first make sure that you can reach "smtp.gmail.com" from your container.

cescoferraro commented 8 years ago

like ping? is there a specific command I could use to do that?

alexcesaro commented 8 years ago

You could use telnet, do some searches on the web.