Logic Machine Forum
Formatting email text - 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: Formatting email text (/showthread.php?tid=4274)



Formatting email text - fleeceable - 30.09.2022

I have little problem with my code.

I want to archive result, where email what LM send is formatted. 
There is list of alarms. If alarm is okay, after variable name I want to show green bold "OK". If there is alarm, I want to show red bold "ALARM".

Basically I have working solution but sometimes system gonna paint whole text to green.

 This script is event based script (alarms count - if any alarms found, this script variable will be over 1)
Code:
value = event.getvalue()
require('user.sendmail')
require('user.emails')

content_alarm = 'Tere! <br><br> <b>SV1</b> automaatikasüsteemis on tuvastatud järgmised häired: <br><br>'
content_ok = 'Tere! <br><br> <b>SV1</b> automaatikasüsteemi häired on taastunud ning süsteem on töökorras.<br><br>Kokkuvõte häiretest: <br><br>'

footer = '<br><br><br><br><i>Email on genereeritud automaatikasüsteemi poolt. Palume mitte sellele emailile vastata!<br><br>Parimate soovidega,<br>Building Automation System OÜ</i>'
summary_to_send = ''

function alarm_summary(tag)
  table_len = 0
  summary = ''
  alarms = grp.tag(tag)
  for _ in pairs(alarms) do
    table_len = table_len + 1
  end
  --os.sleep(0.5)
  for i=1,table_len, 1 do
    if alarms[i].value ==true then
        --summary = summary .. alarms[i].name .. " - <span style='color:red;font-weight:bold'>".."ALARM!".."</span><br>"
      summary = string.format("%s%s%s",summary, alarms[i].name, " - <span style='color:red;font-weight:bold'>ALARM!</span><br>")
      --os.sleep(0.5)
    else
      --summary = summary .. alarms[i].name .. " - <span style='color:green;font-weight:bold'>".."OK".."</span><br>"
      summary = string.format("%s%s%s",summary , alarms[i].name , " - <span style='color:green;font-weight:bold'>OK</span><br>")
      --os.sleep(0.5)
    end
  end
  --os.sleep(0.5)
    return summary
end


summary_to_send = alarm_summary('SV1-ALARMS')

--os.sleep(0.5)
if value >0  then
  res, err = mail(emails, 'Hooneautomaatika',string.format("%s%s%s",content_alarm, summary_to_send, footer ))
    if err ~=nil then
    grp.checkupdate('38/4/6',1)
    else
    grp.checkupdate('38/4/6',0) 
  end
else
  res, err =mail(emails, 'Hooneautomaatika',string.format("%s%s%s",content_ok , summary_to_send , footer))
    if err ~=nil then
    grp.checkupdate('38/4/6',1)
    else
    grp.checkupdate('38/4/6',0) 
  end
end



I have tried delays, formatting text before sending etc but haven't find any fix...
When checking with dev tools I see that span is not ended.
But why, that is the question...

Tried to send email to three different email platform no differences (to gmail, to own domain email).

Any suggestions?

With short emails I haven't seen any errors so far (up to 5 variable names).


RE: Formatting email text - admin - 30.09.2022

Probably has something to do how the email text is encoded when sending. Can you provide the message source from Gmail (Show original > Download original)?


RE: Formatting email text - fleeceable - 03.10.2022

(30.09.2022, 09:30)admin Wrote: Probably has something to do how the email text is encoded when sending. Can you provide the message source from Gmail (Show original > Download original)?

It is not possible to DM you. Can you DM me first so I can send additional information?


RE: Formatting email text - admin - 03.10.2022

I've tried to re-create this issue and it seems that the message source won't be helpful here.
Try this reworked script with extra line breaks and see if it helps.
Code:
value = event.getvalue()

require('user.sendmail')
require('user.emails')

header_alarm = 'Tere! <br><br> <b>SV1</b> automaatikasüsteemis on tuvastatud järgmised häired: <br><br>'
header_ok = 'Tere! <br><br> <b>SV1</b> automaatikasüsteemi häired on taastunud ning süsteem on töökorras.<br><br>Kokkuvõte häiretest: <br><br>'

footer = '<br><br><br><br><i>Email on genereeritud automaatikasüsteemi poolt. Palume mitte sellele emailile vastata!<br><br>Parimate soovidega,<br>Building Automation System OÜ</i>'

text_alarm = ' - <span style="color:red;font-weight:bold">ALARM!</span><br>'
text_ok = ' - <span style="color:green;font-weight:bold">OK</span><br>'

buf = {}

buf[ #buf + 1 ] = value > 0 and header_alarm or header_ok

alarms = grp.tag('SV1-ALARMS')

for _, alarm in ipairs(alarms) do
  buf[ #buf + 1 ] = alarm.name
  buf[ #buf + 1 ] = alarm.value and text_alarm or text_ok
end

buf[ #buf + 1 ] = footer

message = table.concat(buf, '\n')

res, err = mail(emails, 'Hooneautomaatika', message)
grp.checkupdate('38/4/6', err ~= nil)