Logic Machine Forum
Send email with objects file from LM - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Send email with objects file from LM (/showthread.php?tid=1421)



Send email with objects file from LM - JoseJimenez94 - 31.05.2018

Hi,
i have been seeing the next link that explain how to send a email when a KNX object change its value : http://openrb.com/example-sending-email-from-lm2/#codesyntax_1

But i have to do a kind of historical to save the value of certain objects every hour, not just one. 
So i saw this other link: http://openrb.com/example-export-last-hour-csv-object-log-file-to-external-ftp-server-from-lm2/

The problem here is that i dont need to export CSV file to another FTP Server, i have to generate a email with this file.
Is it possible to do this?
Thanks


RE: Send email with objects file from LM - admin - 31.05.2018

See this thread: https://forum.logicmachine.net/showthread.php?tid=394

Code:
if #buffer > 1 then
  data = table.concat(buffer, '\r\n')
  
  -- send file as report.csv with text/csv mime type
  res, err = mailattach('someone@example.com', 'Report for 2016', 'CSV file attached', 'report.csv', data, 'text/csv')
  log(res, err)
end



RE: Send email with objects file from LM - JoseJimenez94 - 06.06.2018

Thanks, but i have not succeeded. So i have started with something simpler to see my error.

I'm trying to send email on KNX group-adress change using this link: http://openrb.com/example-sending-email-from-lm2/
but i don't receive anything. My version is 2.1.1 and i'm using a gmail account with permissions activated.
What's going on??


RE: Send email with objects file from LM - admin - 06.06.2018

Log what mail function returns. Also, you should not use mail function from that example, it's outdated.
Code:
res, err = mail('test@test.com', 'Alert', 'KNX object 1/2/2 value is: ' .. tostring(value))
log(res, err)



RE: Send email with objects file from LM - JoseJimenez94 - 06.06.2018

(06.06.2018, 09:35)admin Wrote: Log what mail function returns. Also, you should not use mail function from that example, it's outdated.
Code:
res, err = mail('test@test.com', 'Alert', 'KNX object 1/2/2 value is: ' .. tostring(value))
log(res, err)
Mail function returns this:
* arg: 1
 * nil
* arg: 2
 * string: Try again



RE: Send email with objects file from LM - Tokatubs - 15.06.2018

(06.06.2018, 09:35)admin Wrote: Log what mail function returns. Also, you should not use mail function from that example, it's outdated.
Code:
res, err = mail('test@test.com', 'Alert', 'KNX object 1/2/2 value is: ' .. tostring(value))
log(res, err)

You write the example is outdated. Which is the newest, i am having some problem sending emails. Could be my settings are incorrect.. I have tried using my office 365 account, when the new 2 step verification to gmail stops me from completing the allow third party apps..


RE: Send email with objects file from LM - admin - 15.06.2018

For gmail with 2-step verification you can generate a separate password for sending e-mails from LM: https://security.google.com/settings/security/apppasswords

Latest mail function:
Code:
-- send an e-mail
function mail(to, subject, message)
  -- make sure these settings are correct
  local settings = {
    -- "from" field, only e-mail must be specified here
    from = 'example@gmail.com',
    -- smtp username
    user = 'example@gmail.com',
    -- smtp password
    password = 'mypassword',
    -- smtp server
    server = 'smtp.gmail.com',
    -- smtp server port
    port = 465,
    -- enable tls, required for gmail smtp
    secure = 'tlsv1_2',
  }

  local smtp = require('socket.smtp')

  if type(to) ~= 'table' then
    to = { to }
  end

  for index, email in ipairs(to) do
    to[ index ] = '<' .. tostring(email) .. '>'
  end

  -- fixup from field
  local from = '<' .. tostring(settings.from) .. '>'

  -- message headers and body
  settings.source = smtp.message({
    headers = {
      to = table.concat(to, ', '),
      subject = subject,
      ['From'] = from,
      ['Content-type'] = 'text/html; charset=utf-8',
    },
    body = message
  })

  settings.from = from
  settings.rcpt = to

  return smtp.send(settings)
end



RE: Send email with objects file from LM - Tokatubs - 15.06.2018

I tried using this:

mail('test@test.com', 'Alert', 'KNX object 1/2/2 value is: ' .. tostring(value))

Then i received this by email:
KNX object 2/4/2 value is: nil so i need to put something more in the script i guess.

But i found and other post by Daniel. So i downloaded the user.FB_Email for the FBEditor. Installed it. Put the script you posted over here in the common functions and tried out the FBEditor. This only returns:

FB Resident (sleep 60) 15.06.2018 14:04:21
* arg: 1
* nil
* arg: 2
* string: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials y13-v6sm1476447lfk.37 - gsmtp

I only wonder why one script can delivere the email and the other one cant. When i thought they both relied one the common Function to work.

Total newbie to this with scripting.


RE: Send email with objects file from LM - admin - 15.06.2018

FB does not use mail from common functions, it comes as a separate user library


RE: Send email with objects file from LM - Daniel - 15.06.2018

Exactly, you have to edit the FB_Email library and put your email setting there.


RE: Send email with objects file from LM - Tokatubs - 15.06.2018

(15.06.2018, 12:30)Daniel. Wrote: Exactly, you have to edit the FB_Email library and put your email setting there.

I found it. And now its working. Thanks for help.


RE: Send email with objects file from LM - Geert - 15.03.2020

Hi,

Is it possible to add in name of CSV-file the date in yymmdd -format?


RE: Send email with objects file from LM - Daniel - 16.03.2020

os.date("%F",d) this will give you date in such format 2020-03-06

PS. Please create new thread when you can't find relevant to your question.