03.02.2023, 09:11
Use this. Put multiple recipients into the to table. The first email is the main recipient, all others will receive a copy. Separate CC header is not needed.
Code:
to = { 'first@example.com', 'second@example.com', 'third@example.com' }
subject = 'CSV'
smtp = require('socket.smtp')
mime = require('mime')
ltn12 = require('ltn12')
if type(to) ~= 'table' then
to = { to }
end
for index, email in ipairs(to) do
to[ index ] = '<' .. tostring(email) .. '>'
end
from = '<' .. tostring(settings.from) .. '>'
msgt = {
headers = {
['To'] = to[ 1 ],
['From'] = from,
['Subject'] = subject,
['Content-Type'] = 'text/csv',
['Content-Disposition'] = 'attachment; filename="file.csv"',
['Content-Transfer-Encoding'] = '7BIT',
},
body = ltn12.source.string(csv)
}
settings.source = smtp.message(msgt)
settings.from = from
settings.rcpt = to
res, err = smtp.send(settings)
log(res, err)