Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/smtp: Proposal to add timeout argument to main functions #36209

Closed
cgarvie opened this issue Dec 19, 2019 · 2 comments
Closed

net/smtp: Proposal to add timeout argument to main functions #36209

cgarvie opened this issue Dec 19, 2019 · 2 comments

Comments

@cgarvie
Copy link

cgarvie commented Dec 19, 2019

What version of Go are you using (go version)?

all versions

Does this issue reproduce with the latest release?

yes

What did you do?

Attempt to use libraries which build upon net/SMTP, or use net/SMTP itself, in order to send emails over SMTP and you will quickly realize that having different allowed timeouts for different stages of the connection is not supported by default using net/SMTP. I have not explored using a custom Client as a workaround, but other standard libraries like Python's smtplib support timeouts by default as function arguments. and even using a custom Client doesn't seem like it would allow different timeouts for different stages of the SMTP process.

What did you expect to see?

c, err := smtp.Dial("mail.example.com:25", timeout_in_seconds)
if err != nil {
	log.Fatal(err)
}
...
_, err = fmt.Fprintf(wc, "This is the email body", timeout_in_seconds)
if err != nil {
	log.Fatal(err)
}
err = wc.Close(timeout_in_seconds)
if err != nil {
	log.Fatal(err)
}

What did you see instead?

c, err := smtp.Dial("mail.example.com:25")
if err != nil {
	log.Fatal(err)
}
...
_, err = fmt.Fprintf(wc, "This is the email body")
if err != nil {
	log.Fatal(err)
}
err = wc.Close()
if err != nil {
	log.Fatal(err)
}

If my suggested behavior is desired, I would be happy to make this improvement myself

@mdlayher
Copy link
Member

Per the package documentation at https://golang.org/pkg/net/smtp/, net/smtp is frozen and is not accepting new features. In addition, changing the signature of existing functions would be a violation of the Go 1 compatibility promise.

Please use an alternative package if you need this functionality.

@dcormier
Copy link
Contributor

dcormier commented Aug 6, 2020

It's painful, but you can work around this limitation by specifying an existing net.Conn to smtp.NewClient, and calling the appropriate (net.Conn).Set*Deadline method before calling the *smtp.Client method that you want to have a timeout apply to. Just remember to clear the timeout afterwards (as needed) by calling (net.Conn).Set*Deadline(time.Time{}).

@golang golang locked and limited conversation to collaborators Aug 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants