Ok. This is a Inform/Delete script.
This script has some more variables; which will allow you to either Warn or just Inform the user of the impending deletion. E.G. You want to tell the user that 'target' emails are deleted or will be deleted.
It only reads partial subjects lines, and will grab the first FROM(reply-to), TO, DATE and SUBJECT found in an email.
NOTE: this requires a command line emailer, I use V-Mailer (google vmailer) cause it uses authentication
NOTE2: requires read/write/delete/execute to its own folder and %hmailserver%\data
NOTE3: It may have problems when email accounts exceed two 'dots'
john.smith.doe@domain.com; I've only tested addresses with one 'dot' or no 'dot'
john.doe@domain.com or
john@domain.com
Variables include:
targetdomain = Target Domain
targetfolder = Target Folder
ageindays = Process email as old or older than xx days
warnonly = 'yes' will only inform the user, 'no' will also delete the mails.
useauth = Wether or not your outbound server requires authentication
mailaddress = the account from the mail will come from; e.g.
system@domain.com
mailersmtp = smtp server to use; default is localhost
mailerpass = if you use authentication, you'll need to provide a password; if not, leave empty.
Code: Select all
@echo off
cls
rem ------------
rem MYSQL Vars
rem ------------
set mysqlexe="D:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe"
set mysqluser=CHANGEME
set mysqlpass=CHANGEME
set mysqldb=hmail
rem ------------
rem Hmail Vars
rem ------------
set targetfolder=Trash
set targetdomain=CHANGEFORTARGETDOMAIN.com
set ageindays=5
rem ------------
rem Script Vars
rem ------------
rem Warn Only - 'yes' will only generate email summaries, while setting this to 'no' will also Delete the mail
rem using 'yes' is helpful if you want to notify but not delete, e.g. delete job runs later/next day.
rem if you use a delete script with this, like this runs Tuesday and the delete scrip runs on Wednesday
rem it is suggested to adjust the delete script to include an extra day-of-age like +1 to the ageindays of
rem of this script.
set warnonly=yes
set useauthentication=yes
set maileraddress=system@domain.com
set mailersmtp=localhost
set mailerpass=SMTPPASSWORD
rem ------------------------------------------------------------------------
rem Do not modify below this line
rem ------------------------------------------------------------------------
rem ' Main, squish sql controls
set mysql=%mysqlexe% -u%mysqluser% -p%mysqlpass% --database %mysqldb%
rem ' Main, empty var space
set mysqlexe=
set mysqluser=
set mysqlpass=
set mysqldb=
del out.*
rem ' Main, create sql script to collect file names and IDs of contents
rem - the following is one line, and should not be word-wraped
echo SELECT messageid,messagefilename FROM hm_messages,hm_accounts,hm_domains,hm_imapfolders WHERE hm_messages.messageaccountid = hm_accounts.accountid AND hm_accounts.accountdomainid = hm_domains.domainid AND hm_domains.domainname = '%targetdomain%' AND hm_messages.messagecreatetime ^<= date_sub(now(), INTERVAL %ageindays% DAY) AND hm_messages.messagefolderid = hm_imapfolders.folderid AND hm_imapfolders.foldername = '%targetfolder%';>oldmail.sql
rem ' Main, get target contents
%mysql%<oldmail.sql>oldmail.txt
rem ' Main, create del sql script
for /f "tokens=1* skip=1" %%a in (oldmail.txt) do echo DELETE FROM hm_messages WHERE messageid = '%%a'; >>del.sql
rem ' Main, delete sql entries
if /i "%warnonly%"=="no" %mysql%<del.sql
rem ' Main, Warn Only?
if /i "%warnonly%"=="no" goto:delete
goto:skipdelete
:delete
rem ' Main, delete actual message file
for /f "tokens=1* skip=1" %%a in (oldmail.txt) do del "%%b" /y
:skipdelete
rem ' Main, processing SQL dump file for file names
for /f "tokens=1* skip=1" %%a in (oldmail.txt) do set file="%%b"&call:read
goto:send
:read
echo -=-=-=-=-=-=-=-=-=-=- NEW MESSAGE -=-=-=-=-=-=-=-=-=-=-=-
rem ' Read, Clear vars between files
set d=
set f=
set s=
set sub=
set from=
set mdate=
set to=
set msgfile=
set dumped=
rem ' Read, Strip extra slashed in file names
set file=%file:\\=\%
rem ' Read, Read message files and collect data
for /f "tokens=1,2,3,4,5,6,7,8 delims=: " %%a in ('type %file%') do set a="%%a"&set b="%%b"&set c="%%c"&set dh="%%c %%d %%e %%f:%%g:%%h"&set bc="%%b %%c"&call:msg
goto:eof
:msg
if "%dumped%"=="mark" goto:eof
rem ' Read, Strip extra quotes from passing vars
set a="%a:"=%"
set b="%b:"=%"
rem ' Read, Process line items for Date, From and Subject
:msg1
rem ' Read, FROM
if "%from%"=="mark" goto:msg2
if /i %a%=="Return-Path" set f=%b%&set from=mark&echo from = %b%&goto:eof
:msg2
rem ' Read, SUBJECT
if "%sub%"=="mark" goto:msg3
if /i %a%=="subject" set s=%bc%&set sub=mark&echo subject = %bc%&goto:eof
:msg3
rem ' Read, DATE
if "%mdate%"=="mark" goto:msg4
if /i %a%=="date" set d=%dh%&set mdate=mark&echo mdate = %dh%&goto:eof
:msg4
if "%sub%"=="" goto:eof
if "%from%"=="" goto:eof
if "%mdate%"=="" goto:eof
rem ' Read, Dump message data to out file
for /f "tokens=1,2,3,4,5,6,7,8,9 delims=:\" %%a in ('echo %file%') do if /i "%%d"=="data" set to="%%f@%%e"&set msgfile=%%f.%%e
for /f "tokens=1,2,3,4,5,6,7,8,9 delims=:\" %%a in ('echo %file%') do if /i "%%c"=="data" set to="%%e@%%d"&set msgfile=%%e.%%d
for /f "tokens=1,2,3,4,5,6,7,8,9 delims=:\" %%a in ('echo %file%') do if /i "%%b"=="data" set to="%%d@%%c"&set msgfile=%%d.%%c
echo to = %to%
if "%mdate%"=="mark" echo %d%,%f%,%s%>>out.%to%
set dumped=mark
goto:eof
:send
rem ' Send
:vmail
for /f "tokens=1* delims=." %%a in ('dir /b out.*') do set to="%%b"&call:draft
goto:end
:draft
rem ---------------------------------------------------
rem ' Draft Email that will be sent to recipients.
rem ---------------------------------------------------
echo FROM: %maileraddress%>out.eml
echo TO: %to%>>out.eml
if /i "%warnonly%"=="yes" echo SUBJECT: Target Email Will Be Deleted.>>out.eml
if /i "%warnonly%"=="no" echo SUBJECT: Target Email Have Been Deleted.>>out.eml
echo.>>out.eml
if /i "%warnonly%"=="yes" echo Targeted emails will be deleted from your email account.>>out.eml
if /i "%warnonly%"=="no" echo Targeted emails have been deleted from your email account.>>out.eml
echo They exceed one or more system policies for %targetdomain%:>>out.eml
echo - Email Older than %ageindays% days in the '%targetfolder%' folder.>>out.eml
echo -------------------------------------------------------------------->>out.eml
echo DATE......................FROM....................Subject (partial)>>out.eml
echo -------------------------------------------------------------------->>out.eml
type out.%to%>>out.eml
rem '' VMailer.exe (file) (server) (to) (from) (auth username-optional) (auth pass-optional)
if /i "%useauthentication%"=="yes" vmailer.exe out.eml %mailersmtp% %to% %maileraddress% %maileraddress% %mailerpass%
if /i "%useauthentication%"=="no" vmailer.exe out.eml %mailersmtp% %to% %maileraddress%
goto:eof
:end
rem ' Cleanup
del oldmail.txt
del oldmail.sql
del out.*
del del.sql
sample output is:
Targeted emails will be deleted from your email account.
They exceed one or more system policies for domain.com:
- Email older than 5 days in the 'Trash' folder.
--------------------------------------------------------------------
DATE......................FROM....................Subject (partial)
--------------------------------------------------------------------
"20 Nov 2010 16:08:42","
user@domain.com","FW [SPAM] "