Automatically delete emails matching the string "[email protected]"

Operating System & Version
linux
cPanel & WHM Version
104.0 (build 4)

MoviHussein

Member
Jun 23, 2022
6
1
3
ns555389.ip-54-39-16.net
cPanel Access Level
Root Administrator
(I am a beginner at Linux and cPanel) My main goal is to make a cron job or script or command that allows me to automatically delete spam emails older than 2 weeks with all emails that start with "scan" and I have seen in other forms commands such as

Code:
Spam = /home/CPANELACCOUNTNAME/mail/DOMAIN.COM/EMAILNAME/.spam/

This allows me to only delete 1 specific emails spam folder at but I want to be able to delete all spam folders under all emails that start with "scan" and I could either use cron job or make a script to run in the terminal.

Any help is appreciated,
thank you.
 
Last edited by a moderator:

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
11,793
1,877
363
cPanel Access Level
Root Administrator
Hey there! While I don't have a ready-made script for this, you'll likely want to use something similar to this to get all the mail directories:

find /home/*/mail/

as the "find" command can use wildcards to get information across multiple accounts.
 

MoviHussein

Member
Jun 23, 2022
6
1
3
ns555389.ip-54-39-16.net
cPanel Access Level
Root Administrator
Hey there! While I don't have a ready-made script for this, you'll likely want to use something similar to this to get all the mail directories:

find /home/*/mail/

as the "find" command can use wildcards to get information across multiple accounts.
I have looked into that command but how would you for example make it find all matching email addresses that match the string "scan" for example instead of the focus on 1 email.

Thank you for the reply.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
11,793
1,877
363
cPanel Access Level
Root Administrator
Find does all that work for you. For example, this works well to find all the .spam folders on the system:

# find /home/*/mail/*/ -type d -name ".spam"
/home/user1/mail/domain1.com/email1/.spam
/home/user1/mail/domain1.com/email2/.spam
/home/user2/mail/domain2.com/email1/.spam
/home/user3/mail/domain3.com/email1/.spam
/home/user4/mail/domain4.com/email1/.spam
 

MoviHussein

Member
Jun 23, 2022
6
1
3
ns555389.ip-54-39-16.net
cPanel Access Level
Root Administrator
Find does all that work for you. For example, this works well to find all the .spam folders on the system:

# find /home/*/mail/*/ -type d -name ".spam"
/home/user1/mail/domain1.com/email1/.spam
/home/user1/mail/domain1.com/email2/.spam
/home/user2/mail/domain2.com/email1/.spam
/home/user3/mail/domain3.com/email1/.spam
/home/user4/mail/domain4.com/email1/.spam
Thank for the example as it helped me find the exact locations for each file.

The code below works with deleting each email
find /home/%user/mail/example.com/mailuser/.spam -type f -mtime +14 -exec rm {} \;

I can now do this for each email through cron job but if possible is there any way where instead of putting the exact "mailuser", I can do this command of deleting spam folders for specific set of emails that start with "string".

An example of what I mean is if I have 5 deferent emails such as
Am I able to find all email that start with "link" and delete the spam folder of those emails every 2 weeks? or if there is a way to make a script that will find all matching emails with the string "link" and then the code above can run and delete spam (or other types of folders) folders every 2 weeks?​
Thanks.​
 

MoviHussein

Member
Jun 23, 2022
6
1
3
ns555389.ip-54-39-16.net
cPanel Access Level
Root Administrator
Ok, I will try to look around for such a command and if I find a solution to that, I will post it here.
Thank you.
I have found a way to find all emails and delete "Sent" emails after a certain amount of time with the code below

find /home/user/mail/domain/scan*/.Sent -type f -mtime +14 -exec rm {} \;

This allows me to delete all the sent emails older than 2 weeks on every emails that matches (or starts) with "scan".

The only issue I am having now is that my junk emails are not getting deleted with using this code

find /home/user/mail/domain/scan*/.Junk -type f -mtime +14 -exec rm {} \;

While testing the codes I tried to move a email from my inbox to junk and run the code but It would not do anything.
Thank you.