From 55c347003093fa29fea0cc1123aa4760886a4a6c Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 17 Dec 2016 18:22:14 -0500 Subject: [PATCH] Start saving SMTP messages in the maildrop. --- smtp.go | 38 +++++++++++++++++++++++++++++++++++++- smtp/server.go | 8 ++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/smtp.go b/smtp.go index fd071fd..24f635f 100644 --- a/smtp.go +++ b/smtp.go @@ -5,6 +5,9 @@ import ( "fmt" "net" "net/mail" + "os" + "path" + "strings" "src.bluestatic.org/mailpopbox/smtp" ) @@ -50,6 +53,9 @@ func (server *smtpServer) TLSConfig() *tls.Config { } func (server *smtpServer) VerifyAddress(addr mail.Address) smtp.ReplyLine { + if server.maildropForAddress(addr) == "" { + return smtp.ReplyBadMailbox + } return smtp.ReplyOK } @@ -58,6 +64,36 @@ func (server *smtpServer) OnEHLO() *smtp.ReplyLine { } func (server *smtpServer) OnMessageDelivered(en smtp.Envelope) *smtp.ReplyLine { - fmt.Printf("MSG: %#v\n%s\n", en, string(en.Data)) + maildrop := server.maildropForAddress(en.RcptTo[0]) + if maildrop == "" { + // TODO: log error + return &smtp.ReplyBadMailbox + } + + f, err := os.Create(path.Join(maildrop, en.ID+".msg")) + if err != nil { + // TODO: log error + return &smtp.ReplyBadMailbox + } + + smtp.WriteEnvelopeForDelivery(f, en) + f.Close() return nil } + +func (server *smtpServer) maildropForAddress(addr mail.Address) string { + domainIdx := strings.LastIndex(addr.Address, "@") + if domainIdx == -1 { + return "" + } + + domain := addr.Address[domainIdx+1:] + + for _, s := range server.config.Servers { + if domain == s.Domain { + return s.MaildropPath + } + } + + return "" +} diff --git a/smtp/server.go b/smtp/server.go index a574ef9..395a03d 100644 --- a/smtp/server.go +++ b/smtp/server.go @@ -2,6 +2,8 @@ package smtp import ( "crypto/tls" + "fmt" + "io" "net" "net/mail" "time" @@ -29,6 +31,12 @@ type Envelope struct { ID string } +func WriteEnvelopeForDelivery(w io.Writer, e Envelope) { + fmt.Fprintf(w, "Delivered-To: <%s>\r\n", e.RcptTo[0].Address) + fmt.Fprintf(w, "Return-Path: <%s>\r\n", e.MailFrom.Address) + w.Write(e.Data) +} + type Server interface { Name() string TLSConfig() *tls.Config -- 2.43.5