Archiving email (Postfix / Courier-IMAP / ISPconfig 3)
If you want to keep and manage the copies of all incoming and outgoing email messages on your Linux server you can use this script for it. It is made on a base of script provided by Adeptus Mechanicus.
This script will parse through the new subfolder and extract the sender from each email (we use reciepient as there can be multiple recipients but only one sender), if it comes from our domain it creates a folder in a specified area with the sender's name and then copies the email to that folder. If the sender is an external person, it copies it to the External.domains folder.
Here is the information about the system that has been used to test the script:
- Operating system: Centos 6.3, x86_64
- Hosting administration panel: ISPconfig 220.127.116.11
- Mail server: Postfix 2.6.6
- IMAP client: Courier-IMAP 4.9.3
With a little of tweaking this script should work on any server with Courier-IMAP.
So what do you need to get it working for you?
- The Perfect Server setup (well, not necessarily - you are free to use any configuration as long as it has Courier-IMAP installed and preferably Postfix and SquirrelMail too)
- SSH access to your server with superuser's rights
- An email account to store your emails in ready
always_bcc = email@example.com added to /etc/postfix/main.cf file and postfix reloaded
- An idea how and when you want the script to be executed. Most likely you will want to add a new cronjob
- Replace the variables in email-archive.sh script with your own
Make a new directory for the script:
sudo mkdir /usr/local/emailarch
Download the script and move it to the directory you just created:
sudo mv email-archive.sh /usr/local/emailarch/email-archive.sh
Make script executable and vmail user the owner of this directory:
chmod +x /usr/local/emailarch/email-archive.sh
sudo chown -R vmail /usr/local/emailarch
sudo chmod 700 /usr/local/emailarch/email-archive.sh
Now open the script with you favourite text editor (vi, nano, emacs, etc.) and use your variables instead default ones. If you are using Perfect Server's setup, the only thing you must change is domain name and user.
sudo vi /usr/local/emailarch/email-archive.sh
Add a task of checking archive's Inbox every day at midnight to crontab:
sudo crontab -e
Add the following line:
0 0 * * * /usr/local/emailarch/email-archive.sh &> /dev/null
Save and exit.
- Use your Webmail client (SquirellMail, Roundcube, etc.) or desktop client via IMAP in order to access archive folders created by the script.
Hope this will help to some of you.