-
Notifications
You must be signed in to change notification settings - Fork 6
/
sendmail.go
149 lines (129 loc) · 3.24 KB
/
sendmail.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Package sendmail is intended for direct sending of emails.
package sendmail
import (
"bytes"
"encoding/base64"
"errors"
"net/mail"
"os"
"os/user"
"sort"
"strings"
"sync"
)
var (
wg sync.WaitGroup
)
// Config of envelope
type Config struct {
Sender string
Recipients []string
Subject string
Body []byte
PortSMTP string
}
// Envelope of message
type Envelope struct {
*mail.Message
Recipients []string
PortSMTP string
}
// NewEnvelope return new message envelope
func NewEnvelope(config *Config) (Envelope, error) {
msg, err := mail.ReadMessage(bytes.NewReader(config.Body))
if err != nil {
if len(config.Recipients) > 0 {
msg, err = GetDumbMessage(config.Sender, config.Recipients, config.Body)
}
if err != nil {
return Envelope{}, err
}
}
if config.PortSMTP == "" {
config.PortSMTP = "25"
}
if config.Sender != "" {
msg.Header["From"] = []string{config.Sender}
} else {
config.Sender = msg.Header.Get("From")
if config.Sender == "" {
user, err := user.Current()
if err == nil {
hostname, err := os.Hostname()
if err == nil {
config.Sender = user.Username + "@" + hostname
msg.Header["From"] = []string{config.Sender}
}
}
}
}
if config.Subject != "" {
msg.Header["Subject"] = []string{"=?UTF-8?B?" + base64.StdEncoding.EncodeToString([]byte(config.Subject))}
}
var recipients []string
if len(config.Recipients) > 0 {
recipient, err := mail.ParseAddressList(strings.Join(config.Recipients, ","))
if err == nil {
recipients = AddressListToSlice(recipient)
}
} else {
recipientsList, err := msg.Header.AddressList("To")
if err != nil {
return Envelope{}, err
}
rcpt := func(field string) []*mail.Address {
if recipient, err := msg.Header.AddressList(field); err == nil {
return recipient
}
return nil
}
recipientsList = append(recipientsList, rcpt("Cc")...)
recipientsList = append(recipientsList, rcpt("Bcc")...)
recipients = AddressListToSlice(recipientsList)
}
if len(recipients) == 0 {
return Envelope{}, errors.New("no recipients listed")
}
if msg.Header.Get("Message-ID") == "" {
msg.Header["Message-ID"] = []string{generateMessageID(GetDomainFromAddress(recipients[0]))}
}
return Envelope{msg, recipients, config.PortSMTP}, nil
}
// Send message.
// It returns channel for results of send.
// After the end of sending channel are closed.
func (e *Envelope) Send() <-chan Result {
smartHost := os.Getenv("SENDMAIL_SMART_HOST")
if smartHost != "" {
return e.SendSmarthost(
smartHost,
os.Getenv("SENDMAIL_SMART_LOGIN"),
os.Getenv("SENDMAIL_SMART_PASSWORD"),
)
}
return e.SendLikeMTA()
}
// GenerateMessage create body from mail.Message
func (e *Envelope) GenerateMessage() ([]byte, error) {
if len(e.Header) == 0 {
return nil, errors.New("empty header")
}
buf := bytes.NewBuffer(nil)
keys := make([]string, 0, len(e.Header))
for key := range e.Header {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
buf.WriteString(key + ": " + strings.Join(e.Header[key], ",") + "\r\n")
}
buf.WriteString("\r\n")
_, err := buf.ReadFrom(e.Body)
if err != nil {
return nil, err
}
if !bytes.HasSuffix(buf.Bytes(), []byte("\r\n")) {
buf.WriteString("\r\n")
}
return buf.Bytes(), nil
}