Example: Sending email from LogicMachine

Task

Send email on KNX object 1/2/2 status change with its current value

Email program – Event-based script

Add Event-based script in Scripting tab.

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

Edit ‘mail’ function in Common functions

Change the Email settings to correct ones.

Source code    
  1. -- send an e-mail,
  2. function mail(to, subject, message)
  3. -- make sure these settings are correct
  4. local settings = {
  5. -- "from" field, only e-mail must be specified here
  6. from = 'example@gmail.com',
  7. -- smtp username
  8. user = 'example@gmail.com',
  9. -- smtp password
  10. password = 'mypassword',
  11. -- smtp server
  12. server = 'smtp.gmail.com',
  13. -- smtp server port
  14. port = 465,
  15. -- enable ssl, required for gmail smtp
  16. secure = 'sslv23',
  17. }
  18.  
  19. local smtp = require('socket.smtp')
  20.  
  21. if type(to) ~= 'table' then
  22. to = { to }
  23. end
  24.  
  25. for index, email in ipairs(to) do
  26. to[ index ] = '<' .. tostring(email) .. '>'
  27. end
  28.  
  29. -- message headers and body
  30. settings.source = smtp.message({
  31. headers = {
  32. to = table.concat(to, ', '),
  33. subject = subject,
  34. ['Content-type'] = 'text/html; charset=utf-8',
  35. },
  36. body = message
  37. })
  38.  
  39. -- fixup from field
  40. settings.from = '<' .. tostring(settings.from) .. '>'
  41. settings.rcpt = to
  42.  
  43. return smtp.send(settings)
  44. end

Note! Rcpt and from fields can contain only email address, no name.

Further it’s possible to add additional functionality, like sending email to Logic Machine with specific commands, make white/black lists etc.

 

Gmail security settings

Due to new security settings from Gmail you have to activate a setting in your Gmail account to allow receive such emails. For this, login to Google with your account and go to the following URL and make sure it’s enabled otherwise the controller will be blocked for sending e-mail: