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