Cpanel script for automatic deleting emails older than x hours/days?

elcidofaguy

Well-known member
Joined
Jan 13, 2015
Messages
866
Best answers
0
Ratings
660
Points
0
#1
Greetings All,

I'm trying to figure out the best way to bulk delete emails automatically on my webserver with using a script and cron job.

The closest I've got is the following bash script - following example is to delete emails in cur folder older than 60 mins (so that I can test it before setting it for a number of days):

Code:
#!/bin/bash
find -P /homelocn/user/folder/domain.com/email/cur -type f -cmin '+60'
Noting for days I'm using -mtime instead of -cmin. Instead of -cmin I've also tried -mmin...

The reason I'm doing this as a script is so that the various folders can be addressed e.g. spam, cur, new etc for each given email address in one go...

I've setup a cron job and as a test set it to execute every minute and unfortunately its not working. The CRON looks like:

Code:
* 	* 	* 	* 	* 	/homelocn/folder/myscript.sh
I really appreciate if anyone out there can see what is wrong... I'm not sure if its a shared web hosting issue which could be the problem.... I've also set the permission on the file to 755... and still no luck....

Alternatively I'm tempted to write this using php to parse through each file within the relevant email folder checking file datetime in order to ascertain deletion and running that via cron - but it seems long winded approach when this could be done in a single line using a bash shell script...

Appreciate any help, especially if you have done something like this before...

Cheers,

Sid
 

elcidofaguy

Well-known member
Joined
Jan 13, 2015
Messages
866
Best answers
0
Ratings
660
Points
0
#2
Just to add I've now managed to get it to work on the command line within cron as follows:

Code:
find /home/user/mail/domain.com/email/cur/* -mmin +60 -exec rm {} \;
Which is deleting everything older than 60 minutes... So that is very cool ;-) !!!

Next step is to transfer that to the bash script and be able to call that correctly from cron command line.... I'm guessing the problem relates to the bin/bash location....

Appreciate any inputs which can help with that... If I figure it out I'll let everyone know how I did that also... The current solution is okay but I'm sure I can do better lol ;-))
 
Joined
Feb 7, 2014
Messages
21
Best answers
0
Ratings
2
Points
0
#3
Devin Holdcraft
What can it do for unread emails? Does it have a backup for emails that we've not read them yet?
 

elcidofaguy

Well-known member
Joined
Jan 13, 2015
Messages
866
Best answers
0
Ratings
660
Points
0
#4
elcidofaguy
That's a good question.. Unread emails by default are stored in a different folder called "new" on the web server... Once the email has been read it is then moved to a folder called "cur"... Therefore if you do not want to delete unread emails then do not apply the script to the "new" folder location.... Other folders include junk, sent, trash, drafts, tmp... You can see them when you browse your web server from the root to a folder called "mail" which holds folders organized by domain name and then by email name and below that the folders that I just mentioned above i.e cur, new, drafts ... etc ...

If you are like me I forward all emails to one email address and overtime all these email addresses all need to be maintained with removing files as it can grow overtime with exceeding server allowances... You would think that cpanel would have an option to delete emails older than x days but not so - hence why I am looking into cron job plus script to solve this for me...
 
Newer Threads
Latest Threads

Latest postsNew threads

Latest Hosting OffersNew Reviews

Sponsors

Latest Blog ArticlesMost Viewed Threads

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top