MikroTik Scripts: Sending Notifications and Files to Email (function)

Another option for notification by MikroTik devices is sending email notifications.

Example email with device MikroTik
Example email with device MikroTik

To configure, you need the SMTP parameters of your mail server. If the mail server uses two-factor authentication, use the app key instead of the password (usually listed in the mail service help desk).

? If you have the IP address of the SMTP server and it does not change, specify it in the server parameter. If you have an SMTP server domain name with a dynamic IP, allow MikroTik to resolve the domain name to an IP address using the [: resolve “smtp.mail.com”] construct.

Article in other languages:
?? – Scripts MikroTik: Envío de notificaciones y archivos por correo electrónico
?? – MikroTik Скрипты: Отправить письмо и файл на почту
?? – Scripts MikroTik: envoi de notifications et de fichiers par courrier électronique
?? – MikroTik-Skripte: Senden von benachrichtigungen und daten an E-mails
?? – MikroTik-scripts: e-mail en bestand naar e-mail verzenden (functie)

Send email from MikroTik (simple option)

A script for sending an email and an attached file to the specified mail.

One line script:

/tool e-mail send to=destination_mail@mail.com server=[:resolve "smtp.mail.com"] port=465 start-tls=tls-only user="source mail@mail.com" password="Password"  from="source mail@mail.com" subject="Subject" body="Simple Text" file="file_for_send.txt"

Multi-line script (better readability):

/tool e-mail send to=destination_mail@mail.com \
    server=[:resolve "smtp.mail.com"] port=465 start-tls=tls-only \
    user="source mail@mail.com" password="Password"  from="source mail@mail.com" \
    subject="Subject" body="Simple Text" \
    file="file_for_send.txt"

Send email from MikroTik (function)

If you use sending email notifications from many scripts, I recommend separating the script into the “Send email” function and the “Call the function to send email” script.

? Attention: in RouterOS v7 the syntax of the tls parameter has changed, so two versions of the function are located here.

Function “Send email” for RouterOS v7

Creating script:

[System] -> [Scripts] -> [+] -> [Name: SendEmailFunction] -> [Policy: Don't require permissions]

Script code:

# Variables
:local SendFrom "source_mail@mail.com";
:local PasswordMail "Password";
:local SmtpServer [:resolve "smtp.mail.com"];
:local UserName "source_mail@mail.com";
:local SmtpPort 465;
:local UseTLS "yes";

# Main script code
/tool e-mail send to=$SendTo server=$SmtpServer port=$SmtpPort tls=$UseTLS user=$SendFrom password=$PasswordMail from=$SendFrom subject=$Subject body=$TextMail file=$FileName;

Function “Send email” for RouterOS v6

Creating script:

[System] -> [Scripts] -> [+] -> [Name: SendEmailFunction] -> [Policy: Don't require permissions]

Script code:

# Variables
:local SendFrom "source_mail@mail.com";
:local PasswordMail "Password";
:local SmtpServer [:resolve "smtp.mail.com"];
:local UserName "source_mail@mail.com";
:local SmtpPort 465;
:local UseTLS "tls-only";

# Main script code
/tool e-mail send to=$SendTo server=$SmtpServer port=$SmtpPort start-tls=$UseTLS user=$SendFrom password=$PasswordMail from=$SendFrom subject=$Subject body=$TextMail file=$FileName;
MikroTik Scripts: Sending Notifications and Files to Email

Calling up the “Send mail” function

Function named SendEmailFunction calling from the SendEmailCall script.

Creating a new SendEmailCall script:

[System] -> [Scripts] -> [+] -> [Name: SendEmailCall] -> [Policy: read, write, policy, test]

Specify the parameters to be passed to the main function:

  • SendTo;
  • Subject;
  • MessageText;
  • FileName (optional parameter).

Script code:

# Variables
:local DeviceName [/system identity get name];
:local Time [/system clock get time];
:local Date [/system clock get date];

:local SendTo "destination_mail@mail.com";
:local Subject "\F0\9F\9F\A2 INFO: $DeviceName [$Date $Time]";
:local MessageText "This informational message indicates the successful execution of the script on device $DeviceName";
:local FileName "some_file.txt";

# Main script code
:local SendEmail [:parse [/system script get SendEmailFunction source]];
$SendEmail SendTo=$SendTo TextMail=$MessageText Subject=$Subject FileName=$FileName;
Send email from MikroTik

? How to send an email or file from a MikroTik device was discussed in this article. I hope you can now send notifications from MikroTik devices via email. However, if you run into any problems while setting up, feel free to write in the comments. I will try to help.

✅ The script is checked: hAP ac lite [RouterBOARD 952Ui-5ac2nD], RouterOS 6.47.8 (stable).

1 thought on “MikroTik Scripts: Sending Notifications and Files to Email (function)”

  1. Thank you it works!
    If you need to send via O365 SMTP, you will need to enable SMTP in Admin panel and edit email script:

    # Variables
    :local SendFrom "sender email";
    :local PasswordMail "sender pw";
    :local SmtpServer [:resolve "smtp-mail.outlook.com"];
    :local UserName "receiver email";
    :local SmtpPort 587;
    :local UseTLS "starttls";

    Reply

Leave a Comment