-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtls-mailer.lua
202 lines (183 loc) · 4.49 KB
/
tls-mailer.lua
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
local socket, smtp, mime, ltn12, ssl, tls_params
local is_openresty = not not _G.ngx
if is_openresty then
socket = require "resty.socket"
smtp = require "resty.smtp"
mime = require "resty.smtp.mime"
ltn12 = require "resty.smtp.ltn12"
tls_params = function(check_cert)
return {
port = 465,
ssl = {
enable = true,
verify_cert = (not not check_cert),
},
}
end
else
socket = require "socket"
mime = require "mime"
smtp = require "socket.smtp"
ltn12 = require "ltn12"
ssl = require "ssl"
tls_params = function(check_cert, tls_protocol)
return {
mode = "client",
options = "all",
port = 465,
protocol = tls_protocol or "tlsv1_2",
verify = check_cert and "peer" or "none",
}
end
end
local map_merge = function(m1, m2)
for k,v in pairs(m2) do
if m1[k] == nil then m1[k] = v end
end
end
local tls_tcp = function(pp)
local cnx_idx = function(_, key)
return function(proxy, ...)
return proxy.sock[key](proxy.sock, ...)
end
end
local cnx_connect = function()
return function(self, host, port)
socket.try(self.sock:connect(host, port))
self.sock = socket.try(ssl.wrap(self.sock, pp))
socket.try(self.sock:dohandshake())
return 1
end
end
return function()
local cnx = {
sock = socket.try(socket.tcp()),
connect = cnx_connect(),
}
return setmetatable(cnx, {__index = cnx_idx})
end
end
local email_for; email_for = function(x)
if type(x) == "string" then
return string.format("<%s>", x)
elseif type(x) == "table" then
assert(x.email)
return email_for(x.email)
else error("bad type") end
end
local header_for = function(x)
if type(x) == "string" then
return email_for(x)
elseif type(x) == "table" then
assert(x.email and x.name)
return string.format("%s <%s>", x.name, x.email)
else error("bad type") end
end
local send = function(self, pp)
assert(pp.from and pp.to and pp.subject and pp.text)
local headers = {
from = header_for(pp.from),
to = header_for(pp.to),
subject = pp.subject,
}
local body = {{
headers = {
["content-type"] = "text/plain; charset=UTF-8",
["content-transfer-encoding"] = "quoted-printable",
},
body = ltn12.source.chain(
ltn12.source.string(pp.text),
ltn12.filter.chain(
mime.normalize(),
mime.encode("quoted-printable"),
mime.wrap("quoted-printable")
)
),
}}
if pp.attach then
local att
for i=1,#pp.attach do
att = pp.attach[i]
assert(att.mimetype and att.fname and att.source)
assert(att.source.string or att.source.fname)
local _h = {
["content-type"] = string.format(
"%s; name=\"%s\"", att.mimetype, att.fname
),
["content-disposition"] = string.format(
"attachment; filename=\"%s\"", att.fname
),
["content-transfer-encoding"] = "base64",
}
local _s
if att.source.string then
_s = ltn12.source.string(att.source.string)
else
_s = ltn12.source.file(io.open(att.source.fname, "rb"))
end
local _b = ltn12.source.chain(
_s,
ltn12.filter.chain(
mime.normalize(),
mime.encode("base64"),
mime.wrap("base64")
)
)
body[#body+1] = {
headers = _h,
body = _b,
}
end
end
local source = smtp.message({
headers = headers,
body = body,
})
local msg = {
from = email_for(pp.from),
rcpt = email_for(pp.to),
source = source,
}
if self.use_tls then
local params = tls_params(self.check_cert, self.tls_protocol)
map_merge(params, self.params)
map_merge(msg, params)
if not is_openresty then
msg.create = tls_tcp(params)
end
else
map_merge(msg, self.params)
end
local r, e = smtp.send(msg)
if r == 1 then
return true, {email = msg.rcpt}
else
return false, {r = r, e = e}
end
end
local methods = {
send = send,
}
local new = function(pp)
assert(
(type(pp.server) == "string") and
(type(pp.user) == "string") and
(type(pp.password) == "string")
)
local r = {
use_tls = (pp.use_tls == nil) or pp.use_tls,
check_cert = (pp.check_cert == nil) or pp.check_cert,
tls_protocol = pp.tls_protocol,
params = {
server = pp.server,
user = pp.user,
password = pp.password,
port = pp.port,
cafile = pp.cafile,
},
}
return setmetatable(r, {__index = methods})
end
return {
new = new,
}