diff --git a/server/smtpd/smtpd.go b/server/smtpd/smtpd.go index 06bf33409..ba7d8d55c 100644 --- a/server/smtpd/smtpd.go +++ b/server/smtpd/smtpd.go @@ -21,6 +21,8 @@ import ( var ( // DisableReverseDNS allows rDNS to be disabled DisableReverseDNS bool + + errorResponse = regexp.MustCompile(`^[45]\d\d `) ) // MailHandler handles the incoming message to store in the database @@ -38,7 +40,7 @@ func Store(origin net.Addr, from string, to []string, data []byte) (string, erro msg, err := mail.ReadMessage(bytes.NewReader(data)) if err != nil { - logger.Log().Errorf("[smtpd] error parsing message: %s", err.Error()) + logger.Log().Warnf("[smtpd] error parsing message: %s", err.Error()) stats.LogSMTPRejected() return "", err } @@ -210,7 +212,17 @@ func Listen() error { return listenAndServe(config.SMTPListen, mailHandler, authHandler) } +// Translate the smtpd verb from READ/WRITE to from/to +func verbLogTranslator(verb string) string { + if verb == "READ" { + return "from" + } + + return "to" +} + func listenAndServe(addr string, handler smtpd.MsgIDHandler, authHandler smtpd.AuthHandler) error { + smtpd.Debug = true // to enable Mailpit logging srv := &smtpd.Server{ Addr: addr, MsgIDHandler: handler, @@ -221,6 +233,16 @@ func listenAndServe(addr string, handler smtpd.MsgIDHandler, authHandler smtpd.A AuthRequired: false, MaxRecipients: config.SMTPMaxRecipients, DisableReverseDNS: DisableReverseDNS, + LogRead: func(remoteIP, verb, line string) { + logger.Log().Debugf("[smtpd] %s %s: %s", verbLogTranslator(verb), remoteIP, line) + }, + LogWrite: func(remoteIP, verb, line string) { + if errorResponse.MatchString(line) { + logger.Log().Warnf("[smtpd] %s %s: %s", verbLogTranslator(verb), remoteIP, line) + } else { + logger.Log().Debugf("[smtpd] %s %s: %s", verbLogTranslator(verb), remoteIP, line) + } + }, } if config.Label != "" {