p_header.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package backends
  2. import (
  3. "github.com/flashmob/go-guerrilla/mail"
  4. "strings"
  5. "time"
  6. )
  7. type HeaderConfig struct {
  8. PrimaryHost string `json:"primary_mail_host"`
  9. }
  10. // ----------------------------------------------------------------------------------
  11. // Processor Name: header
  12. // ----------------------------------------------------------------------------------
  13. // Description : Adds delivery information headers to e.DeliveryHeader
  14. // ----------------------------------------------------------------------------------
  15. // Config Options: none
  16. // --------------:-------------------------------------------------------------------
  17. // Input : e.Helo
  18. // : e.RemoteAddress
  19. // : e.RcptTo
  20. // : e.Hashes
  21. // ----------------------------------------------------------------------------------
  22. // Output : Sets e.DeliveryHeader with additional delivery info
  23. // ----------------------------------------------------------------------------------
  24. func init() {
  25. processors["header"] = func() Decorator {
  26. return Header()
  27. }
  28. }
  29. // Generate the MTA delivery header
  30. // Sets e.DeliveryHeader part of the envelope with the generated header
  31. func Header() Decorator {
  32. var config *HeaderConfig
  33. Svc.AddInitializer(InitializeWith(func(backendConfig BackendConfig) error {
  34. configType := BaseConfig(&HeaderConfig{})
  35. bcfg, err := Svc.ExtractConfig(backendConfig, configType)
  36. if err != nil {
  37. return err
  38. }
  39. config = bcfg.(*HeaderConfig)
  40. return nil
  41. }))
  42. return func(p Processor) Processor {
  43. return ProcessWith(func(e *mail.Envelope, task SelectTask) (Result, error) {
  44. if task == TaskSaveMail {
  45. to := strings.TrimSpace(e.RcptTo[0].User) + "@" + config.PrimaryHost
  46. hash := "unknown"
  47. if len(e.Hashes) > 0 {
  48. hash = e.Hashes[0]
  49. }
  50. protocol := "SMTP"
  51. if e.ESMTP {
  52. protocol = "E" + protocol
  53. }
  54. if e.TLS {
  55. protocol = protocol + "S"
  56. }
  57. var addHead string
  58. addHead += "Delivered-To: " + to + "\n"
  59. addHead += "Received: from " + e.RemoteIP + " ([" + e.RemoteIP + "])\n"
  60. if len(e.RcptTo) > 0 {
  61. addHead += " by " + e.RcptTo[0].Host + " with " + protocol + " id " + hash + "@" + e.RcptTo[0].Host + ";\n"
  62. }
  63. addHead += " " + time.Now().Format(time.RFC1123Z) + "\n"
  64. // save the result
  65. e.DeliveryHeader = addHead
  66. // next processor
  67. return p.Process(e, task)
  68. } else {
  69. return p.Process(e, task)
  70. }
  71. })
  72. }
  73. }