Open Scripting tab, click Common functions button. Editor will open and there will be mail() function defined there. Edit parameters like username and password in function body. Save and then you can use mail() in other scripts.
I have the scripts below. Please tell me what to do.
Email function in user libraries
function Email(to, subject, message)
-- make sure these settings are correct
---------------BEGINNING OF USER EDITABLE AREA-------------------
local settings = {
-- "from" field, only e-mail must be specified here
from = 'christ.spagakas@gmail.com',
-- smtp username
user = 'christ.spagakas@gmail.com',
-- smtp password
password = '..............',
-- smtp server
server = 'smtp.gmail.com',
-- smtp server port
port = 465,
-- enable ssl, required for gmail smtp
secure = 'tlsv1_2',
}
-------------------END OF USER EDITABLE AREA---------------------
local smtp = require('socket.smtp')
local escape = function(v)
return '<' .. tostring(v) .. '>'
end
-- message headers and body
settings.source = smtp.message({
headers = {
to = escape(to),
subject = subject,
},
body = message
})
-- fixup from field
settings.from = escape(settings.from)
settings.rcpt = { escape(to) }
return smtp.send(settings)
end
This in common functions
-- user function library
-- 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 = 'christ.spagakas@gmail.com',
-- smtp username
-- user = 'christ.spagakas@gmail.com',
-- smtp password
-- password = '.....................',
-- smtp server
-- server = 'smtp.gmail.com',
-- smtp server port
-- port = 465,
-- enable ssl, required for gmail smtp
-- secure = 'sslv23',
--}
25.01.2020, 17:43 (This post was last modified: 25.01.2020, 17:44 by Erwin van der Zwart.)
Hi,
You can call any function from common functions without loading the lib, so mail() should work when adding correct password and username. You probably need to enable “less secure apps” in your gmail account.
When calling a function from a user lib you need to load it first by require("user.yourlibname").
Right now you are calling the function ”email” but without loading it with require() and your function is Email() with a capital E and you are calling email() with a lower case e, script is case sensitive so this is also a reason why it does not work.
Why are you using the user lib for Email in the first place? I would use / adjust the one in common functions..
SMTP server is not gmail and it works fine in other system. But I am trying to send mail to a gmail receiver. Here below my script. I cannot find out a solution. Thanks.
-- 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 = 'xxxxx',
-- smtp username
user = 'xxxx',
-- smtp password
password = 'xxxx',
-- smtp server
server = 'xxx',
-- smtp server port
port = 25,
-- enable ssl, 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
-- 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
res, err = mail(...)
log(res, err)--------
(07.06.2020, 07:26)admin Wrote: Maybe the mail server had blackisted the external IP of the network where LM resides. Have you tried sending emails from the same network?
Lm is not in my same network but it is connect in VPN. I am testing mail and sender and receiver are the same. I try to test in a different way.
Strange thing is that in the meantine I am testing the mail service on with another device in Lua (MPM by Schneider) and there is not any problem and it is directy connected to my network.
X.1.1 Bad destination mailbox address
The mailbox specified in the address does not exist. For Internet mail names, this means the address portion to the left of the "@" sign is invalid. This code is only useful for permanent failures.
Check that "to" parameter is correct in your scripts.
Obviously function works fine. Further to many test it seems that the problem occurs when system sends mail to those providers which checks the
compliance RFC. Google, microsoft outlook 365 and Zimbra. When I send mail to those destination receiver does not receive the mail.
My smtp server gives me this error:
550-5.7.1 not RFC 5322 compliant:
550-5.7.1 'From' header is missing.
550-5.7.1 To reduce the amount of spam sent to Gmail, this message has been blocked. Please visit .....