Logic Machine Forum
how to send an e-mail - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: how to send an e-mail (/showthread.php?tid=2353)



how to send an e-mail - josemabera - 16.11.2019

Good morning:
 I can't send the emails from Logic Machine


  In Scriptin, common funtions I have configured this way:

-- 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 = 'logicmahine@gmail.com',

    -- smtp username

    user = 'logicmahine@gmail.com',

    -- smtp password

    password = 'logicmachine1234',

    -- 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


 
 Then I created an event through a virtual object called Test

value = event.getvalue()


if value == true then

  mail('myemail@gmail.com','Alarma Prueba','Alarma Prueba')

  else

  mail('myemail@gmail.com','No Alarma Prueba','Alarma Prueba Desactiva')
  end


But when the value of the event is "1" I don't receive any email
 
 I have verified that the mail I created for LM is operational

Can you help me someone please?


 BR 
 
 Josema



RE: how to send an e-mail - Erwin van der Zwart - 16.11.2019

Hi,

Did you set the default GW and DNS correctly in the network settings?

Otherwise log the err in the mail function, this will give you some direction what goes wrong..

BR,

Erwin


RE: how to send an e-mail - Tokatubs - 16.11.2019

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
This works great for me, with gmail. Could the error be the secure line i see that you use ssl


RE: how to send an e-mail - josemabera - 16.11.2019

When I send a "true" through the virtual object, the following message appears in Error log:
User script:5: attempt to call global 'Log' (a nil value)
stack traceback:
User script:5: in main chunk

I don't know what it means

(16.11.2019, 11:54)Erwin van der Zwart Wrote: Hi,

Did you set the default GW and DNS correctly in the network settings?

Otherwise log the err in the mail function, this will give you some direction what goes wrong..

BR,

Erwin
Thanks for your response Erwin , but what is the GW? (Excuse my ignorance), in the LM in System Confyg, network I don't see where to configure the DNS ..

BR , 
 
Josema


RE: how to send an e-mail - Erwin van der Zwart - 16.11.2019

Hi,

Under System -> Network -> Interfaces you can double click on the NIC eth0 and there you can set the DNS and default GW (Gateway). When this is not set your controller can’t communicate with the internet and makes it impossible to send mail to Gmail (:

BR,

Erwin


RE: how to send an e-mail - josemabera - 17.11.2019

(16.11.2019, 22:01)Erwin van der Zwart Wrote: Hi,

Under System -> Network -> Interfaces you can double click on the NIC eth0 and there you can set the DNS and default GW (Gateway). When this is not set your controller can’t communicate with the internet and makes it impossible to send mail to Gmail (:

BR,

Erwin
Hi Erwin
If  is configured
 DNS1= 8.8.8.8
 DNS2 = 8.8.4.4
and the GW also is configured
Even so it doesn't work, I can't attach Image captures to show you


(16.11.2019, 20:32)Tokatubs Wrote:
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
This works great for me, with gmail. Could the error be the secure line i see that you use ssl
Hi Tokatubs
I don't understand exactly what you tell me about the ssl Secure Line, how can I modify it to test what you tell me?
BR
Josema


RE: how to send an e-mail - Tokatubs - 17.11.2019

If your problem is sending email with gmail, i think that you are using an old script.
Take a look at line 16 it says tlsv1_2 and not Ssl as is in your script on the top.

I had some problem some time ago, and Admin dropped the script i linked, and then i got the Gmail working. Could be worth a try..


RE: how to send an e-mail - josemabera - 18.11.2019

(17.11.2019, 22:10)Tokatubs Wrote: If your problem is sending email with gmail, i think that you are using an old script.
Take a look at line 16 it says tlsv1_2 and not Ssl as is in your script on the top.

I had some problem some time ago, and Admin dropped the script i linked, and then i got the Gmail working. Could be worth a try..
Thank you very much for your response: I have changed line 16 from ssl to tlsv1_2 but I still do not receive the LM e-mails, I have tested it in the 3 LMs that I have installed in different projects, but I do not receive the e-mails, I miss it is that in one of the projects I did receive them with the common function of sending an e-mail with ssl, but now I do not receive them
any other suggestions?



RE: how to send an e-mail - admin - 18.11.2019

log the result of mail function:
Code:
res, err = mail(...)
log(res, err)



RE: how to send an e-mail - josemabera - 18.11.2019

(18.11.2019, 07:31)admin Wrote: log the result of mail function:
Code:
res, err = mail(...)
log(res, err)
Good morning Admin, thank you very much for the answer, this is the answer to the log:
* 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 j3sm21727187wrs.70 - gsmtp
 As I have a moment I will check the username and password again, but have I done it before????

 BR,
 Josema





RE: how to send an e-mail - Daniel - 18.11.2019

It is probably this
https://devanswers.co/allow-less-secure-apps-access-gmail-account/


RE: how to send an e-mail - josemabera - 19.11.2019

(18.11.2019, 09:16)Daniel. Wrote: It is probably this
https://devanswers.co/allow-less-secure-apps-access-gmail-account/
Good morning Daniel
 Thank you very much for the information, I have only allowed access to unsafe apps in the Gmail account and I already receive the test emails, in two of the three LMs that I have installed the third party does not send them to me, I will check the account carefully something must not be right

BR,

Josema

(17.11.2019, 22:10)Tokatubs Wrote: If your problem is sending email with gmail, i think that you are using an old script.
Take a look at line 16 it says tlsv1_2 and not Ssl as is in your script on the top.

I had some problem some time ago, and Admin dropped the script i linked, and then i got the Gmail working. Could be worth a try..
Hi Tokabus
after several tests
 the script works with ssl, the problem was with the gmail account that I solved by enabling access to unsafe Apps on the account
Thank you very much for your help

 BR,
 Josema



RE: how to send an e-mail - AlexD - 06.09.2022

Hello, 

I trying to use the same script as above, but when I enter my new google account made for testing with LM, and enter "less secure app access" https://devanswers.co/allow-less-secure-apps-access-gmail-account/ it says that "this setting is not supported by google anymore". 

Does that mean that I cant use a @gmail to send e-mails from LM anymore?


RE: how to send an e-mail - Daniel - 06.09.2022

https://forum.logicmachine.net/showthread.php?tid=4076&pid=26361#pid26361


RE: how to send an e-mail - AlexD - 06.09.2022

(06.09.2022, 11:07)Daniel Wrote: https://forum.logicmachine.net/showthread.php?tid=4076&pid=26361#pid26361

Thank you!