Skip to main content
February 24, 2009
Question

Email looping

  • February 24, 2009
  • 3 replies
  • 632 views
If I wish to get a listing of email addresses from a database and email them will this work?

<cfloop query="email_bulletin"><cfoutput>
.....
</cfoutput></cloop>

if not what would be the best way to do this?
    This topic has been closed for replies.

    3 replies

    February 24, 2009
    Heya Ian,

    You are of course right, and I would personally go down your route, but in terms of just getting it to work, the CFMAIL tag is enough.

    But yes, as you said, validation may be very important (and I would always recommend it), especially as the email may not be a valid email (highly likely if user entered), and I'm not sure what CFMAIL would do with this - probably error. So sure, I would do something like:

    <cfloop query="something">
    <cfif isValid(something.email,"email")>
    <cfmail ...>email here</cfmail>
    <cfelse>
    Invalid user email.
    </cfif>
    </cfloop>

    It's not that any one version was wrong, we were all right. But it's nice to see everyones points on this.

    Good times! :)
    Mikey.
    Inspiring
    February 24, 2009
    Dan Bracuk wrote:
    > It won't work because cfmail is effectively the same as cfoutput. Your code
    > will crash because of nested cfoutputs.

    Actually Dan is incorrect. Your original code would have worked,
    because you can nest query loops with the <cfloop...> tags. You can't
    nest <cfoutput query=""> loops.

    I would have written it this way personally.

    <cfoutput>
    <cfloop query="">
    </cfloop>
    </cfoutput>

    There is a small amount of overhead for ColdFusion to open and close
    <cfoutput...> blocks. So as a rule of thumb, I minimize that when possible.

    But Dan is correct that the <cfmail...> tag has a parameter to handle
    query record sets that will handle the looping itself. Thus, the above
    code may be unnecessary. But sometimes it is still relevant in that
    this allows other processing or checking of the data before the email is
    generated.

    Finally the <cfoutput> may also be unnecessary as the <cfmail...> tag
    will server the same purpose to render any variables inside the
    <cfmail...>...</cfmail> block. So you would only need the surrounding
    <cfoutput>...</cfoutput> block, if you wanted to output other content,
    outside of the email body being generated.
    February 24, 2009
    You don't need CFLOOP or CFOUTPUT at all.

    Just do:

    <cfquery name="GetCustomers" datasource="myDSN">
    SELECT * FROM Customers
    </cfquery>

    <cfmail query="GetCustomers"
    from="service@MyCompany.com"
    to="#GetCustomers.EMail#"
    subject="Contact Info Verification">

    Dear #GetCustomers.FirstName#...

    </cfmail>

    CFMAIL can loop query data AND output variables like a CFLOOP and CFOUTPUT combined.

    Good day to you sir!
    Mikey.

    Inspiring
    February 24, 2009
    It won't work because cfmail is effectively the same as cfoutput. Your code will crash because of nested cfoutputs.

    The best way to do it is with one of the attributes of the cfmail tag. They are all in the cfml reference manual. If you don't have one, the internet does.

    February 24, 2009
    so then should it be

    <cfmail to="#email_bulletin.email#"
    query=email_bulletin
    from="1@2.com"
    subject="blah">

    ?