A Quick Fix For Undelivered Mail

Posted: December 20th, 2012 | Author: | Filed under: ColdFusion | 2 Comments »

Hello once again ColdFusion fans. I wish I had more time to blog about my adventures over the last six months (there have been many), but alas I do not. I was able to take a few minutes to share this with you, hope you find it helpful…

One of the reasons why I love ColdFusion is that it allows me to solve problems quickly. Today I ran into an issue where we had to move to a new mail server. I had HUNDREDS of email messages in the undelivered folder that needed to be re-sent after the move was completed.

After moving to the new mail server and attempting to move the messages to the Spool folder the messages kept re-appearing in the undelivered folder. Even though I specified a new mail server in ColdFusion Administrator the old messages were still tied to the old server and were bouncing back.

So I wrote this code to solve the problem. Feel free to co-opt it, take full credit for it, add this feature to Ray Camden‘s SpoolMail project (I did swipe a few lines of his code from SpoolMail for this) or get a face tattoo of the source code.

<cfscript>
	// declare variables 
	request.emailMessage = "";
	request.maildir = server.coldfusion.rootdir & "/Mail/Undelivr/";
	request.messagesListQuery = "";
	request.newMailServerAndPort = "new.servername.goes.here:25";
	request.oldMailServerAndPort = "old.servername.goes.here:25";
	request.spooldir = server.coldfusion.rootdir & "/Mail/Spool/";
</cfscript>
 
<!--- get the undelivered messages --->
<cfdirectory action="list" 
	name="request.messagesListQuery" 
	directory="#request.maildir#" 
	filter="*.cfmail" 
	sort="datelastmodified desc">
 
<!--- loop through the list of messages --->
<cfoutput query="request.messagesListQuery">
	<!--- read a message --->
	<cffile action="read" file="#request.maildir#/#name#" variable="request.emailMessage">
	<!--- if the old mail server is present in the email... --->
	<cfif FindNoCase(request.oldMailServerAndPort,request.emailMessage)>
	<!--- ...then swap it out with the new mail server --->
	<cfset request.emailMessage = 
		ReplaceNoCase(request.emailMessage, request.oldMailServerAndPort, request.newMailServerAndPort) />
	<!--- write the file to the spool directory + delete the old message --->
	<cffile action="write" file="#request.spooldir#/#name#" output="#request.emailMessage#">
	<cffile action="delete" file="#request.maildir#/#name#">
	</cfif>
</cfoutput>
<!--- display the results --->
<cfoutput>
	#request.messagesListQuery.recordcount# messages that were set to be delivered to server 
	#request.oldMailServerAndPort# have will now be re-sent using server 
	#request.newMailServerAndPort#.  Have a nice day.
</cfoutput>

Once again ColdFusion helped me to quickly solve a problem. Face tattoo sold separately.


2 Comments on “A Quick Fix For Undelivered Mail”

  1. 1 Steve S said at 1:48 pm on December 21st, 2012:

    Genius Chris, we had a similar issue, but didn’t even think to script the change. We could have even done it right in our mailspool script! Awesome idea.

  2. 2 Christopher Vigliotti said at 3:44 pm on January 2nd, 2013:

    Thanks Steve. I manually moved the first 20 messages in SpoolMail before the lightbulb went off.


Leave a Reply