Email on vpn connection

the client wants an automated email when I log into their network on the VPN. How do I configure Mikrotik to send an email when a VPN connection is established or lost?

I can send emails on vpn connect but it sends like 20 emails as each line of the log is it’s own email.

I also found a link that talks about having a script on the MT that parses the logs at regular intervals.

Both methods are somewhat clunky. Is there a better way?

just make a script with a name VPN_Login_Email

:local interface $interface
:local user [/ppp active get [find where interface="$interface"] name]
:local ip [/ppp active get [find where interface="$interface"] address]
:local date [/system clock get date]
:local time [/system clock get time]
:local msg "VPN Login Alert:\nUser: $user\nIP: $ip\nDate: $date\nTime: $time"

/tool e-mail send to="your_mail@gmail.com" subject="VPN Login Alert" body="$msg"

Then go to PPP → Profiles → Find Your Vpn Profile and Open It
In the Filed: On Up write the below script

/system script run VPN_Login_Email