coldfusionrailocfmail

ColdFusion Try Catch on loop through mail function


I've got a list of email addresses that an email is sent to. The mail function loops through the list from the database but if it encounters a malformed email address, it halts and breaks out of the loop. I've tried using try/catch to catch the error and was hoping it'd continue through the loop but it's not worked as I'd hoped. The code is below. If anyone has any ideas, or maybe a regex that I can sift through the email addresses before the loop to filter out bad ones, that'd be awesome.

Thanks.

    <!---Try to send the mail(s)--->
<cftry>
    <cfmail to="<#Auctioneer.email#>" from="#emailSite#" subject="#Email.subject#" server="#emailServer#" query="Auctioneer" type="html">
        <!---Some email content--->
    </cfmail>

    <cfcatch type="Application">
        <cflog text="#cfcatch.detail#" file="mail" type="Error" application="yes">
        <cfmail to="admin@website.co.uk" from="#emailSite#" subject="Invalid E-Mail Address" type="html">
            Email address not valid error.
            #Auctioneer.email#
            <cfdump var="#cfcatch.detail#">
        </cfmail>
    </cfcatch>
</cftry>

Solution

  • What you want is to loop through the addresses, validate them and send mails only for valid entries. Something like this

    <cfloop query="getEmails">
        <cfif isValid("email", Auctioneer.email)
        ...send valid email...
        <cfelse>
        ...send invalid email, or better log in database...
        </cfif>
    </cfloop>
    

    P.S. No need to put <> in to.