i-CONICA
i-CONICA

Reputation: 2379

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="[email protected]" from="#emailSite#" subject="Invalid E-Mail Address" type="html">
            Email address not valid error.
            #Auctioneer.email#
            <cfdump var="#cfcatch.detail#">
        </cfmail>
    </cfcatch>
</cftry>

Upvotes: 2

Views: 1438

Answers (3)

James Hull
James Hull

Reputation: 3689

I would personally loop through them, catch the error and continue the loop.

for(var i = 1; i < Auctioneer.recordCount; i++) {
    try {
        //send email
    } catch (Any e) {
        //log
        continue;
    }
}

Upvotes: 0

Sergey Galashyn
Sergey Galashyn

Reputation: 6956

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.

Upvotes: 2

ale
ale

Reputation: 6430

You could try validating the e-mail addresses in the query first.

For me, though, I never liked having the CFMAIL tag manage the query. It always seemed to cause more trouble than it's worth. I usually do something like this:

<cfoutput query="Auctioneer">
  <cftry>
    <cfmail to="#email#" from="#variables.emailSite#" subject="#variables.subject#" server="#application.emailServer#" type="html">
      <!---Some email content--->
    </cfmail>

    <cfcatch type="Application">
        <cflog text="#cfcatch.detail#" file="mail" type="Error" application="yes">
        <cfmail to="[email protected]" from="#variables.emailSite#" subject="Invalid E-Mail Address" type="html">
            Email address not valid error.
            #email#
            <cfdump var="#cfcatch.detail#">
        </cfmail>
    </cfcatch>
  </cftry>
</cfoutput>

Upvotes: 2

Related Questions