p_header.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package backends
  2. import (
  3. "github.com/flashmob/go-guerrilla/envelope"
  4. "strings"
  5. "time"
  6. )
  7. type HeaderConfig struct {
  8. PrimaryHost string `json:"primary_mail_host"`
  9. }
  10. // Generate the MTA delivery header
  11. // Sets e.DeliveryHeader with the result
  12. func Header(dc *DecoratorCallbacks) Decorator {
  13. var config *HeaderConfig
  14. dc.loader = func(backendConfig BackendConfig) error {
  15. configType := baseConfig(&HeaderConfig{})
  16. bcfg, err := ab.extractConfig(backendConfig, configType)
  17. if err != nil {
  18. return err
  19. }
  20. config = bcfg.(*HeaderConfig)
  21. return nil
  22. }
  23. return func(c Processor) Processor {
  24. return ProcessorFunc(func(e *envelope.Envelope) (BackendResult, error) {
  25. to := strings.TrimSpace(e.RcptTo[0].User) + "@" + config.PrimaryHost
  26. hash := "unknown"
  27. if len(e.Hashes) > 0 {
  28. hash = e.Hashes[0]
  29. }
  30. var addHead string
  31. addHead += "Delivered-To: " + to + "\r\n"
  32. addHead += "Received: from " + e.Helo + " (" + e.Helo + " [" + e.RemoteAddress + "])\r\n"
  33. if len(e.RcptTo) > 0 {
  34. addHead += " by " + e.RcptTo[0].Host + " with SMTP id " + hash + "@" + e.RcptTo[0].Host + ";\r\n"
  35. }
  36. addHead += " " + time.Now().Format(time.RFC1123Z) + "\r\n"
  37. // save the result
  38. e.DeliveryHeader = addHead
  39. // next processor
  40. return c.Process(e)
  41. })
  42. }
  43. }