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.

script Lua, wiser for KNX
#1
hello,
i am woking on a project where there is a wiser for KNX, and i received a new requirement which is send mail currenly, i have the mailbox, the password and the smtp server, i would like to have a function in Lua which allows the sending of emails
please i really don't know where to start
Reply
#2
Just fill in the ready function in common functions and then use in script ready email function from helpers.
------------------------------
Ctrl+F5
Reply
#3
(01.09.2023, 14:22)Daniel Wrote: Just fill in the ready function in common functions and then use in script ready email function from helpers.

I did exactly as you said, and I entered the mailbox settings. But when I run the script I don't receive any mail.
I don't know if there is still a setting to be made on the wiser so that it can send emails, or if it is the Mailbox in question that has a problem
Reply
#4
Log what the server reply is:
Code:
res, err = mail('user@example.com', 'test subject', ' test message')
log(res, err)
Reply
#5
(12.09.2023, 14:12)admin Wrote: Log what the server reply is:
Code:
res, err = mail('user@example.com', 'test subject', ' test message')
* 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 pw16-20020a17090720b000b0099ca4f61a8bsm6958955ejb.92 - gsmtp

this is the reponse
Does this mean my username and password are not correct? but it's the password for the mailbox, I don't understand!
Reply
#6
Use a different email provider. Gmail does not allow SMTP without user interaction (OAuth) so it cannot be used in scripts.
Reply
#7
(12.09.2023, 15:43)admin Wrote: Use a different email provider. Gmail does not allow SMTP without user interaction (OAuth) so it cannot be used in scripts.

Gmail is not my email provider,
I changed the port and the answer is this:

* arg: 1
  * nil
* arg: 2
  * string: closed

it is good?
Reply
#8
If you are not using Gmail then you also need to change the server = '...' parameter in mail() in Common functions. The standard SMTP ports are 465 or 587. For 587 you need to change secure parameter to starttls

When the email is sent successfully then the first logged argument won't be nil.
Reply
#9
(13.09.2023, 07:49)admin Wrote: If you are not using Gmail then you also need to change the server = '...' parameter in mail() in Common functions. The standard SMTP ports are 465 or 587. For 587 you need to change secure parameter to starttls

When the email is sent successfully then the first logged argument won't be nil.

hello, I still haven't been able to solve my Mail problem, I tried with 2 different mailboxes I don't understand, here is the result of the function that I logged
test de création de fichier csv 22.09.2023 12:08:36
* 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 n6-20020a05600c294600b0040472ad9a3dsm4302532wmd.14 - gsmtp
Reply
#10
You can't use Gmail. It requires user interaction for sending emails (OAuth) so it can't be used for automated sending.
Reply
#11
(22.09.2023, 12:40)emmanuel.dassi Wrote:
(13.09.2023, 07:49)admin Wrote: If you are not using Gmail then you also need to change the server = '...' parameter in mail() in Common functions. The standard SMTP ports are 465 or 587. For 587 you need to change secure parameter to starttls

When the email is sent successfully then the first logged argument won't be nil.

hello, I still haven't been able to solve my Mail problem, I tried with 2 different mailboxes I don't understand, here is the result of the function that I logged
test de création de fichier csv 22.09.2023 12:08:36
* 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 n6-20020a05600c294600b0040472ad9a3dsm4302532wmd.14 - gsmtp

I remind you again that my email provider is not Google, here is the script that I took entirely from wiser without modifying anything
-- 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 = '*****@elecit.net',
    -- smtp username
    user = '*****@elecit.net',
    -- smtp password
    password = '*******',
    -- smtp server
    server = 'smtp.1und1.de',
    -- 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
#12
Check that you don't have duplicate function somewhere else. Your script error message clearly says that you still have Gmail SMTP server set.
Reply
#13
(22.09.2023, 12:46)admin Wrote: Check that you don't have duplicate function somewhere else. Your script error message clearly says that you still have Gmail SMTP server set.

Hello, I would like to thank you, my wiser sends emails correctly, you were right there were two functions with the same name. thanks again
Reply


Forum Jump: