Skip to content

Commit

Permalink
Chore: Add smtpd server logging in the CLI (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
axllent committed Aug 17, 2024
1 parent 5fc025b commit ba42cac
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion server/smtpd/smtpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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,
Expand All @@ -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 != "" {
Expand Down

0 comments on commit ba42cac

Please sign in to comment.