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.
NB! Starting 2023 Gmail is not allowing to use their service via Less-secure-apps, so you need to use your own SMTP server or 3rd party services like SendGrid.
- -- 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.