Sending email from LM
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.
- 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.
- -- 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 ssl, required for gmail smtp
- secure = 'sslv23',
- }
-
- 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
-
- -- message headers and body
- settings.source = smtp.message({
- headers = {
- to = table.concat(to, ', '),
- subject = subject,
- ['Content-type'] = 'text/html; charset=utf-8',
- },
- body = message
- })
-
- -- fixup from field
- settings.from = '<' .. tostring(settings.from) .. '>'
- settings.rcpt = to
-
- return smtp.send(settings)
- 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: