2011-11-26 58 views
2

我收到了一封电子邮件发送到的电子邮件地址列表。邮件功能通过数据库中的列表进行循环,但如果遇到格式错误的电子邮件地址,则会暂停并跳出循环。我尝试使用try/catch来捕获错误,并希望它能够继续循环,但它不能像我所希望的那样工作。代码如下。如果任何人有任何想法,或者可能是一个正则表达式,我可以在循环之前筛选电子邮件地址以筛选出不好的电子邮件地址,那真是太棒了。ColdFusion通过邮件功能尝试抓取循环

谢谢。

<!---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> 

回答

2

你想要什么,通过这些地址是循环,验证它们只为有效条目发送邮件。事情是这样的

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

附:无需将<>放入to

+0

不过要小心。看起来'isValid(“email”,...)'可以拒绝一些有效的电子邮件地址。 – ale

2

您可以尝试首先验证查询中的电子邮件地址。

对我而言,我从来不喜欢让CFMAIL标签管理查询。它似乎总是比它的价值更麻烦。我平时做这样的事情:

<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> 
0

我会亲自循环他们,捕捉错误并继续循环。

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

它可能只是为了清晰起见,但从技术上讲,由于'try/catch'在循环中,因此不需要'continue'。 – Leigh