hMail Server Cleanup Utility [PHP Required]

This section contains scripts that hMailServer has contributed with. hMailServer 5 is needed to use these.
Post Reply
^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

hMail Server Cleanup Utility [PHP Required]

Post by ^DooM^ » 2009-06-28 21:36

DO NOT USE AT ALL

BACKUP YOUR DATABASE AND DATA DIRECTORY BEFORE YOU USE THIS SCRIPT

If you have been using hMail for long periods of time and upgraded all the way up from V4 as new releases have emerged then you may have orphaned files lurking in the data and sub directories. AV and other anti spam mechanisms could sometimes lock a file at which point it's not removed (this doesn't happen any more).

I have written a PHP script that recurses through the accounts (user selected) and checks files against MySQL database entries. If there is no corresponding database entry the file is orphaned and can be removed. This script also can remove empty directories for you if you select the option.

I doubt I will expand on this script any further and will not be adding any more features unless I feel it's extremely useful. I will not be adding support to recurse through every domain and account in one go as PHP would time out before its done (unless you start messing with timeout values under PHP.ini) and will place your database under heavy load. If you want it to do that then feel free to modify this script to do so.

Once you have downloaded the file open it up in notepad++ or similar and alter the config values at the top of the script and uploaded it to your webserver.
Make sure that PHP has the correct permissions to delete files and directories from hMail's data directory else this script is pretty useless.

This script is potentially dangerous if you don't know what you are doing. I have restricted the script to only allow access to specific IP addresses (configurable at the top of the script).
img1.png
img2.png
img3.png
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

User avatar
ekosch
New user
New user
Posts: 19
Joined: 2007-01-06 07:48
Location: INDONESIA
Contact:

Re: hMail Server Cleanup Utility [MySQL & PHP Required]

Post by ekosch » 2009-07-11 05:13

how to set database port at Configuration variables below?
my hmailserver database work at port 3307.

regards

eko s

// -------------------- Configuration variables ----------------------
$pData->host = "127.0.0.1"; // Host Name
$pData->user = "root"; // Username
$pData->pass = "bla bla bla"; // Password
$pData->dbase = "hmailserver"; // Database Name
$pData->MainDir = "C:\Program Files\hMailServer\Data"; // Must NOT contain trailing slash.
$pData->Ignore = array( 'domain.com', 'domain2.com' ); // Domains to ignore.
$pData->AllowedIP = array( '127.0.0.1' ); // IP's that can use this script

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [MySQL & PHP Required]

Post by ^DooM^ » 2009-07-12 17:50

In the host put 127.0.0.1:3307
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

Softek
New user
New user
Posts: 12
Joined: 2008-10-30 16:53

Re: hMail Server Cleanup Utility [MySQL & PHP Required]

Post by Softek » 2009-09-14 18:07

Hey, I ported the script to use the new Microsoft SQL Server Driver for PHP for those of us using MSSQL.

Softek

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [MySQL & PHP Required]

Post by ^DooM^ » 2009-09-14 19:15

Nicely done!

Nice idea adding the timeout as well, were you having issues with the timeout? I have my timeout set high by default for upload purposes.

Edit: I updated my Script with the timeout value and placed your script in the first post as well.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

Softek
New user
New user
Posts: 12
Joined: 2008-10-30 16:53

Re: hMail Server Cleanup Utility [PHP Required]

Post by Softek » 2009-10-15 17:51

*sigh* I need to remember to check these forums more often.

Yes I was having timeout issues. The php timeout is probably still default on that server.

The hMail server we have handles 20-50 domains each with several email addresses. We also have some pretty heavy users that would get 500-1000 emails a day. Several of the users had around 20-50 orphaned files or empty folders, and ended up timing out when I tried to run the cleanup.

jourdan
New user
New user
Posts: 14
Joined: 2009-11-12 04:15

Re: hMail Server Cleanup Utility [PHP Required]

Post by jourdan » 2009-11-13 07:05

Hi, Doom.

Nice script!! I'm looking for a program to automatically remove those empty directories for my domain and user directories.

May I know how to change your program to predefined remove those empty directories(without select Domain, Account and Process)?

Regards,

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [PHP Required]

Post by ^DooM^ » 2009-11-13 10:53

Feel free to rip the script apart and alter it to your own needs.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

delporo
New user
New user
Posts: 8
Joined: 2009-03-06 17:06

Re: hMail Server Cleanup Utility [PHP Required]

Post by delporo » 2010-05-11 19:45

helo every body
is it possible to allow all ip ?

$pData->AllowedIP = array( '123.123.123.123' );


Thank you for all

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [PHP Required]

Post by ^DooM^ » 2010-05-11 20:05

Which version are you using MySQL or MsSQL?
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

delporo
New user
New user
Posts: 8
Joined: 2009-03-06 17:06

Re: hMail Server Cleanup Utility [PHP Required]

Post by delporo » 2010-06-02 12:54

i use mysql

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [PHP Required]

Post by ^DooM^ » 2010-06-02 13:18

Ok start at line 97 find the following code

Code: Select all

if( in_array( $pData->ConnectingIP, $pData->AllowedIP ) )
    ShowMainPage();
else
    echo '<table align="center" class="bordered" width="600" border="1" cellpadding="2" cellspacing="0"><tr><td colspan="2" align="center"><strong>Access Denied!!</strong></td></tr></table>';
Replace all of it with

Code: Select all

ShowMainPage();
That should bypass the check completely.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

morcock
New user
New user
Posts: 11
Joined: 2009-12-09 14:43

Re: hMail Server Cleanup Utility [PHP Required]

Post by morcock » 2010-11-21 22:40

Hello.

I have tried to use the script but I can not. My configuration is:
$pData->host = "127.0.0.1"; // Host Name
$pData->user = "root"; // Username
$pData->pass = "password"; // Password
$pData->dbase = "hmailserver"; // Database Name
$pData->MainDir = "C:\Archivos de programa\hMailServer\Data"; // Must NOT contain trailing slash.
$pData->Ignore = array( 'dominio.es', 'dominio2.es' ); // Domains to ignore.
$pData->AllowedIP = array( '127.0.0.1' ); // IP's that can use this script

set_time_limit(240); // Script Time Limit, for larger mailboxes a longer timeout is needed.

I can see this error:

host = "127.0.0.1"; // Host Name
$pData->user = "root"; // Username
$pData->pass = "password"; // Password
$pData->dbase = "hmailserver"; // Database Name
$pData->MainDir = "C:\Archivos de programa\hMailServer\Data"; // Must NOT contain trailing slash.
$pData->Ignore = array( 'dominio.es', 'dominio2.es' ); // Domains to ignore.
$pData->AllowedIP = array( '127.0.0.1' ); // IP's that can use this script
set_time_limit(240); // Script Time Limit, for larger mailboxes a longer timeout is needed.
// ------------------ End Config Do Not Alter Below ------------------
$pData->DomainID = 0;
$pData->AccountID = 0;
$pData->DomainID = $_GET['domainid']+0;
$pData->AccountID = $_POST['lstaccountid']+0;
$pData->Process = addslashes( $_POST['lstprocess'] ); $pData->ConnectingIP = $_SERVER['REMOTE_ADDR']; ?> ConnectingIP, $pData->AllowedIP ) ) ShowMainPage(); else echo '
Access Denied!!


I can connect with my phpmyadmin.

What is wrong?.

Thanks in advance

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [PHP Required]

Post by ^DooM^ » 2010-11-22 00:39

You see the code in the error? That is usually indicative of a php config error but you say you are running phpmyadmin fine.

Without an actual error it's hard to diagnose.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

morcock
New user
New user
Posts: 11
Joined: 2009-12-09 14:43

Re: hMail Server Cleanup Utility [PHP Required]

Post by morcock » 2010-11-22 11:30

Ok. I will check it out.

morcock
New user
New user
Posts: 11
Joined: 2009-12-09 14:43

Re: hMail Server Cleanup Utility [PHP Required]

Post by morcock » 2011-02-01 13:54

Sorry for the delay, too many things in my head.

These are my versions:

Apache: 2.2.17 (Win32)
PHP: 5.3.4
Mysql: 5.1.54-community
Versión del cliente: mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $
My hmailserver program is in this folder:

I receive this error when I try to run the php script:
[Tue Feb 01 12:21:35 2011] [error] [client 127.0.0.1] PHP Notice: Undefined index: domainid in C:\\appsrv\\www\\phpwebadmin\\hMailServerCleanUpUtility.php on line 45
[Tue Feb 01 12:21:35 2011] [error] [client 127.0.0.1] PHP Notice: Undefined index: lstaccountid in C:\\appsrv\\www\\phpwebadmin\\hMailServerCleanUpUtility.php on line 46
[Tue Feb 01 12:21:35 2011] [error] [client 127.0.0.1] PHP Notice: Undefined index: lstprocess in C:\\appsrv\\www\\phpwebadmin\\hMailServerCleanUpUtility.php on line 47
[Tue Feb 01 12:35:14 2011] [error] [client 127.0.0.1] script 'C:/appsrv/www/phpwebadmin/hMailDBtoFileCleanup.php' not found or unable to stat

How can I resolve this error?

morcock
New user
New user
Posts: 11
Joined: 2009-12-09 14:43

Re: hMail Server Cleanup Utility [PHP Required]

Post by morcock » 2011-02-01 13:56

The hmailserver version: 5.3.3-B1879

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [PHP Required]

Post by ^DooM^ » 2011-02-01 14:10

The first three are notices. php is warning because i don't define the variables at the start of the script. Either disable the notice error messages in your php.ini or add the vars to the top of the script with empty values.

The last error is because you renamed the script. it must be called hMailDBtoFileCleanup.php
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

morcock
New user
New user
Posts: 11
Joined: 2009-12-09 14:43

Re: hMail Server Cleanup Utility [PHP Required]

Post by morcock » 2011-02-01 15:41

Thank you very for your quick response.

After I renamed the script to its original name the script started to function.

Thanks for your support

kento
New user
New user
Posts: 20
Joined: 2011-04-06 15:07

Re: hMail Server Cleanup Utility [PHP Required]

Post by kento » 2011-04-16 09:15

i tryed out the script, but where do i find the user and password?
i installed the the latest version of hmailserver and the db that came with it, but havent got a clue what username and password it used. so cant run the script. and the server do make alot of folders that becomes emty then the user have read the mail and then delete it.

greetings
kento

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [PHP Required]

Post by ^DooM^ » 2011-04-16 10:41

If at first you don't succeed, bomb disposal probably isn't for you! ヅ

kento
New user
New user
Posts: 20
Joined: 2011-04-06 15:07

Re: hMail Server Cleanup Utility [PHP Required]

Post by kento » 2011-04-16 12:46

changed the server to mysql, seamed a bit more easy becource i already have mysql running (yep that was quite a work, but guess its worth it) :mrgreen:
ill have a look at the cleanup php page when i have used the server a day or 2 so there is some folders to work on.
ill be back if i cant get it up and running.

greetings
kento.

xbipin
Normal user
Normal user
Posts: 79
Joined: 2010-05-23 16:24

Re: hMail Server Cleanup Utility [PHP Required]

Post by xbipin » 2011-04-25 17:07

i used this script today and ended up loosing almost all emails in all domains, the db entries remain and the eml file also remain but all get erased with with the same content of

The mail server could not deliver the message to you since the file D:\Program Files (x86)\hMailServer\Data\domain.com\sales\52\{52868C66-463D-4A07-9F32-FAAF962409AD}.eml does not exist on the server.

The file may have been deleted by anti virus software running on the server.

hMailServer

rjk
Normal user
Normal user
Posts: 248
Joined: 2010-03-30 19:30
Location: uʍop ǝpısdn

Re: hMail Server Cleanup Utility [PHP Required]

Post by rjk » 2011-04-25 18:18

xbipin wrote:i used this script today and ended up loosing almost all emails in all domains, the db entries remain and the eml file also remain but all get erased
What version are you using? Did you make a backup?
hMailServer 5.5.2-B2129 on Server 2008 R2 VM
MySQL 5.5.25, IIS 7.5, PHP 5.6.2 via FastCGI, RoundCube 1.0.3
XenServer 6.0 on HP DL380 G5 32GB RAM

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [PHP Required]

Post by ^DooM^ » 2011-04-25 18:41

As rjk posted, what version of hmail, did you backup first, and did you test it with the report only option before you blindly ran it on all of your accounts on all of your domains?
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

xbipin
Normal user
Normal user
Posts: 79
Joined: 2010-05-23 16:24

Re: hMail Server Cleanup Utility [PHP Required]

Post by xbipin » 2011-04-28 07:49

im using HM 5.3.3, i didnt backup, i tested it with the report only on a single account and looked fine, then when i ran it, it replaced the content of all the eml files with a single message

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [PHP Required]

Post by ^DooM^ » 2011-04-28 09:21

Why did you not backup? It says quite specifically on the first post and in the script where you add your DB username and pass to backup before use. Many users have used this script without issue and I ran the script today on my 5.3 box as a test and it worked perfectly fine. The script did not create those messages, hmail created them when it was unable to locate the emails that the database was pointing to. The script will only remove an orphaned file, it will not remove one that has a database record. The only explination I can think of is that those files were already removed by AV or you broke the script when you installed it. The source is there for anyone to verify it does what it's coded to do. It even err's on the side of caution.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

xbipin
Normal user
Normal user
Posts: 79
Joined: 2010-05-23 16:24

Re: hMail Server Cleanup Utility [PHP Required]

Post by xbipin » 2011-04-28 11:14

it could possibly be something else or mayb hmailserver itself but let me point it out to u

- the eml modified timestamp is same as when i ran the script
- there is no antivirus on the server
- the database is pointing to those files with such data and says couldnt find that file but the fact is the file name is the same which it says is missing so actually something happened and all the eml files went down to 1KB and content as those messages and suppose if i open xxxx.eml, the message says xxx.eml does not exist in its content.

after that i though something went terribly wrong so i ran the data synchronize tool and mayb that did the rest of the damage and 12GB worth of eml files with same content now.

neways im not blaming the script in total, could be that or data sync tool or hmail itself but i just wanted to bring to notice that such a thing happened after i initially ran the script

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [PHP Required]

Post by ^DooM^ » 2011-04-28 11:31

Well if it was the script i can only apologise but i still do not see how it could be possible without testing the script on your setup which isn't likely to happen. I'll put up a bigger warning to backup in the first post.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

irblue
New user
New user
Posts: 29
Joined: 2005-06-27 15:51

Re: hMail Server Cleanup Utility [PHP Required]

Post by irblue » 2011-05-04 11:11

Dear Doom,

Any chance to run this cleanup utility on 5.4 ?

Thanks
Blue

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [PHP Required]

Post by ^DooM^ » 2011-05-04 14:24

Not currently due to file paths no longer being full in the database. If your file paths are still full in hm_messages, this script will work.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

JimmE
New user
New user
Posts: 2
Joined: 2011-08-03 12:21

Re: hMail Server Cleanup Utility [PHP Required]

Post by JimmE » 2011-08-03 12:26

Hi,

Many thanks for the script.

Just one suggestion for anyone who wants to develop the script further.

I've noticed that if the user's directory does not exist in the data directory (no idea why it wouldn't - maybe the address has never received an email?) then the script will return an infinite number of 'orphaned file' results with no filename.

I'll try to modify the script myself and post back but someone may be able to beat me to it - I'm much better at identifying things than fixing them, sadly :)

Many thanks though, this script is helping considerably!

Regards,

James.

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [PHP Required]

Post by ^DooM^ » 2011-08-03 12:47

If the user directory doesn't exist then there should be no emails to check against. seems strange it should return an infinate number of orphaned files. Perhaps i have misunderstood
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

JimmE
New user
New user
Posts: 2
Joined: 2011-08-03 12:21

Re: hMail Server Cleanup Utility [PHP Required]

Post by JimmE » 2011-08-03 13:03

That's exactly what I thought. It could well be something specific to the local configuration on this server, perhaps. I've no idea why the user folder doesn't exist, but for accounts where it doesn't, running the script shows an output such as:

Results for Blackberry@beag****.co.uk
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File
Orphaned File

Whereas if I create the folder manually (my initial post here was a question asking what was going wrong until I managed to work out the cause of the sympoms at least!) the output is simply:

Results for Blackberry@beag****.co.uk
Process Complete

Hardly a major issue, although like you say, it shouldn't be able to find anything to display. It's running on a Windows server with MS SQL though, so could be to do with the return value from windows-php when it cannot find the directory.

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [PHP Required]

Post by ^DooM^ » 2011-08-03 13:22

I can't see the bug right now and i don't have time to check through it. I'll decommission this script as it's potentially dangerous.

I suspect this is what happened to xbipin except instead of checking like you did he just ran it blindly.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

jogep
New user
New user
Posts: 1
Joined: 2012-05-08 17:52

Re: hMail Server Cleanup Utility [PHP Required]

Post by jogep » 2012-05-08 18:11

Where can I download this Cleanup Utility?

Some of my Users become an Error 0x800CCC0F when using Outlook to fetch latest Mails.
Maybe this Tool can help me to check if this POP3 Account fine.

Thanks
jogep

^DooM^
Site Admin
Posts: 13861
Joined: 2005-07-29 16:18
Location: UK

Re: hMail Server Cleanup Utility [PHP Required]

Post by ^DooM^ » 2012-05-08 19:27

You can't it's not safe to run.
If at first you don't succeed, bomb disposal probably isn't for you! ヅ

Post Reply