I’ve had an issue come up several times in the last month where I’ll get several thousand emails from sites on errors or notifications or whatever. I have no choice but to be on the list to receive the emails and I was sick of trying to manage all of them (most of them just got deleted anyway) so I thought I’d write a script to do the deleting for me. You can see the script below (with certain data such as usernames and passwords removed for privacy):
<Cfsetting requesttimeout="330">
<cfparam name="url.startRow" default="1">
<cfparam name="url.maxRows" default="1000">
<cfparam name="url.action" default="FIND">
<cfset mailToDeleteFile = "absolute path to your mail file (I used mailToDelete.txt in the same directory as this script)">
<cfset lFromItemsToDelete = List of FROM items to delete (i.e. error@domain.com)>
<cfoutput>
Action: <u>#url.action#</u><br />
Start Row: <u>#url.startRow#</u><br />
Max Rows: <u>#url.maxRows#</u><br />
<cfif fileExists(mailToDeleteFile)>
<cffile action="read" file="#mailToDeleteFile#" variable="fileRead">
Messages marked for deletion: <u>#ListLen(fileRead,Chr(13)&Chr(10))#</u><br />
Delete Messages File: mailToDelete.txt
</cfif>
</cfoutput>
<cfflush />
<cfif url.action eq "find">
<cftry>
<cfif url.startRow eq 1 AND FileExists(mailToDeleteFile)>
<cffile action="delete" file="#mailToDeleteFile#" />
</cfif>
<cfcatch type="any"></cfcatch>
</cftry>
<cftry>
<cfpop action="getHeaderOnly" server="my_mail_server" timeout="300" username="my_username" password="my_password" maxrows="#url.maxRows#" startrow="#url.startRow#" name="popResults">
<cfloop from="1" to="#popResults.recordcount#" step="1" index="i">
<cfif ListFindNoCase(lFromItemsToDelete, popResults.from[i])>
<cffile action="append" file="#mailToDeleteFile#" output="#popResults.uid[i]#">
</cfif>
</cfloop>
<cfcatch type="any">
<cfset url.startRow = url.startRow - url.maxRows>
</cfcatch>
</cftry>
<cfoutput>
<cfif popResults.recordcount LT url.maxRows>
<cfset url.action = "DELETE">
<cfset url.maxRows = 100>
<cfset url.startRow = 1>
</cfif>
<meta http-equiv="refresh" content="1,index.cfm?startRow=#url.startRow+url.maxRows#&maxRows=#url.maxRows#&action=#url.action#">
</cfoutput>
<cfelseif url.action eq "delete">
<cftry>
<cffile action="read" file="#mailToDeleteFile#" variable="fileRead">
<cfset uidsToDelete = ">
<cfloop from="#url.startRow#" to="#url.startRow+url.maxRows-1#" step="1" index="i">
<cfif i LTE ListLen(fileRead,Chr(13)&Chr(10))>
<cfset uidsToDelete = ListAppend(uidsToDelete,ListGetAt(fileRead,i,Chr(13)&Chr(10)))>
</cfif>
</cfloop>
<cfpop action="delete" server="my_mail_server" timeout="300" username="my_username" password="my_password" uid="#uidsToDelete#">
<cfcatch type="any">
<cfif NOT FindNoCase('Invalid list index',cfcatch.Message)>
<cfset url.startRow = url.startRow - url.maxRows>
</cfif>
</cfcatch>
</cftry>
<cfif isDefined('fileRead') AND url.startRow LTE ListLen(fileRead,Chr(13)&Chr(10))>
<cfoutput>
<meta http-equiv="refresh" content="1,index.cfm?startRow=#url.startRow+url.maxRows#&maxRows=#url.maxRows#&action=#url.action#">
</cfoutput>
<cfelse>
<cfoutput>
<br /><br /><h2>Finished!</h2>
</cfoutput>
<cftry>
<cfif FileExists(mailToDeleteFile)>
<cffile action="delete" file="#mailToDeleteFile#" />
<cfoutput>
<meta http-equiv="refresh" content="1,index.cfm?startRow=#url.startRow+url.maxRows#&maxRows=#url.maxRows#&action=#url.action#">
</cfoutput>
</cfif>
<cfcatch type="any"></cfcatch>
</cftry>
</cfif>
</cfif>
When reading the file, you can see that basically it just goes through and pops your mail server 1000 messages at a time and if it finds someting in the lFromItemsToDelete it adds that message’s uid to a file. So, if you are getting messages from the email address ‘errors@domain.com’ add that and any other items in standard list format.
After it’s found all the messages to delete, it goes through and deletes them 100 at a time. The script takes care of itself so if you just browse to it, it’ll start finding messages and then start deleting them automatically once they are all found. Hope this helps save someone the time I spent sifting through junk emails.

Related Articles
No user responded in this post
Leave A Reply