smtpd.go 950 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "code.osinet.fr/fgm/smtpd"
  7. baseSmtpd "github.com/bradfitz/go-smtpd/smtpd"
  8. )
  9. func onNewConnection(c baseSmtpd.Connection) error {
  10. var err error
  11. log.Printf("New connection from %v\n", c.Addr())
  12. network := c.Addr().Network()
  13. if network != "tcp" {
  14. err = fmt.Errorf("unsupported network: %v", network)
  15. }
  16. return err
  17. }
  18. func onNewMail(c baseSmtpd.Connection, addr baseSmtpd.MailAddress) (baseSmtpd.Envelope, error) {
  19. var err error
  20. e := smtpd.DemoEnvelope{
  21. SmtpNetwork: c.Addr().Network(),
  22. SmtpRemote: c.Addr().String(),
  23. MailFrom: addr,
  24. }
  25. return &e, err
  26. }
  27. func main() {
  28. var addr string
  29. flag.StringVar(&addr, "addr", ":25", "The address on which to listen.")
  30. flag.Parse()
  31. s := baseSmtpd.Server{
  32. Addr: addr,
  33. OnNewConnection: onNewConnection,
  34. OnNewMail: onNewMail,
  35. }
  36. err := s.ListenAndServe()
  37. if err != nil {
  38. log.Fatalf("ListenAndServe: %v\n", err)
  39. }
  40. }