go-smtp
go-smtp copied to clipboard
Custom greeting and DATA response
-
Methods of
Session
interface have to returnsmtp.SMTPError{...}
to customize returned smtp code and message. Although it's ok to set smtp code250
for normal exit withSMTPError{...}
, but will the logic of handlingSMTPError
change internally ingo-smtp
someday? Should we separate a normal exit (with custom return message) and error (e.g.SMTPResponse
and existingSMTPError)
? Should we expose something likeconn.WriteResponse()
? -
No way to customize the greeting text. We'd like to customize it and display our product name. https://github.com/emersion/go-smtp/blob/master/conn.go#L937
How about add a new attribute like
Banner
inServer
struct for this purpose, with default value set to currentfmt.Sprintf("%v ESMTP Service Ready", c.server.Domain)
. PR #132 fixes this.Postfix MTA uses parameter
smtpd_banner
for this greeting text, so i thinkBanner
should be fine.
Just don't try customize messages, they aren't shown anywhere to the user.
How about the first request, easier / clearer SMTP response?
This also sounds like customization.
But why doesn't allow developers to customize SMTP response?
- We may reject message and reply 554.
- Server may have some internal error and can not even save message, we reply 451 and ask sender server to try again later.
Reasonable? :)
In both of these cases you don't reply 250, right?
In both of these cases you don't reply 250, right?
Yes, and 4xx, 5xx smtp code should end the smtp session.
If you're hitting an error, returning an SMTPError
is fine. If you're not hitting an error, return a nil error.
We return 4xx/5xx on purpose. For example:
- Access control. we don't allow this sender server to contact us.
- Relied resource error. e.g. can not save message on disk (disk is full), SQL operation failed and return 4xx to ask sender server to retry.
For first case, it's not an "error" like "disk is full", but we still need to reject the session.
My suggestion is: Add a new struct SMTPReply
, which is same as SMTPError
. The word "Error" in struct name SMTPError
is some kind of confusing, and it introduces a concern that the internal process to handle an error may be not what developers want. We handle a lot errors in Golang, but this SMTPError
is just a normal smtp reply to end the session.
- SMTPError is treated just like any other error, even if 2xx code is used in it.
- Customizing non-failure responses is not supported and will not be - it is just not worth the hassle.
- I'm not strictly against customizing the initial response ("server banner"). Adding Server.Banner that would replace "ESMTP Service Ready" is not a big deal, isn't it? CC @emersion.
- Adding a special error type to terminate the connection may be a good idea. This could be used in policy mechanisms to get rid of malicious connections as fast as possible.