-2

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")
}
Chinmay Samant
  • 258
  • 1
  • 3
  • 13
  • 2
    There's no such thing as an SMTP proxy, per se, except to the extent that every SMTP server can serve as a proxy. Which exact "system proxy settings" were you thinking of using? – Jonathan Hall Nov 10 '18 at 10:08
  • See https://github.com/Supme/directEmail/blob/master/send.go for some code that diverts SMTP through a socks proxy – Vorsprung Nov 10 '18 at 11:31
  • i am not trying to make a proxy server. the vm on where my code runs is behind a proxy, so i need a way by which my email client will make calls using proxy – Chinmay Samant Nov 10 '18 at 12:44
  • 1
    @ChinmaySamant: This isn't a coding problem. You need to allow access to an SMTP server _somehow_, from within your VM. A common way to do this is with a 'smarthost' (the SMTP term most similar to a 'proxy'), but this is entirely a networking/server issue, nothing to do with coding. – Jonathan Hall Nov 10 '18 at 15:34
  • i fail to understand how it's not a coding problem.!! for http client in GO to work behind proxy i had to use `&http.Transport{Proxy: http.ProxyFromEnvironment}` so in similar way, i am asking what should i use for smtp clinet in GO to make it work behind proxy? – Chinmay Samant Nov 11 '18 at 05:45
  • Your "proxy" is an HTTP proxy. It proxies HTTP. It does not proxy SMTP. Contact your system administrator to find out the correct SMTP server to use for outgoing mail. – Michael Hampton Nov 14 '18 at 17:18
  • @MichaelHampton yes !! we are using squid for proxy and we do have a socks proxy server.. but how will I configure these settings in the smtp go client. – Chinmay Samant Nov 15 '18 at 07:35
  • for smtp proxy we have ngnx smtp proxy server – Chinmay Samant Nov 15 '18 at 12:20

1 Answers1

1

@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.

Chinmay Samant
  • 258
  • 1
  • 3
  • 13
  • For the benefit of users like me who are facing exactly same problem, it will be great if you can write in more detail how you tackled and solved this issue. I am not able to find any site on the net where it is explained in clear terms. – rnso Sep 05 '19 at 16:44
  • when your VM is behind a firewall and if you try to access anything which is outside the firewall (ex. any public internet site), you won't be able to. – Chinmay Samant Oct 17 '19 at 10:41