Default sieve rules for new users

mejay

Verified User
Joined
Mar 1, 2020
Messages
14
Is there a way that I can have certain sieve rules automatically created for new users? They should be able to see them and edit/delete them, but I want them to show up and be active automatically for new accounts.
 

This is the way I've done it. Here's my current script, to create a filter to put suspected spam into the Junk folder. Might be useful for someone.

Bash:
# cat /usr/local/directadmin/scripts/custom/email_create_post/spam_sieve_filter.sh
#!/bin/bash

# Available environment variables, as documented in
# <https://docs.directadmin.com/developer/hooks/email.html#email-create-pre-post-sh>:
#
# username: directadmin account
# user: email account name
# domain: domain where virtual email is created
# passwd: password of account
# passwd_is_crypted(0|1): if set, passwd is encrypted
# quota: disk quota in Mib for email account (0 is unlimited)
# limit: daily send limit (0 is unlimited)

# Make sure the environment variables we use have values that make sense.
if [ "${username}" = "" -o "${domain}" = "" -o "${user}" = "" ]; then
    echo "Error: Either \$username, \$domain, or \$user is empty. Aborting." >&2
    exit 1
fi

if [ ! -d "/home/${username}" ]; then
    echo "Error: Directory /home/${username} does not exist. Aborting." >&2
    exit 1
fi

if [ ! -d "/home/${username}/imap/${domain}" ]; then
    echo "Error: Directory /home/${username}/imap/${domain} does not exist. Aborting." >&2
    exit 1
fi

if [ ! -d "/home/${username}/imap/${domain}/${user}" ]; then
    echo "Error: Directory /home/${username}/imap/${domain}/${user} does not exist. Aborting." >&2
    exit 1
fi

# The path of the email account, which we know is valid.
email_account_dir="/home/${username}/imap/${domain}/${user}"

# Check if the managesieve.sieve file already exist. This would not usually be
# the case when the script is run as a DirectAdmin hook, but could be the case,
# if this script is run independently. If the file already exist, we don't want
# to overwrite it.
if [ -f "${email_account_dir}/sieve/managesieve.sieve" ]; then
    echo "Error: ${email_account_dir}/sieve/managesieve.sieve already exists. Will not overwrite. Aborting." >&2
    exit 1
fi

# The contents of the sieve filter we're creating
spam_filter='require ["fileinto"];
# rule:[Spam]
if allof (header :contains "x-spam-flag" "YES")
{
    fileinto "Junk";
    stop;
}'

# Set up the directories, filter, symlink, and fix ownership/permissions
mkdir -p "${email_account_dir}/sieve/tmp"

echo "${spam_filter}" > "${email_account_dir}/sieve/managesieve.sieve"

if [ ! -L "${email_account_dir}/.dovecot.sieve" ]; then
    ln -sr "${email_account_dir}/sieve/managesieve.sieve" "${email_account_dir}/.dovecot.sieve"
fi

chown -hR $username:mail \
    "${email_account_dir}/sieve" \
    "${email_account_dir}/.dovecot.sieve"

chmod 0700 \
    "${email_account_dir}/sieve" \
    "${email_account_dir}/sieve/tmp"

chmod 0600 \
    "${email_account_dir}/sieve/managesieve.sieve"
 
Back
Top