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.

Send email with objects file from LM
#1
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-...desyntax_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-ho...-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
Reply
#2
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
Reply
#3
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??
Reply
#4
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)
Reply
#5
(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
Reply
#6
(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..
Reply
#7
For gmail with 2-step verification you can generate a separate password for sending e-mails from LM: https://security.google.com/settings/sec...ppasswords

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
Reply
#8
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.
Reply
#9
FB does not use mail from common functions, it comes as a separate user library
Reply
#10
Exactly, you have to edit the FB_Email library and put your email setting there.
------------------------------
Ctrl+F5
Reply
#11
(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.
Reply
#12
Hi,

Is it possible to add in name of CSV-file the date in yymmdd -format?
Gr.
Geert
Reply
#13
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.
------------------------------
Ctrl+F5
Reply


Forum Jump: