This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Automatic export all logs and send by mail
#14
Try this:
Code:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
to = 'to@example.com' settings = {   -- "from" field, only e-mail must be specified here   from = 'sender@example.com',   -- smtp username   user = 'sender@example.com',   -- smtp password   password = '12345678',   -- smtp server   server = 'smtp.gmail.com',   -- smtp server port   port = 465,   -- enable ssl, required for gmail smtp   secure = 'sslv23', } subject = 'CSV logs' logtime = os.time() - 60 * 60 -- last hour (3600 seconds) buffer = {'"date","address","name","value","metadata"'} query = [[   SELECT o.id, o.datatype, o.name, ol.datahex, ol.logtime, ol.eventtype, ol.meta   FROM objectlog ol   JOIN objects o ON ol.address=o.id   WHERE ol.logtime >= ?   ORDER BY ol.id DESC ]] for _, row in ipairs(db:getall(query, logtime)) do   if row.datatype and row.eventtype == 'write' then     data = grp.decodevalue(row.datahex, row.datatype)     logdate = os.date('%Y.%m.%d %H:%M:%S', row.logtime)     buffer[ #buffer + 1 ] = string.format('%q,%q,%q,%q,%q',       logdate, knxlib.decodega(row.id), row.name, tostring(data), row.meta or '')   end end csv = table.concat(buffer, '\r\n') smtp = require('socket.smtp') mime = require('mime') ltn12 = require('ltn12') function escape(v)   return '<' .. tostring(v) .. '>' end to = escape(to) msgt = {   headers = {     to = to,     ['content-type'] = 'text/csv',     ['content-disposition'] = 'attachment; filename="logs.csv"',     ['content-transfer-encoding'] = 'BASE64',     subject = subject,   },   body = ltn12.source.chain(     ltn12.source.string(csv),     ltn12.filter.chain(mime.encode('base64'), mime.wrap('base64'))   ) } settings.source = smtp.message(msgt) settings.from = escape(settings.from) settings.rcpt = { to } res, err = smtp.send(settings) log(res, err)
Reply


Messages In This Thread
RE: Automatic export all logs and send by mail - by admin - 02.01.2023, 12:54

Forum Jump: