Introduction:

In some situation you may need to delete your mails that are older than the set amount of days from your control panel servers, leaving those mails that are not older than the value set on the server. The best way to achieve this is by simply creating the bash script on the servers and running the files on the server using console or adding cron job to run it automatically after some X amount of days or even often

How to run a Cron job?

Before planning to run a cron job scripts, first we should know the location where the emails are stored on the servers. Then we can execute the steps to remove those. Exactly there are only 3 folders in the control panel for managing received emails.

Those are 3 types:
  • Cur: Having mails that are opened or read by the end user
  • New: All new emails are received in the folder first
  • Temp: Folder that contains the processed emails for the delivery purpose
Log into the server with the account on where you want to remove messages from either in cur, new, temp or having a user with root privileges

To clear the older emails from the server, first you need to remove emails related files from cur folders. They are located under mail directories. There are two directories under the mail folders which are cur and new control panel default mail and same in each user domain directories.

/home/username/mail/kumar.com/***/cur. Here the three star represents the mail user folders.

Step 1:

Creating an exe file with commands to remove all emails:

Code:
# touch mailqueue.sh
Edit the file using any editors like notepad

Code:
# vim mailqueue.sh
find /home/username/mail/domainame.com/*/cur -type f -mtime +30 -exec rm -f {} \;
:wq
Then change the file to an exe. You need to give the file permission has 755

Code:
#chmod 755 /root/mailqueue.sh
Step2:

Create cron job

In this step we are setting the cron job and the set the amount of days the job need to run to find old emails. We can set at monthly, everyday depends upon how many mails you get in a day. To set the cron job we need to execute the following commands

Code:
# crontab -e
0 0 1 * * /root/mailqueue.sh >
/dev/null 2>&1
: wq
# crontab -l
In this scenario the mail cleaner will run on first day of every month and remove all emails that are more than 30 days
Author
bhawanisingh
Views
1,640
First release
Last update
Rating
0.00 star(s) 0 ratings
Top