Filtering
While configuring each service can be sufficient to setup a working vSMTP infrastructure, it is quite limited, especially for email filtering.
Fortunatly, vSMTP enables email filtering using Rhai scripts.
Filtering script
Here is an example of a possible filtering script for the SMTP Receiver service.
fn on_connect(ctx) {
if ctx.client_ip() is "x.x.x.x" {
status::accept()
} else {
status::deny("I won't receive emails from you. Go away.")
}
}
This script is pretty stupid: it simply checks for the ip address of the sender of the email, and if it does not match the one desired, it denies the transaction. This way, only a single machine can send mail to our vSMTP infrastructure. Neat.
The on_connect
function is executed when a client connects to the receiver.
As you may have noticed, the name of the function is based on a specific convention, like the on_config
function, as seen in the previous chapter.
There are many 'callbacks' that you can use to filter your email that are specific to each service. For example, the SMTP Receiver service exposes:
on_connect
: executed when a client connects to the server.on_auth
: executed when the server receives an authentication request.on_rcpt
: executed when the server receives aRCPT TO
command.
etc.
A list of all the functions for each service are described in the [Service reference].
What you can do
The above example is really simple, but filtering allows you to create complexe rules to:
- inspect and modify the content of incoming emails.
- forward and deliver emails locally or remotely.
- connect to databases.
- run shell commands.
- quarantine emails.
and much more.
All filtering functions are defined in the Filtering Reference