CSV
Comma-separated values (CSV) is a text file format that uses commas to separate values. This plugin enables your vSMTP service to read and write CSV files.
Usage
In this example, we will setup the plugin to be used in the SMTP SMTP Receiver service. First, create a handle to the desired csv file.
// /etc/vsmtp/smtp-receiver/services/database.rhai
import "plugins/libvsmtp_plugin_csv" as db;
export const user_accounts = db::csv(#{
// The path to the csv database.
connector: "/db/user_accounts.csv",
// The access mode of the database. Can be:
// `O_RDONLY`, `O_WRONLY` or `O_RDWR`.
access: "O_RDONLY",
// The refresh mode of the database.
// Can be "always" (database is always refreshed once queried)
// or "no" (database is readonly and never refreshed).
//
// WARNING: using the "always" option can make vsmtp really slow,
// because it has to pull the whole database in memory every
// time it is queried. Use it only with a small database.
refresh: "always",
// The delimiter character used in the csv file.
delimiter: ",",
});
Then, query the file in your rules. In this example, we use the csv file to store information on the sender of the email.
// /etc/vsmtp/smtp-receiver/rules.rhai
import "services/databases" as db;
fn on_mail_from(ctx) {
ctx.run([
rule "is sender in database ?" || {
// Query the database.
// Let's assume that the `ctx::mail_from()` value is `john.doe@example.com`.
// `db::user_accounts.get` return an array representing the selected row with
// the key passed as parameter.
let user = db::user_accounts.get(ctx.mail_from().local_part);
// The record returned is an array `["john.doe", "john.doe@example.com"]`.
if user != [] {
// We can select columns by index.
log("info", `A trusted client just connected: user=${user[0]}, email=${user[1]}`)
}
status::next()
}
]);
}