Skip to content

Sender Domain Restrictions


Limit outbound mail to configured recipient domains per sender

This plugin restricts users to sending emails only to domains explicitly listed in the configuration file, providing per-sender control over outbound recipient domains.

# plugins/restrictsender.js

exports.register = function () {
  const plugin = this;
  plugin.load_restrictsender_ini();
  plugin.register_hook('rcpt', 'restrict_sender');
}

exports.load_restrictsender_ini = function () {
  const plugin = this;
  plugin.cfg = plugin.config.get('restrictsender.ini', function () {
      plugin.load_restrictsender_ini();
    });
}

exports.restrict_sender = function (next, connection, params) {
    const plugin = this;
    var recip = params[0];
    if (connection.relaying || connection.notes.authentication_results ) {
        var check_domain = connection.transaction.mail_from.host;
        const email = connection.transaction.mail_from.address();
        if (!plugin.cfg[email]) {
           return next();
        }
        var recip_host = recip.host.toLowerCase();
        var a_d = Object.keys(plugin.cfg[email]);
        plugin.loginfo("recipint:" + recip_host);
        if ( a_d.includes(recip_host)) {
             return next();
        }
        return next(DENY,"Sorry, you are only allowed to send mails to particular domains. Please contact system adminstrator if you want to send emails to this domain");
   }
   return next();
}
# config/restrictsender.ini

[user@example.com]
gmail.com