Add block/white list option for email senders
This commit is contained in:
7 files changed
+164
-18
No files matched your search
@@ -74,7 +74,7 @@ Generate a keypair for rspamd with `rspamadm keypair gen` and add it to /etc/rsp
|
||||
|
||||
Set a password for the web interface with `rspamadm pw` and add the hash for it to /etc/rspamd/override.d/worker-controller.inc
|
||||
|
||||
Configure the Rspamd dynamic settings map by replacing `CHANGE_ME_TO_A_LONG_RANDOM_SECRET` with the same value in `/var/www/mail/common_config.php` and `/etc/rspamd/rspamd.conf.override`. Generate a secret with `openssl rand -hex 32`. Also replace the example hostname in the Rspamd override URL with your mail server's public hostname. The endpoint returns the settings map in Rspamd's UCL format and is authenticated because the map contains mailbox addresses and filtering preferences.
|
||||
Configure the Rspamd dynamic settings map by replacing `CHANGE_ME_TO_A_LONG_RANDOM_SECRET` with the same value in `/var/www/mail/common_config.php` and `/etc/rspamd/rspamd.conf.override`. Generate a secret with `openssl rand -hex 32`. Also replace the example hostname in the Rspamd override URL with your mail server's public hostname. The endpoint returns the settings map in Rspamd's UCL format and is authenticated because the map contains mailbox addresses and filtering preferences. Users can configure exact sender addresses to block or whitelist in their account settings; whitelisting uses Rspamd's `want_spam` option and bypasses all Rspamd checks, including virus scanning.
|
||||
|
||||
Generate DKIM signing keys and add them to /etc/rspamd/local.d/arc.conf /etc/rspamd/local.d/dkim_signing.conf, then add the printed DNS records to your domain:
|
||||
```
|
||||
|
||||
+18
-1
@@ -9,7 +9,7 @@ const DBHOST = 'localhost'; // Database host
|
||||
const DBUSER = 'postfix'; // Database user
|
||||
const DBPASS = 'YOUR_PASSWORD'; // Database password
|
||||
const DBNAME = 'postfix'; // Database
|
||||
const DBVERSION = 6; // Database schema version
|
||||
const DBVERSION = 7; // Database schema version
|
||||
const INACTIVITY_PERIOD_MONTHS = [1, 6, 12, 24]; // Allowed inactivity periods in months - don't forget to update get_inactivity_period_options on changes here and if an option is removed, you should also update accounts in the database mailbox table
|
||||
const DEFAULT_INACTIVITY_PERIOD = 12; // Default inactivity period in months
|
||||
const ALLOW_ACCOUNT_NEVER_DELETE = false; // Whether to allow users to set their inactivity period to "never delete"
|
||||
@@ -458,6 +458,23 @@ function check_email_valid( string $email, string &$msg = '' ): bool
|
||||
return true;
|
||||
}
|
||||
|
||||
function parse_sender_address_list( string $value ): ?array
|
||||
{
|
||||
$validator = new EmailValidator();
|
||||
$addresses = [];
|
||||
foreach ( preg_split( '/\R/', $value ) as $address ) {
|
||||
$address = trim( $address );
|
||||
if ( $address === '' ) {
|
||||
continue;
|
||||
}
|
||||
if ( ! $validator->isValid( $address, new NoRFCWarningsValidation() ) ) {
|
||||
return null;
|
||||
}
|
||||
$addresses[] = mb_strtolower( $address );
|
||||
}
|
||||
return array_values( array_unique( $addresses ) );
|
||||
}
|
||||
|
||||
function alt_links(): void
|
||||
{
|
||||
global $language;
|
||||
|
||||
@@ -1061,3 +1061,40 @@ msgstr ""
|
||||
#: www/admin.php:1055
|
||||
msgid "Oops, this mailbox doesn't seem to exist."
|
||||
msgstr ""
|
||||
|
||||
#: www/manage_account.php:149 www/admin.php:351
|
||||
msgid "Please enter a valid email address on each line."
|
||||
msgstr ""
|
||||
|
||||
#: www/manage_account.php:151 www/admin.php:353
|
||||
msgid "An email address cannot be both blocked and whitelisted."
|
||||
msgstr ""
|
||||
|
||||
#: www/manage_account.php:155
|
||||
msgid ""
|
||||
"Successfully updated sender rules. Changes can take up to 10 minutes to take "
|
||||
"effect."
|
||||
msgstr ""
|
||||
|
||||
#: www/manage_account.php:481 www/admin.php:1236
|
||||
msgid "Sender rules"
|
||||
msgstr ""
|
||||
|
||||
#: www/manage_account.php:482 www/admin.php:1237
|
||||
msgid ""
|
||||
"Enter one exact sender email address per line. Blocking a sender rejects "
|
||||
"matching incoming mail. Whitelisting a sender makes Rspamd bypass all checks, "
|
||||
"including virus scanning, for matching mail."
|
||||
msgstr ""
|
||||
|
||||
#: www/manage_account.php:485 www/admin.php:1239
|
||||
msgid "Blocked senders"
|
||||
msgstr ""
|
||||
|
||||
#: www/manage_account.php:489 www/admin.php:1243
|
||||
msgid "Whitelisted senders"
|
||||
msgstr ""
|
||||
|
||||
#: www/manage_account.php:492
|
||||
msgid "Update sender rules"
|
||||
msgstr ""
|
||||
@@ -36,7 +36,7 @@ $createTableStatements = [
|
||||
'dmarc_report_record_reason' => "CREATE TABLE IF NOT EXISTS `dmarc_report_record_reason` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `record_id` bigint(20) unsigned NOT NULL, `type` varchar(255) NOT NULL, `comment` varchar(255) NOT NULL, PRIMARY KEY (`id`), KEY `record_id` (`record_id`), CONSTRAINT `dmarc_report_record_reason_ibfk_1` FOREIGN KEY (`record_id`) REFERENCES `dmarc_report_records` (`id`) ON DELETE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
|
||||
'dmarc_report_record_spf_result' => "CREATE TABLE IF NOT EXISTS `dmarc_report_record_spf_result` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `record_id` bigint(20) unsigned NOT NULL, `domain` varchar(255) NOT NULL, `result` varchar(255) NOT NULL, `scope` varchar(255) NOT NULL, PRIMARY KEY (`id`), KEY `record_id` (`record_id`), CONSTRAINT `dmarc_report_record_spf_result_ibfk_2` FOREIGN KEY (`record_id`) REFERENCES `dmarc_report_records` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;",
|
||||
'domain_admins' => "CREATE TABLE IF NOT EXISTS `domain_admins` (`username` varchar(255) NOT NULL, `domain` varchar(255) NOT NULL, `created` datetime NOT NULL DEFAULT '2000-01-01 00:00:00', `active` tinyint(1) NOT NULL DEFAULT 1, `id` bigint(20) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`), KEY `username` (`username`), KEY `active` (`active`), KEY `domain` (`domain`), CONSTRAINT `domain_admins_ibfk_1` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `domain_admins_ibfk_2` FOREIGN KEY (`username`) REFERENCES `admin` (`username`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
|
||||
'mailbox' => "CREATE TABLE IF NOT EXISTS `mailbox` (`username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL DEFAULT '', `quota` bigint(20) NOT NULL DEFAULT 0, `local_part` varchar(255) NOT NULL DEFAULT '', `domain` varchar(255) NOT NULL DEFAULT '', `created` datetime NOT NULL DEFAULT current_timestamp(), `modified` datetime NOT NULL DEFAULT current_timestamp(), `active` tinyint(1) NOT NULL DEFAULT 1, `password_hash_type` varchar(20) NOT NULL DEFAULT '', `openpgpkey_wkd` char(32) NOT NULL DEFAULT '', `pgp_key` text DEFAULT NULL, `pgp_verified` tinyint(1) NOT NULL DEFAULT 0, `tfa` tinyint(1) NOT NULL DEFAULT 0, `totp_secret` varchar(64) DEFAULT NULL, `last_login` bigint(20) unsigned DEFAULT NULL, `inactivity_period` tinyint unsigned NOT NULL DEFAULT 12, `spam_reject_score` tinyint unsigned NOT NULL DEFAULT 15, `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_out` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`username`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `openpgpkey_wkd` (`openpgpkey_wkd`), KEY `inactivity_period` (`inactivity_period`), KEY `spam_reject_score` (`spam_reject_score`), CONSTRAINT `mailbox_ibfk_2` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
|
||||
'mailbox' => "CREATE TABLE IF NOT EXISTS `mailbox` (`username` varchar(255) NOT NULL, `password` varchar(255) NOT NULL DEFAULT '', `quota` bigint(20) NOT NULL DEFAULT 0, `local_part` varchar(255) NOT NULL DEFAULT '', `domain` varchar(255) NOT NULL DEFAULT '', `created` datetime NOT NULL DEFAULT current_timestamp(), `modified` datetime NOT NULL DEFAULT current_timestamp(), `active` tinyint(1) NOT NULL DEFAULT 1, `password_hash_type` varchar(20) NOT NULL DEFAULT '', `openpgpkey_wkd` char(32) NOT NULL DEFAULT '', `pgp_key` text DEFAULT NULL, `pgp_verified` tinyint(1) NOT NULL DEFAULT 0, `tfa` tinyint(1) NOT NULL DEFAULT 0, `totp_secret` varchar(64) DEFAULT NULL, `last_login` bigint(20) unsigned DEFAULT NULL, `inactivity_period` tinyint unsigned NOT NULL DEFAULT 12, `spam_reject_score` tinyint unsigned NOT NULL DEFAULT 15, `blocked_senders` text NOT NULL DEFAULT '', `whitelisted_senders` text NOT NULL DEFAULT '', `enforce_tls_in` tinyint(1) NOT NULL DEFAULT 1, `enforce_tls_out` tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (`username`), KEY `domain` (`domain`), KEY `active` (`active`), KEY `openpgpkey_wkd` (`openpgpkey_wkd`), KEY `inactivity_period` (`inactivity_period`), KEY `spam_reject_score` (`spam_reject_score`), CONSTRAINT `mailbox_ibfk_2` FOREIGN KEY (`domain`) REFERENCES `domain` (`domain`) ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
|
||||
'monitored_mailboxes' => "CREATE TABLE IF NOT EXISTS `monitored_mailboxes` (`username` varchar(255) NOT NULL, `last_login` int(11) NOT NULL, PRIMARY KEY (`username`), KEY `lastlogin` (`last_login`), CONSTRAINT `monitored_mailboxes_ibfk_1` FOREIGN KEY (`username`) REFERENCES `mailbox` (`username`) ON DELETE CASCADE ON UPDATE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
|
||||
'tls_report' => "CREATE TABLE IF NOT EXISTS `tls_report` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `report_id` varchar(255) NOT NULL, `organization` varchar(255) NOT NULL, `contact` varchar(255) NOT NULL, `start` datetime NOT NULL, `end` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `report_id_organization` (`report_id`,`organization`), KEY `start_end` (`start`,`end`)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
|
||||
'tls_report_policy' => "CREATE TABLE IF NOT EXISTS `tls_report_policy` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `report_id` bigint(20) unsigned NOT NULL, `policy_type` varchar(255) NOT NULL, `policy_string` text NOT NULL, `policy_domain` varchar(255) NOT NULL, `mx_host` text NOT NULL, `success` int(11) NOT NULL, `failure` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `report_id` (`report_id`), KEY `policy_domain` (`policy_domain`), CONSTRAINT `tls_report_policy_ibfk_1` FOREIGN KEY (`report_id`) REFERENCES `tls_report` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `tls_report_policy_ibfk_3` FOREIGN KEY (`policy_domain`) REFERENCES `domain` (`domain`) ON DELETE CASCADE) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;",
|
||||
@@ -79,6 +79,18 @@ try{
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( $version < 7 ) {
|
||||
foreach ( [ 'blocked_senders', 'whitelisted_senders' ] as $columnName ) {
|
||||
$stmt = $db->query( "SHOW COLUMNS FROM `mailbox` LIKE '$columnName';" );
|
||||
if ( ! $stmt->fetch() ) {
|
||||
$db->exec( "ALTER TABLE `mailbox` ADD COLUMN `$columnName` text NOT NULL DEFAULT '';" );
|
||||
}
|
||||
$stmt = $db->query( "SHOW INDEX FROM `mailbox` WHERE `Key_name` = '$columnName';" );
|
||||
if ( ! $stmt->fetch() ) {
|
||||
$db->exec( "ALTER TABLE `mailbox` ADD INDEX `$columnName` (`$columnName`);" );
|
||||
}
|
||||
}
|
||||
}
|
||||
$db->beginTransaction();
|
||||
$stmt=$db->prepare("UPDATE `settings` SET `value`=? WHERE `setting`='version';");
|
||||
$stmt->execute([DBVERSION]);
|
||||
|
||||
+21
-3
@@ -341,8 +341,16 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
|
||||
);
|
||||
$spam_reject_score = filter_var( $_POST[ 'spam_reject_score' ] ?? null, FILTER_VALIDATE_INT );
|
||||
$valid_spam_settings = in_array( $spam_reject_score, SPAM_REJECT_SCORE_OPTIONS, true );
|
||||
$blocked_senders = is_string( $_POST[ 'blocked_senders' ] ?? null ) ? parse_sender_address_list( $_POST[ 'blocked_senders' ] ) : null;
|
||||
$whitelisted_senders = is_string( $_POST[ 'whitelisted_senders' ] ?? null ) ? parse_sender_address_list( $_POST[ 'whitelisted_senders' ] ) : null;
|
||||
$valid_sender_addresses = $blocked_senders !== null && $whitelisted_senders !== null;
|
||||
$sender_lists_overlap = $valid_sender_addresses && (bool) array_intersect( $blocked_senders, $whitelisted_senders );
|
||||
if ( ! $valid_inactivity_period || ! $valid_spam_settings ) {
|
||||
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Please select valid mailbox settings.')).'</div>';
|
||||
} elseif ( ! $valid_sender_addresses ) {
|
||||
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Please enter a valid email address on each line.')).'</div>';
|
||||
} elseif ( $sender_lists_overlap ) {
|
||||
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('An email address cannot be both blocked and whitelisted.')).'</div>';
|
||||
} else {
|
||||
$alias_goto = '';
|
||||
if ( isset( $_POST[ 'alias_keep_copy' ] ) ) {
|
||||
@@ -355,8 +363,8 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
|
||||
$alias_goto = rtrim( $alias_goto, ',' );
|
||||
$stmt = $db->prepare( 'UPDATE alias SET goto = ?, enforce_tls_in = ?, active = ? WHERE address = ?;' );
|
||||
$stmt->execute( [ $alias_goto, ( isset( $_POST[ 'enforce_tls_in' ] ) ? 1 : 0 ), ( isset( $_POST[ 'active' ] ) ? 1 : 0 ), $_POST[ 'user' ] ] );
|
||||
$stmt = $db->prepare( 'UPDATE mailbox SET enforce_tls_in = ?, enforce_tls_out = ?, active = ?, quota = ?, inactivity_period = ?, spam_reject_score = ?, modified = NOW() WHERE username = ?;' );
|
||||
$stmt->execute( [ ( isset( $_POST[ 'enforce_tls_in' ] ) ? 1 : 0 ), ( isset( $_POST[ 'enforce_tls_out' ] ) ? 1 : 0 ), ( isset( $_POST[ 'active' ] ) ? 1 : 0 ), DEFAULT_QUOTA, $inactivity_period, $spam_reject_score, $_POST[ 'user' ] ] );
|
||||
$stmt = $db->prepare( 'UPDATE mailbox SET enforce_tls_in = ?, enforce_tls_out = ?, active = ?, quota = ?, inactivity_period = ?, spam_reject_score = ?, blocked_senders = ?, whitelisted_senders = ?, modified = NOW() WHERE username = ?;' );
|
||||
$stmt->execute( [ ( isset( $_POST[ 'enforce_tls_in' ] ) ? 1 : 0 ), ( isset( $_POST[ 'enforce_tls_out' ] ) ? 1 : 0 ), ( isset( $_POST[ 'active' ] ) ? 1 : 0 ), DEFAULT_QUOTA, $inactivity_period, $spam_reject_score, implode( "\n", $blocked_senders ), implode( "\n", $whitelisted_senders ), $_POST[ 'user' ] ] );
|
||||
$msg .= '<div class="green" role="status">'.htmlspecialchars(_('Successfully updated mailbox.')).'</div>';
|
||||
}
|
||||
}
|
||||
@@ -1163,7 +1171,7 @@ function send_new_mailbox(): void
|
||||
function send_edit_mailbox(): void
|
||||
{
|
||||
$db = get_db_instance();
|
||||
$stmt = $db->prepare( 'SELECT a.goto, m.active, m.enforce_tls_in, m.enforce_tls_out, m.inactivity_period, m.spam_reject_score FROM alias AS a INNER JOIN mailbox AS m ON (m.username=a.address) WHERE m.username = ?;' );
|
||||
$stmt = $db->prepare( 'SELECT a.goto, m.active, m.enforce_tls_in, m.enforce_tls_out, m.inactivity_period, m.spam_reject_score, m.blocked_senders, m.whitelisted_senders FROM alias AS a INNER JOIN mailbox AS m ON (m.username=a.address) WHERE m.username = ?;' );
|
||||
$stmt->execute( [ $_REQUEST[ 'user' ] ] );
|
||||
if ( $email = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
|
||||
$aliases = explode( ',', $email[ 'goto' ] );
|
||||
@@ -1223,6 +1231,16 @@ function send_edit_mailbox(): void
|
||||
</div>
|
||||
</div>
|
||||
<p><?php echo htmlspecialchars(_('Disabling spam filtering by setting the score to 0 bypasses all spam checks for incoming mail. The higher the score, the more spam you may receive.')); ?></p>
|
||||
<h3><?php echo htmlspecialchars(_('Sender rules')); ?></h3>
|
||||
<p><?php echo htmlspecialchars(_('Enter one exact sender email address per line. Blocking a sender rejects matching incoming mail. Whitelisting a sender bypasses all spam checks, for matching mail.')); ?></p>
|
||||
<div class="row">
|
||||
<div class="col"><label for="blocked_senders"><?php echo htmlspecialchars(_('Blocked senders')); ?></label></div>
|
||||
<div class="col"><textarea name="blocked_senders" id="blocked_senders"><?php echo htmlspecialchars( $email[ 'blocked_senders' ] ?? '' ); ?></textarea></div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col"><label for="whitelisted_senders"><?php echo htmlspecialchars(_('Whitelisted senders')); ?></label></div>
|
||||
<div class="col"><textarea name="whitelisted_senders" id="whitelisted_senders"><?php echo htmlspecialchars( $email[ 'whitelisted_senders' ] ?? '' ); ?></textarea></div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<button name="action" value="save_edit_mailbox" type="submit"><?php echo htmlspecialchars(_('Save mailbox')); ?></button>
|
||||
|
||||
+28
-1
@@ -142,6 +142,18 @@ if ( $_SERVER[ 'REQUEST_METHOD' ] === 'POST' ) {
|
||||
$stmt->execute( [ $spam_reject_score, $_SESSION[ 'email_user' ] ] );
|
||||
$msg .= '<div class="green" role="status">'.htmlspecialchars(_('Successfully updated spam filter settings, changes can take up to 10 minutes to take effect.')).'</div>';
|
||||
}
|
||||
} elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'update_sender_rules' ) {
|
||||
$blocked_senders = is_string( $_POST[ 'blocked_senders' ] ?? null ) ? parse_sender_address_list( $_POST[ 'blocked_senders' ] ) : null;
|
||||
$whitelisted_senders = is_string( $_POST[ 'whitelisted_senders' ] ?? null ) ? parse_sender_address_list( $_POST[ 'whitelisted_senders' ] ) : null;
|
||||
if ( $blocked_senders === null || $whitelisted_senders === null ) {
|
||||
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Please enter a valid email address on each line.')).'</div>';
|
||||
} elseif ( array_intersect( $blocked_senders, $whitelisted_senders ) ) {
|
||||
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('An email address cannot be both blocked and whitelisted.')).'</div>';
|
||||
} else {
|
||||
$stmt = $db->prepare( 'UPDATE mailbox SET blocked_senders = ?, whitelisted_senders = ? WHERE username = ? AND active = 1;' );
|
||||
$stmt->execute( [ implode( "\n", $blocked_senders ), implode( "\n", $whitelisted_senders ), $_SESSION[ 'email_user' ] ] );
|
||||
$msg .= '<div class="green" role="status">'.htmlspecialchars(_('Successfully updated sender rules. Changes can take up to 10 minutes to take effect.')).'</div>';
|
||||
}
|
||||
} elseif ( ! empty( $_SESSION[ 'email_user' ] ) && $_POST[ 'action' ] === 'update_password' ) {
|
||||
if ( empty( $_POST[ 'pass_update' ] ) || empty( $_POST[ 'pass_update2' ] ) || $_POST[ 'pass_update' ] !== $_POST[ 'pass_update2' ] ) {
|
||||
$msg .= '<div class="red" role="alert">'.htmlspecialchars(_('Passwords empty or don\'t match')).'</div>';
|
||||
@@ -384,7 +396,7 @@ if ( empty( $_SESSION[ 'email_user' ] ) ) { ?>
|
||||
$aliases = explode( ',', $tmp[ 'goto' ] );
|
||||
}
|
||||
$aliases_to = implode( "\n", array_diff( $aliases, [ $_SESSION[ 'email_user' ] ] ) );
|
||||
$stmt = $db->prepare( 'SELECT enforce_tls_in, enforce_tls_out, inactivity_period, spam_reject_score FROM mailbox WHERE username = ?;' );
|
||||
$stmt = $db->prepare( 'SELECT enforce_tls_in, enforce_tls_out, inactivity_period, spam_reject_score, blocked_senders, whitelisted_senders FROM mailbox WHERE username = ?;' );
|
||||
$stmt->execute( [ $_SESSION[ 'email_user' ] ] );
|
||||
$mailbox_options = $stmt->fetch( PDO::FETCH_ASSOC );
|
||||
?>
|
||||
@@ -462,6 +474,21 @@ if ( empty( $_SESSION[ 'email_user' ] ) ) { ?>
|
||||
<button name="action" value="update_inactivity_period" type="submit"><?php echo htmlspecialchars(_('Update inactivity period')); ?></button>
|
||||
</form>
|
||||
|
||||
<form class="form_limit" action="manage_account.php" method="post">
|
||||
<h3><?php echo htmlspecialchars(_('Sender rules')); ?></h3>
|
||||
<p><?php echo htmlspecialchars(_('Enter one exact sender email address per line. Blocking a sender rejects matching incoming mail. Whitelisting a sender bypasses all spam checks, for matching mail.')); ?></p>
|
||||
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION[ 'csrf_token' ]; ?>">
|
||||
<div class="row">
|
||||
<div class="col"><label for="blocked_senders"><?php echo htmlspecialchars(_('Blocked senders')); ?></label></div>
|
||||
<div class="col"><textarea name="blocked_senders" id="blocked_senders"><?php echo htmlspecialchars( $mailbox_options[ 'blocked_senders' ] ); ?></textarea></div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col"><label for="whitelisted_senders"><?php echo htmlspecialchars(_('Whitelisted senders')); ?></label></div>
|
||||
<div class="col"><textarea name="whitelisted_senders" id="whitelisted_senders"><?php echo htmlspecialchars( $mailbox_options[ 'whitelisted_senders' ] ); ?></textarea></div>
|
||||
</div>
|
||||
<button name="action" value="update_sender_rules" type="submit"><?php echo htmlspecialchars(_('Update sender rules')); ?></button>
|
||||
</form>
|
||||
|
||||
<h2><?php echo htmlspecialchars(_('Change password')); ?></h2>
|
||||
<form class="form_limit" action="manage_account.php" method="post">
|
||||
<input type="hidden" name="csrf_token" value="<?php echo $_SESSION[ 'csrf_token' ]; ?>">
|
||||
|
||||
+46
-11
@@ -12,16 +12,45 @@ if ( !in_array( $_SERVER[ 'REQUEST_METHOD' ], [ 'GET', 'HEAD' ], true ) || ! has
|
||||
}
|
||||
|
||||
$db = get_db_instance();
|
||||
$stmt = $db->prepare( 'SELECT username, spam_reject_score FROM mailbox WHERE active = 1 AND spam_reject_score != ?;' );
|
||||
$stmt = $db->prepare( "SELECT username, spam_reject_score, blocked_senders, whitelisted_senders FROM mailbox WHERE active = 1 AND (spam_reject_score != ? OR blocked_senders != '' OR whitelisted_senders != '');" );
|
||||
$stmt->execute( [ DEFAULT_SPAM_REJECT_SCORE ] );
|
||||
$recipients_by_policy = [];
|
||||
$sender_settings = [];
|
||||
$sender_rule_index = 0;
|
||||
while ( $mailbox = $stmt->fetch( PDO::FETCH_ASSOC ) ) {
|
||||
if ( (int) $mailbox[ 'spam_reject_score' ] > 0 ) {
|
||||
$policy = 'enabled_' . (int) $mailbox[ 'spam_reject_score' ];
|
||||
} else {
|
||||
$policy = 'disabled';
|
||||
if ( (int) $mailbox[ 'spam_reject_score' ] !== DEFAULT_SPAM_REJECT_SCORE ) {
|
||||
if ( (int) $mailbox[ 'spam_reject_score' ] > 0 ) {
|
||||
$policy = 'enabled_' . (int) $mailbox[ 'spam_reject_score' ];
|
||||
} else {
|
||||
$policy = 'disabled';
|
||||
}
|
||||
$recipients_by_policy[ $policy ][] = mb_strtolower( $mailbox[ 'username' ] );
|
||||
}
|
||||
|
||||
$blocked_senders = array_filter( array_map( 'trim', preg_split( '/\R/', $mailbox[ 'blocked_senders' ] ) ) );
|
||||
$whitelisted_senders = array_filter( array_map( 'trim', preg_split( '/\R/', $mailbox[ 'whitelisted_senders' ] ) ) );
|
||||
$whitelisted_sender_keys = array_fill_keys( array_map( 'mb_strtolower', $whitelisted_senders ), true );
|
||||
foreach ( $blocked_senders as $sender ) {
|
||||
if ( isset( $whitelisted_sender_keys[ mb_strtolower( $sender ) ] ) ) {
|
||||
continue;
|
||||
}
|
||||
$sender_rule_index++;
|
||||
$sender_settings[ 'blocked_sender_' . $sender_rule_index ] = [
|
||||
'priority' => 'high',
|
||||
'from' => $sender,
|
||||
'rcpt' => mb_strtolower( $mailbox[ 'username' ] ),
|
||||
'apply' => [ 'actions' => [ 'reject' => -999 ] ],
|
||||
];
|
||||
}
|
||||
foreach ( $whitelisted_senders as $sender ) {
|
||||
$sender_rule_index++;
|
||||
$sender_settings[ 'whitelisted_sender_' . $sender_rule_index ] = [
|
||||
'priority' => 'high',
|
||||
'from' => $sender,
|
||||
'rcpt' => mb_strtolower( $mailbox[ 'username' ] ),
|
||||
'want_spam' => true,
|
||||
];
|
||||
}
|
||||
$recipients_by_policy[ $policy ][] = mb_strtolower( $mailbox[ 'username' ] );
|
||||
}
|
||||
|
||||
$settings = [];
|
||||
@@ -41,16 +70,22 @@ foreach ( $recipients_by_policy as $policy => $recipients ) {
|
||||
}
|
||||
$settings[ $policy ] = $setting;
|
||||
}
|
||||
$settings += $sender_settings;
|
||||
|
||||
$output = [ 'settings {' ];
|
||||
foreach ( $settings as $name => $setting ) {
|
||||
$output[] = "\t" . $name . ' {';
|
||||
$output[] = "\t\tpriority = " . $setting[ 'priority' ] . ';';
|
||||
$recipients = array_map(
|
||||
static fn( string $recipient ): string => json_encode( $recipient, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ),
|
||||
$setting[ 'rcpt' ]
|
||||
);
|
||||
$output[] = "\t\trcpt = [" . implode( ', ', $recipients ) . '];';
|
||||
if ( isset( $setting[ 'from' ] ) ) {
|
||||
$output[] = "\t\tfrom = " . json_encode( $setting[ 'from' ], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ) . ';';
|
||||
$output[] = "\t\trcpt = " . json_encode( $setting[ 'rcpt' ], JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ) . ';';
|
||||
} else {
|
||||
$recipients = array_map(
|
||||
static fn( string $recipient ): string => json_encode( $recipient, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES ),
|
||||
$setting[ 'rcpt' ]
|
||||
);
|
||||
$output[] = "\t\trcpt = [" . implode( ', ', $recipients ) . '];';
|
||||
}
|
||||
if ( isset( $setting[ 'want_spam' ] ) ) {
|
||||
$output[] = "\t\twant_spam = yes;";
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user