Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Email looping

Guest
Feb 24, 2009 Feb 24, 2009
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?
559
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 24, 2009 Feb 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 24, 2009 Feb 24, 2009
so then should it be

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

?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 24, 2009 Feb 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 24, 2009 Feb 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 24, 2009 Feb 24, 2009
Kapitaine wrote:
> You don't need CFLOOP or CFOUTPUT at all.
>

Need can be a bit misleading here. I believe my post indicated you can
use <cfmail...> in the manner you advocated.

But sometimes one does not *want* to do it all in the <cfmail...> tag.
I have often needed to do other processing and|or validation for each
record set row before building the email message. In such situations I
have employed <cfloop query=""> blocks around my <cfmail...> blocks.

I have also had cases where I wanted to output content to a browser as
well as the content being generated as an email which requires an
external <cfoutput>...</cfoutput> block to the <cfmail...> block.

I.E.

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

<cfloop query="getCustomers">
<!--- validate email address is correct --->
I have never worked with a database that one could trust all the
emails in it.

<!--- calculate something to be used in the email --->
Maybe I need to incorporate information or calculations that are not
easily done in the database query.

<cfoutput>
Display something indicating progress and|or problems with the
email processing.
</cfoutput>

<cfmail from="somebody@mySite.com"
to="#getCustomers.email#"
subject="Here is your email">
Dear #getCustomers.FirstName#

You wanted to know this #variables.importantValue# fact.
</cfmail>

So yes, CFMAIL can loop a record set and output variable. But sometimes
one needs to do more then just loop and output.

Good day to you sir (or madam)!
Ian. ;-)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 24, 2009 Feb 24, 2009
LATEST
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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources