SMTP Server implementation

792 views
Skip to first unread message

Sean Murphy

unread,
Aug 12, 2015, 10:00:29 AM8/12/15
to golang-nuts
Hey golang-nuts,

After working in go for almost a year now and finding the net/http server package so eloquent and productive to work in I found myself wanting an smtp implementation as well. The go project has a few relevant packages that have gotten me to a successful go mail server (perhaps more just a Mail Exchanger) I then began wondering if I could create an smtp package that would make it really simple for devs to start up a smtp server in the same spirit of simplicity that the http package provides. Something like:

smtp.HandleFunc("ad...@example.com", func(r *smtp.Connection){
  fmt.Println("Incoming message!")
  fmt.Println(" RemoteAddr:", r.RemoteAddr)
  fmt.Println(" From:", r.From)
  for _, t := range r.To{
    fmt.Println(" To:", t)
  }
  fmt.Println(" Message:", r.Message)
}
smtp.ListenAndServeTLS(":2525", "cert.pem", "key.pem", nil)

Questions

1. Because smtp is stateful, I'm not really sure how to handle things like authentication, verification, expand, etc
  a. One of my ideas is to make 'handlers' more than a .ServeSMTP(r *smtp.Request) interface, perhaps it has multiple callbacks for each command that might come through smtp, but abstracted away from the protocol a bit. For example verify might have the signature func Verify(email string) bool, and expand: func Expand(email string) []string
  b. I thought about utilizing the handler to 'verify' addresses that the server can handle. So internally the server would just call the .Handle method and if the muxer had a registered result than the server would allow that recipient or respond accordingly to verify command.
  c. Authentication would obviously need some callback, should that be on the server? Or perhaps on the servers handler as mentioned above?
2. The smtp rfc's have recommendations for limits on line length. My idea is to expand textproto to read lines up to a certain length before they would return with an error perhaps. This would need some additional methods, or some way to set the read length on the connection.
3. I'd also like to expand message a bit more, add the ability to set headers, write/modify the body. It also seems like a good place to throw in a method for dkim validation and signing.
4. Is there interest for adding this to the standard library?

Proposal

To sum up what I'm thinking:
Add sa server to the net/smtp package
- A default server will be provided
- The default server will use a smtp muxer that surfaces HandleFunc and Handle methods
Provide byte limit readers to the net/textproto package
Add functionality to the net/message package
- Ability to write new/replace headers

Thanks for taking the time to read through this and consider a contribution to golang. I hope this sparks a good discussion and gives me a better idea of whether this would be a valuable contribution.

Ian Lance Taylor

unread,
Aug 12, 2015, 10:17:17 AM8/12/15
to Sean Murphy, golang-nuts
On Wed, Aug 12, 2015 at 12:16 AM, Sean Murphy <murphy...@gmail.com> wrote:
>
> 4. Is there interest for adding this to the standard library?

I think that Go is a great language for writing an SMTP server and I
would encourage you to work on it if you are interested. But I don't
think it is a good fit for the standard library. An SMTP server must
inherently make decisions like where to store messages and how to
reject spam. Those are too complex for the standard library. And
while we could provide an SMTP server framework as we do an HTTP
server framework, I think that very few programs would use it, so,
again, not a good fit.

See also http://tip.golang.org/doc/faq#x_in_std .

Ian

Egon

unread,
Aug 12, 2015, 10:43:59 AM8/12/15
to golang-nuts
On Wednesday, 12 August 2015 17:00:29 UTC+3, Sean Murphy wrote:
Hey golang-nuts,

After working in go for almost a year now and finding the net/http server package so eloquent and productive to work in I found myself wanting an smtp implementation as well.

Manlio Perillo

unread,
Aug 12, 2015, 10:54:14 AM8/12/15
to golang-nuts, murphy...@gmail.com


Il giorno mercoledì 12 agosto 2015 16:17:17 UTC+2, Ian Lance Taylor ha scritto:
On Wed, Aug 12, 2015 at 12:16 AM, Sean Murphy <murphy...@gmail.com> wrote:
>
> 4. Is there interest for adding this to the standard library?

I think that Go is a great language for writing an SMTP server and I
would encourage you to work on it if you are interested.

I would like to see a Go re-implementation for all the network client/servers for "personal" use I have installed on my system.
Many of these are written in C.

As an example on my system I have installed:

- pdns
- msmtp
- isync
- wget
- curl


Regards  Manlio

Matt Harden

unread,
Aug 12, 2015, 12:34:47 PM8/12/15
to Manlio Perillo, golang-nuts, murphy...@gmail.com

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages