0
  • Coldfusion 11
  • windows 2012

code:

  <cfmail query="getEmail" 
                to="#getEmail.email#"
                from="#getEmail.displayName# <#getEmail.emailfrom#>"
                subject="#getEmail.emailsubject#" 
                type="html" 
                server="10.1.0.2" 
                timeout="360" 
                username="#myuser#" 
                password="#mypassword#" spoolenable='no' >

    My email content 

    </cfemail>

Im running cfmail tag with query. how can i use isvalid("email",'') in my situation.

IBM
  • 252
  • 1
  • 12
  • 3
    As far as I know, you'll either need to build a filtered query result set first or wrap cfmail in a and get rid of your query attribute. Keep in mind isValid() for email has issues. See https://www.raymondcamden.com/2014/07/21/ColdFusion-isValid-Email-and-new-TLDs/ – Chris Tierney May 02 '16 at 19:24
  • this is the main prob i can't change so much in code its because of production. is there anything i can do ? query coming from MSSQL SP – IBM May 02 '16 at 19:33
  • 2
    Can you change the code that insets the email into the database in the first place? If you tested isValid() before you save the data, you would not need to test it when calling `cfmail`. – Adrian J. Moreno May 02 '16 at 20:59
  • Application code is not necessarily the only way data gets written to databases. However, @AdrianJ.Moreno's suggestion is still a good idea. – Dan Bracuk May 03 '16 at 09:27
  • Update your stored procedure to filter out bad emails using something like this http://stackoverflow.com/questions/423606/t-sql-checking-for-email-format or look for a REGEX example. – Chris Tierney May 03 '16 at 17:21

3 Answers3

2

Try your code inside a cfif tag.

<cfloop query="getEmail">
  <cfif isValid("email", getEmail.email)>
      //Code to send mail
  <cfelse>
      Invalid Email <cfoutput>#email#</cfoutput>.
  </cfif>
</cfloop>

Here, an email you're getting from getEmail query. If it is valid then only email will be sent, else the email won' be sent. Ultimately you will find the list of Invalid emails (if any).

Leigh
  • 28,765
  • 10
  • 55
  • 103
W Gunvant
  • 111
  • 4
1

Maybe u can use regex on your SQL query, try putting this condition

SELECT .... WHERE email NOT REGEXP '^[^@]+@[^@]+\.[^@]{2,}$'

I believe this return only records with valid emails.

let me know if this works

Deivi
  • 197
  • 2
  • 12
0

I discovered that an email address like this...

    <cfset new_Emailaddress = '?é?áomega@testdomain.com'>

    <cfset blnValid = #IsValid("email", new_Emailaddress)# />
    <cfset email_test_result = #YesNoFormat( blnValid )#>

    <cfoutput>#email_test_result#</cfoutput>

(try it, code will output "yes")

BUT the ColdFusion mail server will bomb when attempting a message addressed to ?é?áomega@testdomain.com. (Tested on CF 2016)

"..Attribute validation error for tag CFMAIL.The value of the attribute to, which is currently ?é?áomega@testdomain.com, is invalid. ..."

I want to add to the accepted answer that one can't rely on the isvalid ColdFusion function alone. I suggest one filter unlikely email address characters too.

    <cfset new_Emailaddress2 = ReReplaceNoCase(new_Emailaddress,"[\)|\(|\<|\>|\?|\=|##|\$|\%|\^|\&|\*|\!|\é|\á]","","ALL")> 
    <cfoutput>#new_Emailaddress#</cfoutput><br>
    <cfoutput>#new_Emailaddress2#</cfoutput>