-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmultipart.lua
215 lines (190 loc) · 4.56 KB
/
multipart.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
203
204
205
206
207
208
209
210
211
212
213
214
215
--[=[
Multipart MIME encoding.
Written by Cosmin Apreutesei. Public Domain.
multipart(type, [write], [boundary]) -> m
m:add_raw(s)
m:add_base64(s)
m:add_part(headers)
m:add_field(name, val)
m:add_file(filename, content_type, contents)
m:add_inline_file(cid, filename, content_type, contents)
m:add_multipart(type) -> m
m:finish()
multipart_mail(msg) -> s
msg.text
msg.html '<img src="@foo">'
msg.inlines {{id='@foo', contents=},...}
msg.attachments {{file},...}
]=]
require'glue'
require'base64'
local mp = {utf8_headers = {subject=1}}
local function encode(s)
return _('=?utf-8?B?%s?=', base64_encode(s))
end
function multipart(multipart_type, write, boundary)
local t
if not write then
t = {}
function write(s)
t[#t+1] = s
end
end
local m = object(mp)
boundary = boundary or tohex(random_string(19))
m.content_type = _('multipart/%s; boundary=%s', multipart_type, boundary)
local function cmp(a, b) return tostring(a) < tostring(b) end
local first
function m:add_part(headers)
if first then
write'\r\n'
else
first = true
end
write'--'; write(boundary); write'\r\n'
local ht = {}
for k,v in pairs(headers) do
ht[k:gsub('_', '-'):lower()] = v
end
for k,v in sortedpairs(ht) do
write(k); write': '
if istab(v) then
local v, opt = v[1], v
if self.utf8_headers[k] then
v = encode(v)
end
write(v)
for k,v in sortedpairs(opt, cmp) do
if k ~= 1 then
write'; '
write(k); write'="'; write(v); write'"'
end
end
else
if self.utf8_headers[k] then
v = encode(v)
end
write(v)
end
write'\r\n'
end
write'\r\n'
end
function m:finish()
write'\r\n--'; write(boundary); write'--\r\n'
if t then
return concat(t)
end
end
function m:add_raw(s)
write(s)
end
function m:add_base64(s)
write(base64_encode(s, nil, nil, nil, 76))
end
function m:add_multipart(type)
local m = multipart(type, write)
self:add_part{content_type = m.content_type}
return m
end
function m:add_field(name, val)
self:add_part{
content_disposition = {'form-data', name = name},
content_transfer_encoding = 'base64',
}
if val then self:add_base64(val) end
end
function m:add_file(filename, content_type, contents)
self:add_part{
content_disposition = {'attachment', filename = filename},
content_transfer_encoding = 'base64',
content_type = content_type,
}
self:add_base64(contents)
end
--NOTE: gmail will show these as attachments.
function m:add_inline_file(cid, filename, content_type, contents)
self:add_part{
content_id = '<'..cid..'>',
content_disposition = {'inline', filename = filename},
content_transfer_encoding = 'base64',
content_type = content_type,
}
self:add_base64(contents)
end
return m
end
--https://stackoverflow.com/questions/3902455/mail-multipart-alternative-vs-multipart-mixed
function multipart_mail(msg)
local mix = multipart'mixed'
local alt = mix:add_multipart'alternative'
if msg.text then
alt:add_part{
content_type = {'text/plain', charset = 'utf-8'},
content_transfer_encoding = 'base64',
}
alt:add_base64(msg.text)
end
local rel = alt:add_multipart'related'
if msg.html then
rel:add_part{
content_type = {'text/html', charset = 'utf-8'},
content_transfer_encoding = 'base64',
}
rel:add_base64(msg.html)
end
if msg.inlines then
for _,file in ipairs(msg.inlines) do
rel:add_inline_file(file.cid, file.filename, file.content_type, file.contents)
end
end
rel:finish()
alt:finish()
if msg.attachments then
for _,file in ipairs(msg.attachments) do
mix:add_file(file.filename, file.content_type, file.contents)
end
end
local req = {headers = {}}
req.from = msg.from
req.to = msg.to
req.headers.from = msg.from
req.headers.to = msg.to
req.headers.subject = msg.subject
req.headers.content_type = mix.content_type
req.message = mix:finish()
return req
end
if not ... then
local req = multipart_mail{
from = '[email protected]',
text = 'Hello Dude!',
html = '<h1>Hello</h1><p>Hello Dude</p>',
inlines = {
{
cid = 'img1',
filename = 'progressive.jpg',
contents = load'../tests/jpeg_test/progressive.jpg',
},
{
cid = 'img2',
filename = 'birds.jpg',
contents = load'../tests/resize_image_test/birds.jpg',
},
},
attachments = {
{
filename = 'att1.txt',
content_type = 'text/plain',
contents = 'att1!',
},
{
filename = 'att2.txt',
content_type = 'text/plain',
contents = 'att2!',
},
},
}
pr(req.headers)
pr((req.message:gsub('\r', '')))
end