Script to send email upon hotspot user login

guys, I need help with this script I found, I’ve copied and pasted it here as I have it on my mikrotik…it logs that the user has logged into the hotspot and that it matches the name, nothing further gets logged and I do not receive an email. Can someone please help and parse through my script and tell me where I’m going wrong? Regular email tests work fine.

This is my hotspot on-login script.

/log info “$user just logged in, triggered On Login script”;
:local emailaddress “myemail@googlemail.com”;
:local timeout 30d;
:local nameFilter “rtl”;
if ([:find “$user” “$nameFilter”] = 0) do={/log info “[HOTSPOT] - $user - logged in, matches name filter”;
:local date [/system clock get date];
:local time [/system clock get time];
:local ip [/ip hotspot active get [find user=“$user”] address];
:local emailsubject;
:local emailbody;
:if ([:len [/ip firewall address-list find list~“^$user - HSLOGIN”]] = 0) do={/log info “[HOTSPOT] - $user - not found in Address List”;
/ip firewall filter add action=add-src-to-address-list address-list=“$user - HSLOGIN,$date,$time” address-list-timeout=$timeout chain=pre-hs-input disabled=no src-address=$ip comment=“$user - HSLOGIN”;
/ip firewall filter add action=add-src-to-address-list address-list=“$user - HSLOGIN,$date,$time” address-list-timeout=$timeout chain=forward disabled=no src-address=$ip comment=“$user - HSLOGIN”;
:local counter 0;
:local limit 60;
:local delaytime 5s;
:while (counter < $limit) do={
:set counter ($counter + 1);
/log info “[HOTSPOT] - $user - checking if user is in Address List - attempt $counter of $limit”;
:delay $delaytime;
:if ([:len [/ip firewall address-list find list~“^$user - HSLOGIN”]] = 1) do={/log info “[HOTSPOT] - $user - user has been added to Address List, sending email”;
:set emailsubject “New Hotspot Login ($user)”;
:set emailbody “User: $user\r\n$time, $date\r\nIP: $ip\r\nExpires in: $timeout”;
:set counter ($limit+10);} else={
:if ($counter = $limit) do={/log info “[HOTSPOT] - $user - failed to add user to Address List, sending email”;
:set emailsubject “New Hotspot Login ERROR ($user)”;
:set emailbody “ERROR: failed to add to Address List, need to investigate.\r\n\r\nUser: $user\r\n$time, $date\r\nIP: $ip\r\n”;
}
}
}
/ip firewall filter remove [find comment=“$user - HSLOGIN”];
/tool e-mail send to=“$emailaddress” subject=“$emailsubject” body=“$emailbody”;
} else={/log info “[HOTSPOT] - $user - already in Address List”;}
} else={/log info “[HOTSPOT] - $user - logged in, does not match name filter”;}

What is the last entry you see in the log?
/log info “$user just logged in, triggered On Login script”;
or
/log info “[HOTSPOT] - $user - checking if user is in Address List - attempt $counter of $limit”;
or…

The last entry I see is
“[HOTSPOT] - $user - logged in, matches name filter”;

When I look at the address lists under the ip/firewall gui I don’t see any address lists…not sure if that is relevant or not…

Then your script is failing somewhere in here. Maybe you should add another log entry between them to check exactly which command in here is failing.

if ([:find "$user" "$nameFilter"] = 0) do={/log info "[HOTSPOT] - $user - logged in, matches name filter";
:local date [/system clock get date];
:local time [/system clock get time];
:local ip [/ip hotspot active get [find user="$user"] address];
:local emailsubject;
:local emailbody;
:if ([:len [/ip firewall address-list find list~"^$user - HSLOGIN"]] = 0) do={/log info "[HOTSPOT] - $user - not found in Address List";

// maybe put another log entry here

/ip firewall filter add action=add-src-to-address-list address-list="$user - HSLOGIN,$date,$time" address-list-timeout=$timeout chain=pre-hs-input disabled=no src-address=$ip comment="$user - HSLOGIN";
/ip firewall filter add action=add-src-to-address-list address-list="$user - HSLOGIN,$date,$time" address-list-timeout=$timeout chain=forward disabled=no src-address=$ip comment="$user - HSLOGIN";
:local counter 0;
:local limit 60;
:local delaytime 5s;
:while (counter < $limit) do={
:set counter ($counter + 1);
/log info "[HOTSPOT] - $user - checking if user is in Address List - attempt $counter of $limit";

Thanks Surfer Tim,

Unfortunately I know little of scripting, and I copy/pasted this script from elsewhere. What exactly should I be logging and hopefully you can give me the command that I should insert?

Your help is very much appreciated!