main.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package smtp_server
  2. import (
  3. "crypto/tls"
  4. "github.com/emersion/go-smtp"
  5. log "github.com/sirupsen/logrus"
  6. "net"
  7. "pmail/config"
  8. "time"
  9. )
  10. // The Backend implements SMTP server methods.
  11. type Backend struct{}
  12. func (bkd *Backend) NewSession(conn *smtp.Conn) (smtp.Session, error) {
  13. remoteAddress := conn.Conn().RemoteAddr()
  14. return &Session{
  15. RemoteAddress: remoteAddress,
  16. }, nil
  17. }
  18. // A Session is returned after EHLO.
  19. type Session struct {
  20. RemoteAddress net.Addr
  21. }
  22. func (s *Session) AuthPlain(username, password string) error {
  23. return nil
  24. }
  25. func (s *Session) Mail(from string, opts *smtp.MailOptions) error {
  26. return nil
  27. }
  28. func (s *Session) Rcpt(to string) error {
  29. return nil
  30. }
  31. func (s *Session) Reset() {}
  32. func (s *Session) Logout() error {
  33. return nil
  34. }
  35. func Start() {
  36. be := &Backend{}
  37. s := smtp.NewServer(be)
  38. s.Addr = ":25"
  39. s.Domain = config.Instance.Domain
  40. s.ReadTimeout = 10 * time.Second
  41. s.WriteTimeout = 10 * time.Second
  42. s.MaxMessageBytes = 1024 * 1024
  43. s.MaxRecipients = 50
  44. // force TLS for auth
  45. s.AllowInsecureAuth = false
  46. // Load the certificate and key
  47. cer, err := tls.LoadX509KeyPair(config.Instance.SSLPublicKeyPath, config.Instance.SSLPrivateKeyPath)
  48. if err != nil {
  49. log.Fatal(err)
  50. return
  51. }
  52. // Configure the TLS support
  53. s.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cer}}
  54. log.Println("Starting server at", s.Addr)
  55. if err := s.ListenAndServe(); err != nil {
  56. log.Fatal(err)
  57. }
  58. }