Empowering you with the knowledge to master Linux web hosting, DevOps and Cloud

 Linux Web Hosting, DevOps, and Cloud Solutions

Category: iRedmail Mailserver

Notes about iRedmail server, settings and configurations

Automating Email Cleanup with doveadm expunge

Managing email storage is a crucial part of maintaining efficient mail servers, especially for administrators using Dovecot. Over time, mailboxes can accumulate a massive number of emails, leading to performance issues and potential storage costs. One effective way to manage this is by automatically deleting emails older than a specific period. In this blog, we’ll discuss how to use doveadm expunge to delete old emails.

Understanding the Basics
Dovecot’s doveadm expunge command is a powerful utility for deleting emails based on specified criteria. Here’s a quick overview of the command syntax:

doveadm expunge -u mailbox ''
-u: Specifies the user mailbox.
mailbox '': Specifies the folder, such as INBOX, INBOX.Spam, etc.
: Defines the filter for emails to be deleted, e.g., before 1w (one week) or before 2w (two weeks).

Use Cases
1. List Existing Mailboxes
Before deleting emails, identify the folders within a specific mailbox. Use the following command:

doveadm mailbox list -u user@example.com
Sample output:

INBOX
INBOX.Spam
INBOX.Drafts
INBOX.Trash
INBOX.Sent

2. Delete Emails Older Than 2 Weeks in All Folders
To remove all emails older than two weeks in all folders for a specific mailbox:

doveadm expunge -u user@example.com mailbox '*' before 2w
3. Exclude INBOX Folder While Deleting
If you want to delete old emails from all folders except INBOX, use:

doveadm expunge -u user@example.com mailbox INBOX.'*' before 2w
4. Delete All Emails in a Mailbox
To delete all emails from all folders within a specific mailbox:

doveadm expunge -u user@example.com mailbox '*' all
Bulk Removal of Old Emails
When managing multiple accounts, you may need to automate the process for all mailboxes on a server. Here’s how to approach this on Plesk and cPanel.

Step 1: Generate a List of Mailboxes
For Plesk:
Run the following command to get a list of all active mailboxes:

plesk db -Ne "select concat(m.mail_name,'@',d.name) as mailbox, m.postbox from domains d, mail m, accounts a where m.dom_id=d.id and m.account_id=a.id and m.postbox='true'" | awk '{print $1}' >mbox.txt
For cPanel:
Generate a list of all mailboxes with:

for i in $(awk '{print $2}' /etc/trueuserdomains); do uapi --user=$i Email list_pops | egrep "\s+email:" ; done | awk '{print $2}' >mbox.txt
Step 2: Automate Deletion with a Script
Create a shell script (mailbox-doveadm-expunge.sh) to process the mailboxes:

#!/bin/bash
# Script to delete emails older than 2 weeks from all mailboxes

MAILBOX_FILE="mbox.txt"

if [ ! -f "$MAILBOX_FILE" ]; then
    echo "Mailbox list file $MAILBOX_FILE not found!"
    exit 1
fi

for mailbox in $(cat $MAILBOX_FILE); do
    echo "Processing mailbox: $mailbox"
    doveadm expunge -u $mailbox mailbox 'INBOX' before 2w
    doveadm expunge -u $mailbox mailbox 'INBOX.*' before 2w
    doveadm expunge -u $mailbox mailbox 'Sent' before 2w
    doveadm expunge -u $mailbox mailbox 'Trash' before 2w
    doveadm expunge -u $mailbox mailbox 'Drafts' before 2w
    doveadm expunge -u $mailbox mailbox 'Spam' before 2w
done

Save the script and ensure it has executable permissions:

chmod +x mailbox-doveadm-expunge.sh
Run the script:

./mailbox-doveadm-expunge.sh

Best Practices
1. Backup Emails: Before performing a mass deletion, create a backup of your mail directories.
2. Test on a Single Mailbox: Verify your deletion criteria by testing on a single mailbox before applying changes in bulk.
3. Monitor Logs: After running doveadm expunge, check Dovecot logs for errors or warnings.

Conclusion
Using doveadm expunge simplifies email management and helps prevent mail server overload by automatically removing old emails. Whether you’re working with individual accounts or hundreds of mailboxes, this approach can save significant time and effort. Integrate this cleanup process into your routine server maintenance to keep your mail system optimized.

Adding Domain Aliases in iRedMail: A Simple bash script

iRedMail is a powerful and open-source mail server solution that simplifies the process of setting up and managing email services. It supports popular email protocols, including IMAP, POP3, and SMTP, and can be used to host multiple email domains. In this guide, we’ll explore how to add domain aliases to iRedMail’s free version with a MySQL backend.

What Are Domain Aliases?
Domain aliases are additional domain names that point to an existing email domain. For example, if you have a primary domain like example.com, you can set up domain aliases like domain.ltd so that emails sent to username@domain.ltd are delivered to the corresponding mailbox of username@example.com. Domain aliases are a convenient way to manage multiple email addresses under a single domain.

The Bash Script:
Here’s a Bash script that simplifies the process of adding domain aliases in iRedMail. You can use this script to automate the task:

#!/bin/bash

# Author: 	Abdul Wahab
# Website: 	Linuxwebhostingsupport.in
# Print purpose and note
printf "Purpose: Add an alias domain in iRedMail. \n\n"
printf "Note: Let's say you have a mail domain example.com hosted on your iRedMail server, if you add domain name domain.ltd as an alias domain of example.com, all emails sent to username@domain.ltd will be delivered to user username@example.com's mailbox. So here domain.ltd is the alias domain and example.com is the traget domain \n\n"

# Prompt the user to enter the alias domain name
read -p "Enter the alias domain name: " ALIAS_DOMAIN

# Prompt the user to enter the target domain name
read -p "Enter the target domain name: " TARGET_DOMAIN

# Connect to the vmail database and check if the target domain exists in the domain table
RESULT=`mysql vmail -N -B -e "SELECT COUNT(*) FROM domain WHERE domain='$TARGET_DOMAIN'"`
if [ $RESULT -ne 1 ]
then
  echo "Error: The target domain $TARGET_DOMAIN does not exist in the domain table. You need to add the target domain first"
  exit 1
fi

# Insert the alias domain record
mysql vmail <<EOF
INSERT INTO alias_domain (alias_domain, target_domain)
VALUES ('$ALIAS_DOMAIN', '$TARGET_DOMAIN');
EOF

# Print completion message
echo "Alias domain $ALIAS_DOMAIN has been added for $TARGET_DOMAIN."

How to Use the Script:

Copy the provided Bash script into a text file, e.g., add_domain_alias.sh.
Make the script executable by running the following command:

chmod +x add_domain_alias.sh

Execute the script by running ./add_domain_alias.sh in your terminal.
Follow the prompts to enter the alias domain and target domain names.
The script will connect to the MySQL database and insert the alias domain record.

Conclusion:
Adding domain aliases in iRedMail is a straightforward process, and the provided Bash script can simplify it even further. With domain aliases, you can efficiently manage multiple email addresses under a single domain, enhancing your email hosting capabilities.

Feel free to use this script to streamline your iRedMail email domain management, making it easier to accommodate various email addresses and domains.

Email sending limits and throttling – iRedmail free edition

iRedMail is:
1. A ZERO COST, fully fledged, full-featured mail server solution. All components are free and open source software
2. It support unlimited domains and email accounts.
2. SpamAssassin, ClamAV, SPF, DKIM, greylisting, whitelisting, blacklisting.
3. Stores mail accounts in your favorite backend: OpenLDAP, MySQL, MariaDB, PostgreSQL.
4. Supports major Linux distros

Throttling

iRedmail supports following level of throttling for email limits.
Global, per-domain and per-user throttling based on: max size of single message, number of max inbound/outbound messages per time unit.

iRedmail uses a component named iredpad for this. With iRedmail Pro version, you can configure these settings through GUI(iRedadmin panel). However they are not available through GUI for free version. The feature is enabled though we will have to modify them through database directly.

So the database is “iredapd” and table is “throttle”.

Here are some examples

1. Allow user `user@domain.com` to send 50 mails in 5 minutes (period=300):

INSERT INTO throttle (account, kind, priority, period, msg_size, max_msgs, max_quota) VALUES (‘user@domain.com’,’outbound’,100,300,0,50,0);

2. Set global limit of 500 mails per day and a maximum single mail size of 55MB

INSERT INTO throttle (account, kind, priority, period, msg_size, max_msgs, max_quota) VALUES (‘@.’,’outbound’,0,86400,57671680,500,0);

57671680 bytes = 55MB
86400 – 1 Day

# Technical details of throttle plugin
# ————-
#
# Currently you may throttle based on:
#
# – amount of mails sent over a given period of time
# – accumulated mail size sent over a given period of time
# – size of singe message
#
# Eg: You can enforce that user@domain.com does not send more than 1000 mails
# or 1GB of mail (whichever limit is hit first) in 5 minute.
#
# Possible throttling address:
#
# *) Full email address: user@domain.com
# *) Domain name (with a prefixed ‘@’): @domain.com
# *) Sub-domain name (with a prefixed ‘@.’): @.domain.com
# *) IP address: 192.168.1.1
# *) IP network: 192.168.1.*
# *) Catch-all for email address: ‘@.’ (note, the dot is required)
# *) Catch-all for IP address: ‘@ip’
#
# Priorities of different thorttle address (larger digital number has higher priority):
#
# *) email: 100 # e.g. ‘user@domain.com’. Highest priority
# *) wildcard_addr: 90 # e.g. `user@*`. used in plugin `amavisd_wblist`
# # as wildcard sender. e.g. ‘user@*`
# *) ip: 80 # e.g. 173.254.22.21
# *) wildcard_ip: 70 # e.g. 173.254.22.*
# *) cidr: 70 # e.g. 173.254.22.0/24
# *) domain: 60 # e.g. @domain.com
# *) subdomain: 50 # e.g. @.domain.com
# *) top_level_domain: 40 # e.g. @com, @org
# *) catchall: 0 # ‘@.’. Lowest priority

Please note that priority for global setting is 0 and priority of a single user is 100.

SOGo ActiveSync not working – iRedmail

iRedMail is:
1. A ZERO COST, fully fledged, full-featured mail server solution. All components are free and open source software
2. It support unlimited domains and email accounts.
2. SpamAssassin, ClamAV, SPF, DKIM, greylisting, whitelisting, blacklisting.
3. Stores mail accounts in your favourte backend: OpenLDAP, MySQL, MariaDB, PostgreSQL.
4. Supports major Linux distros

You know how difficult is to configure a fully fledged email service you have tried. iRedmail do this heavy part for you. I would recommend this piece of software to everyone. I used to configure email server manually using Postfix, Postfixadmin, MySQL, SpamAssassin, etc. Even though I have done this several times, it needs 2-3 hours for setup. But iRedmail can do all of this and MORE in few minutes, thus saves lot of time.

iRedmail comes with SOGo which provides EAS Activesync support

SOGo is providing the EAS support. If you are getting an error “Unable to connect” on email client do following

1. Check https://hostname/Microsoft-Server-ActiveSync

if it gives 404, issue with SOGo httpd conf
If it gives white page, no problem with conf

SOGo installs Apache config file /etc/httpd/conf.d/SOGo.conf by default, please open it and find below lines:

#ProxyPass /Microsoft-Server-ActiveSync \
# http://127.0.0.1:20000/SOGo/Microsoft-Server-ActiveSync \
# retry=60 connectiontimeout=5 timeout=360

Remove # at the beginning to enable ActiveSync support:
ProxyPass /Microsoft-Server-ActiveSync \
http://127.0.0.1:20000/SOGo/Microsoft-Server-ActiveSync \
retry=60 connectiontimeout=5 timeout=360

Find string yourhostname in the same file, replace all yourhostname by your FQDN server hostname. (Tip: you can get your FQDN hostname with command hostname -f.)

Restart Apache

Disable greylisting completely – iRedmail

iRedMail is:
1. A ZERO COST, fully fledged, full-featured mail server solution. All components are free and open source software
2. It support unlimited domains and email accounts.
2. SpamAssassin, ClamAV, SPF, DKIM, greylisting, whitelisting, blacklisting.
3. Stores mail accounts in your favourte backend: OpenLDAP, MySQL, MariaDB, PostgreSQL.
4. Supports major Linux distros

You know how difficult is to configure a fully fledged email service you have tried. iRedmail do this heavy part for you. I would recommend this piece of software to everyone. I used to configure email server manually using Postfix, Postfixadmin, MySQL, SpamAssassin, etc. Even though I have done this several times, it needs 2-3 hours for setup. But iRedmail can do all of this and MORE in few minutes, thus saves lot of time.

How to disable greylisting completely

Greylisting is a method of defending e-mail users against spam. A mail transfer agent (MTA) using greylisting will “temporarily reject” any email from a sender it does not recognize

To disable greylisting completely, please remove plugin name greylisting in iRedAPD config file
/opt/iredapd/settings.py, parameter plugins =:
plugins = […, ‘greylisting’, …]

Restarting iRedAPD service is required.

Reject outgoing Spam – iRedmail server

iRedMail is:
1. A ZERO COST, fully fledged, full-featured mail server solution. All components are free and open source software
2. It support unlimited domains and email accounts.
2. SpamAssassin, ClamAV, SPF, DKIM, greylisting, whitelisting, blacklisting.
3. Stores mail accounts in your favourte backend: OpenLDAP, MySQL, MariaDB, PostgreSQL.
4. Supports major Linux distros

You know how difficult is to configure a fully fledged email service you have tried. iRedmail do this heavy part for you. I would recommend this piece of software to everyone. I used to configure email server manually using Postfix, Postfixadmin, MySQL, SpamAssassin, etc. Even though I have done this several times, it needs 2-3 hours for setup. But iRedmail can do all of this and MORE in few minutes, thus saves lot of time.

Reject outgoing Spam

iRedmail comes with Amavis, Spamassasin and Clamd. By default iRedmail will not block Outgoing spam message. However you can tweak Amavis configurations to reject such mails.

/etc/amavisd/amavisd.conf

Add following to $policy_bank{‘ORIGINATING’}

final_virus_destiny => D_DISCARD,
final_banned_destiny => D_REJECT,
final_spam_destiny => D_REJECT,
final_bad_header_destiny => D_REJECT,

Then restart amavis /etc/init.d/amavisd restart

This will reject all outgoing spam mails above the score

Of course tweak the spam score accordingly.

Powered by WordPress & Theme by Anders Norén