Address List Sending Email

Hello Folks

Here is a schedule script i got for one to have mikrotik send your address list to set email
This script worked on my Mikrotik RB3011UIAS-RM running V7.16.2
Reasoning for this was mostly as a form of monitoring - so one can monitor for regular “attacks” even if router rebooted.
As you get the addresses in period of time set on your scheduler.

If anyone got any other recommendations of more refines suggestion I am open to hear.

# Script to export firewall address list, save to file, and email with logging and delays

:local filename "Addresslist.txt"
:local mailto "email@emailaddress.com" ##change this address to yours

# Check email settings
:local emailServer [/tool/e-mail/get server]
:local emailFrom [/tool/e-mail/get from]
:if (($emailServer = "") || ($emailFrom = "")) do={
    :log error "Email settings are not configured. Please set up Tools > Email."
    :error "Email settings are not configured."
}

# Initialize the content with a header
:if ([/file find where name=$filename] != "") do={
    /file remove $filename
    :log info "Removed existing file $filename."
}
:local content "Address List / IP / Comment\r\n"
:log info "Initialized content for $filename."
:delay 2

# Loop through address list entries
:foreach item in=[/ip/firewall/address-list/find] do={
    :local listName [/ip/firewall/address-list/get $item list]
    :local ipAddress [/ip/firewall/address-list/get $item address]
    :local comment [/ip/firewall/address-list/get $item comment]
    
    # Append the data to the content variable
    :set content ($content . "$listName / $ipAddress / $comment\r\n")
    :log info "Added entry: $listName / $ipAddress / $comment."
    :delay 1
}

# Save content to a file
/file print file=$filename
:delay 2
/file set [find name=$filename] contents=$content
:log info "Content saved to $filename."
:delay 2

# Log file creation
:if ([/file find where name=$filename] != "") do={
    :log info "File $filename successfully created."
} else={
    :log error "File $filename could not be created."
    :error "File creation failed."
}

# Verify file existence and send email
:if ([/file find where name=$filename] != "") do={
    /tool/e-mail/send to=$mailto subject="Firewall Address List" body="Attached is the current firewall address list." file=$filename
    :log info "Email with $filename sent to $mailto."
} else={
    :log error "File $filename does not exist. Email not sent."
}
:delay 2

All this:

# Initialize the content with a header
:if ([/file find where name=$filename] != "") do={
    /file remove $filename
    :log info "Removed existing file $filename."
}

simply:

/file remove [find where name=$filename]

Why uselessly do same test again and again?

# Log file creation
:if ([/file find where name=$filename] != "") do={

[...]

# Verify file existence and send email
:if ([/file find where name=$filename] != "") do={



Why overflow logs if the list is huge?

 :log info "Added entry: $listName / $ipAddress / $comment."



Why this

:set content ($content . "$listName / $ipAddress / $comment\r\n")

instead of that, for copy & paste in excel & co.

:set content ($content . "$listName\t$ipAddress\t$comment\r\n")

or this for generate csv file?

:set content ($content . "\"$listName\",\"$ipAddress\",\"$comment\"\r\n")