Skip to content

Lab 09-01 · E-mail from the router

MTCNA

Module 9 · RouterOS tools. Official course slides 349–351: Tools → Email, the script that exports the configuration and e-mails it, and the LAB "configure your SMTP server, export your configuration, send it to your e-mail from RouterOS" (marked Optional in the course).

Your phone tells you when a parcel is out for delivery. Your bank sends you an e-mail when someone uses your card. Machines that work alone, day and night, need a way to tell a human that something happened.

A router is one of those machines. It sits in a cupboard, nobody looks at it, and one day the configuration you spent a whole afternoon on is gone. Wouldn't it be nice if the router had mailed you a copy of its configuration every night? In this lab you teach r1 to send e-mails, and then to send its own configuration as an attachment.

What you will learn

  • Explain what an SMTP server is and why the router needs one to send mail
  • Configure Tools → Email with server, port and sender address
  • Send a test e-mail from the CLI and find it in a real inbox
  • Export the configuration to a file and attach it to an e-mail
  • Turn the whole thing into a script you can run with one command (or schedule)
  • Avoid the trap of thinking "e-mail sent" in the log means "e-mail received"

How e-mail travels: the post office

An SMTP server (Simple Mail Transfer Protocol) is the computer that accepts e-mails from senders and delivers them towards the recipient's mailbox.

The analogy for this whole lab: e-mail is the post office. The router writes a letter (the e-mail), but it cannot walk to your house. It drops the letter at the post office (the SMTP server), and the post office puts it in your mailbox (the inbox you read).

flowchart LR
    R1["<b>r1</b><br>writes the letter<br>/tool e-mail send"]
    SMTP["<b>SMTP server</b><br>the post office<br>mailpit · port 1025"]
    BOX["<b>Inbox</b><br>your mailbox<br>web on port 8025"]
    YOU["<b>You</b><br>read it"]
    R1 -->|"SMTP"| SMTP
    SMTP -->|"stores"| BOX
    BOX -->|"web / API"| YOU

So the router needs to know three things, exactly what you write on the envelope at a post office counter:

Setting Post office meaning In this lab
server Which post office 192.168.88.25
port Which counter window 1025 (real servers: 25, 587 or 465)
from Your return address r1@mtcna.lab

What is mailpit?

Mailpit is a "fake" mail server made for testing: it accepts every e-mail and shows it on a web page, but never delivers anything to the real Internet. Perfect for a classroom: you can mail as much as you want and nobody gets spam. Its SMTP counter is port 1025 and its web inbox is on port 8025.

Real mail servers want a password and encryption

Gmail, Outlook and your school's server will not accept mail from strangers. With them you also set user, password and tls (encryption; usually tls=starttls with port 587). Gmail even requires a special app password, not your normal one. Mailpit accepts everything, so in the lab you can skip that.

The scenario

flowchart LR
    INET["<b>internet</b><br>198.51.100.10"]
    CAP["<b>class-ap</b><br>10.0.0.1 · 198.51.100.1"]
    subgraph R1["r1 · your router"]
        WAN["ether2 (WAN)<br>10.0.0.x by DHCP"]
        BR["<b>bridge1</b><br>192.168.88.1/24"]
    end
    PC1["<b>pc1</b><br>your laptop<br>DHCP 192.168.88.x"]
    MAIL["<b>mailpit</b><br>the post office<br>192.168.88.25"]
    INET ---|"198.51.100.0/24"| CAP
    CAP ---|"10.0.0.0/24"| WAN
    WAN --- BR
    BR ---|"ether3"| PC1
    BR ---|"ether4"| MAIL
Node Role Address
internet "The Internet" (web server) 198.51.100.10
class-ap Class gateway: DHCP, DNS, NAT 10.0.0.1/24
r1 Your router, working, nothing about e-mail configured WAN by DHCP, LAN 192.168.88.1/24
pc1 Your laptop DHCP from r1
mailpit Office mail server (SMTP 1025, web inbox 8025) 192.168.88.25

Why ether2 and not ether1?

On a real hAP the Internet port is ether1. In the virtual lab ether1 is reserved for the lab itself (containerlab uses it to manage the router), so the Internet port is ether2 and the LAN ports start at ether3. Never touch ether1 in these labs.

Start the lab

Download the lab files and unzip them: 09-01-email.zip. Open a terminal inside the 09-01-email folder.

sudo containerlab deploy -t topology.clab.yml

Give RouterOS about two minutes to boot. Then open two terminals:

ssh admin@clab-mtcna-09-01-r1                 # password: admin
docker exec -it clab-mtcna-09-01-pc1 bash

At any moment, CLAB_EXEC="docker exec" ./check.sh tells you what is already solved.

Step 1 · Look inside the empty mailbox

Before sending anything, check that the post office is open and the mailbox is empty. Mailpit has an API (a web address that answers with data instead of a pretty page), so you can read the inbox with curl.

On pc1:

curl -s http://192.168.88.25:8025/api/v1/messages

Expected: {"total":0,"unread":0,...,"messages":[]}. Zero messages.

On r1, check that the router can reach the mail server at all:

/ping 192.168.88.25 count=3

Check yourself

  1. Which two port numbers does mailpit use, and what is each one for?

Done when the inbox is empty and r1 pings 192.168.88.25.

Step 2 · Tell the router where the post office is

WinBox: Tools → Email. Server 192.168.88.25, Port 1025, TLS no, From r1@mtcna.lab. Apply.

/tool e-mail set server=192.168.88.25 port=1025 from="r1@mtcna.lab" tls=no
/tool e-mail print

Expected output (part):

    server: 192.168.88.25
      port: 1025
       tls: no
      from: r1@mtcna.lab

Older names

In RouterOS v6 (and in many old forum posts) the server field was called address. RouterOS 7.23.8 calls it server. If a command from the Internet says address=, translate it.

Check yourself

  1. Why does the router need a from address at all, if nobody is going to reply?

Done when /tool e-mail print shows the server and port above.

Step 3 · Send a test e-mail

Always test the simplest thing first: a short e-mail with no attachment.

/tool e-mail send to="you@mtcna.lab" subject="Hello from r1" body="My first e-mail sent by a router."
/log print where topics~"e-mail"

Expected in the log: e-mail,info sent <Hello from r1> to: you@mtcna.lab.

Now the important part: check the mailbox, not only the log. On pc1:

curl -s http://192.168.88.25:8025/api/v1/messages | python3 -m json.tool | grep -E '"Subject"|"Address"'

You should see the subject Hello from r1, from r1@mtcna.lab to you@mtcna.lab.

\"Sent\" is not \"received\"

The log line only means the post office accepted the letter. A real server could still throw it in the spam folder or reject it later. The only proof is the e-mail in the inbox. That is why check.sh reads mailpit's inbox, not the router's log.

Check yourself

  1. Write the exact log line you got. Which topic does RouterOS use for e-mail messages?

Done when the test e-mail is in mailpit's inbox.

Step 4 · Export the configuration and mail it

An export is the configuration written as RouterOS commands in a text file (.rsc). If the router dies, you paste that file into a new one and it is back. That is exactly what you want to keep somewhere safe.

WinBox: New Terminal, or Files to see the result.

/export file=export
/file print where name~"export"

Expected: a file export.rsc of about 1 KiB. Now send it as an attachment, with the slides' subject and body:

/tool e-mail send to="you@mtcna.lab" subject="$[/system identity get name] export" body="$[/system clock get date] configuration file" file=export.rsc

The $[ ... ] parts are small commands that RouterOS runs before sending: $[/system identity get name] becomes r1, and $[/system clock get date] becomes today's date. So the subject arrives as r1 export.

The date looks different from the slides

RouterOS 7.10 and later write dates as 2026-10-23 (the international ISO 8601 format). The v6 slides show oct/23/2026. Same date, new format.

Check yourself

  1. Open export.rsc in WinBox (Files, double-click) or with /file print detail where name=export.rsc. Find your LAN address in it. Is the admin password inside? Why is that a good thing?

Done when mailpit shows an e-mail r1 export with the attachment export.rsc.

Step 5 · Make it one command: a script

Typing two long commands every night is not going to happen. A script is a saved list of commands with a name. Run the name, and the router does all the steps.

WinBox: System → Scripts → +. Name email-export, paste the source below into Source. OK.

/system script add name=email-export comment="export the config and e-mail it" source={/export file=export; :delay 2s; /tool e-mail send to="you@mtcna.lab" subject="$[/system identity get name] export" body="$[/system clock get date] configuration file" file=export.rsc; :log info "email-export: sent"}
/system script run email-export
/log print where message~"email-export"
  • The { ... } braces keep the source as text: the $[ ... ] parts are evaluated each time the script runs, not when you create it.
  • :delay 2s gives the export time to finish writing the file before it is attached.
  • :log info leaves a trace in the log, so you can see that the script ran.

Every night at 03:00

The scheduler runs scripts at a time or interval. In a real network you would add: /system scheduler add name=nightly-export start-time=03:00:00 interval=1d on-event=email-export. You do not need it today (the check runs the script itself), but now you know the missing piece.

Check yourself

  1. What would go wrong if the source were written with quotes "..." instead of braces, without escaping the $?
  2. In the slides the e-mail goes to a Gmail address. Which three extra settings would you need for that?

Done when /system script run email-export makes a new r1 export e-mail appear in mailpit. Run CLAB_EXEC="docker exec" ./check.sh: it must end with RESULT: lab solved.

If something goes wrong

Symptom Likely cause Fix
failure: error connecting to server, log e-mail,error Error sending e-mail Wrong server or port (25 instead of 1025) /tool e-mail set server=192.168.88.25 port=1025
Log says sent, but the inbox is empty You are reading a different inbox, or the API call failed Run the curl on pc1; check the address 192.168.88.25:8025
E-mail arrives without attachment The file name is wrong (export instead of export.rsc) /file print and use the exact name
$[/system identity get name] appears literally in the subject The command was pasted inside single quotes or escaped Type it exactly as shown, with double quotes
Script shows flag I (invalid) A typo inside the braces /system script edit email-export source and fix it
curl to 8025 hangs Very rare: the LAN link MTU The lab sets MTU 1500 on the PCs; redeploy with --reconfigure

Clean up

sudo containerlab destroy -t topology.clab.yml --cleanup

Key takeaways

  • A router sends e-mail through an SMTP server, the post office: you configure server, port and from in Tools → Email.
  • /tool e-mail send can attach a file, for example the export of the configuration.
  • $[ ... ] inserts the result of a command (identity, date) into the text when it runs.
  • A script turns several commands into one; the scheduler can run it every night.
  • The log line sent means the server accepted the e-mail; only the inbox proves it arrived.

Quick check

1. What does the router need to know to send an e-mail?
  • The address and port of an SMTP server, and a sender address
  • The recipient's password
  • The IP address of the recipient's computer
  • A DNS server that ends in .mail

E-mail never goes straight to the recipient's computer. The router hands it to an SMTP server (the post office), so it needs that server's address, its port, and a from address for the envelope.

2. Which RouterOS menu holds the SMTP settings?
  • Tools → Email
  • IP → Services
  • System → Logging
  • IP → DNS

The SMTP settings live in Tools → Email (/tool e-mail). IP → Services lists the router's own servers (SSH, WinBox, www...), not the servers it talks to.

3. The log says e-mail,info sent <r1 export>. What does that prove?
  • The SMTP server accepted the e-mail
  • The recipient has read the e-mail
  • The e-mail is in the recipient's inbox, not in spam
  • The attachment was opened correctly

The router only knows that the post office accepted the letter. Delivery, spam filters and reading happen later, out of its sight. The real proof is the inbox.

4. Why is /export a good thing to e-mail from a router?
  • It is the whole configuration as text commands, enough to rebuild the router
  • It is a full disk image including RouterOS itself
  • It contains the admin password so you never forget it
  • It speeds up the router after a reboot

An export (.rsc) is the configuration written as commands. Paste it into a new router and you are back. By default it does not include passwords, which is good for a file that travels by e-mail.

5. In the script, why is the source written inside braces { ... }?
  • So that $[ ... ] is evaluated every time the script runs, not once when it is created
  • Because RouterOS scripts must always start with a brace
  • To make the script run faster
  • To hide the source from other users

Inside braces the source is stored as text. The identity and the date are calculated at run time, so each nightly e-mail carries that night's date.

6. Your school's mail server accepts mail on port 587 with encryption and a login. Which settings must you add compared with this lab?
  • tls=starttls, user and password
  • Only a different from address
  • A static DNS entry for the recipient
  • A firewall rule that opens port 587 on the router's input chain

Real servers want encryption (tls=starttls on port 587) and authentication (user, password). The router is the client here, so it does not need any port opened on its own input chain.

Next: instead of you asking the router, the router watches other machines and reacts by itself: Netwatch.