I am using "net/mail" and "net/smtp" to create an email client in Go, but it fails when it's behind the proxy.
I had the same issue for http client but it got resolved using &http.Transport{Proxy: http.ProxyFromEnvironment}
couldn't find a similar fix for SMTP
The below code works on my machine, which is behind the co-corporate proxy. but if I run the same code on a VM which is not behind any proxy, It works.
package main
import (
"fmt"
"net/smtp"
)
func main() {
fmt.Println("email sending")
// Set up authentication information.
auth := smtp.PlainAuth(
"",
"ma****017@gmail.com",
"A***a",
"smtp.gmail.com",
)
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
err := smtp.SendMail(
"smtp.gmail.com:587",
auth,
"ma***17@gmail.com",
[]string{"chi****11@gmail.com"},
[]byte("This is the email body."),
)
if err != nil {
panic(err)
}
fmt.Println("email sent")
}
@Flimzy I am new to networking stuff and Yes! Now I understand that it was less of coding and more of a networking issue.
Solution that worked for me was that I configured postfix on the proxy machine to relay it to the Gmail SMTP server. (make sure ur proxy machine has access to ur needed SMTP server)
Proxy machine ip needs to be set as an SMTP host in SMTP golang client.