Solved

Cfmail tag that has conditional statement?

Posted on 2011-09-26
14
559 Views
Last Modified: 2012-05-12
I'm using a cfmail tag with a cfif statement in the body of the tag... Coldfusion does not seem to like that. My goal is to use some conditional logic to determine if a particular form field is present in a submission. Any thoughts on how to make this happen? I'm using Coldfusion 8 and mysql 5.0

<cfmail 
server = "########"
username = "####"
password = "#######"
subject="#Form.subject#"
to='#FORM.EmailAddress#<cfif len(trim(FORM.SecondEmail)) gt 0>,#FORM.SecondEmail#</cfif>'
cc = "#FORM.CCemail#"
bcc="#FORM.bccemail#"
from='"Person Sending" <email@email.com>'
type="HTML">

Open in new window

0
Comment
Question by:jasch2244
  • 6
  • 5
  • 3
14 Comments
 
LVL 13

Accepted Solution

by:
ansudhindra earned 250 total points
ID: 36598619
make it like this....
 
<cfset toEmailIds=FORM.EmailAddress />
<cfif len(trim(FORM.SecondEmail)) gt 0>
    <cfset toEmailIds=toEmailIds & "," & FORM.SecondEmail />
</cfif>

<cfmail 
server = "########"
username = "####"
password = "#######"
subject="#Form.subject#"
to='#toEmailIds#'
cc = "#FORM.CCemail#"
bcc="#FORM.bccemail#"
from='"Person Sending" <email@email.com>'
type="HTML">

Open in new window

0
 
LVL 13

Expert Comment

by:ansudhindra
ID: 36598630
How you did is wrong, coldfusion does not allow this type of nestings..
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 36599065

Here's an even easier way of doing it..   using listAppend()  will put two items together seperated with a comma if both elements have a value.    In short, no CFIF needed...




<cfmail 
server = "########"
username = "####"
password = "#######"
subject="#Form.subject#"
to="#listAppend(FORM.EmailAddress, FORM.SecondEmail)#"
cc = "#FORM.CCemail#"
bcc="#FORM.bccemail#"
from="Person Sending <email@email.com>"
type="HTML">
v

Open in new window

0
 
LVL 1

Author Comment

by:jasch2244
ID: 36599842
gdemaria:

What if the FORM.EmailAddress or the FORM.SecondEmail don't exist ( Either / or...I'm using check boxes as form inputs)?
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 36599862
If either one doesn't exist, the result will be just the one email address, if both exist, then you are good too as the result will be both items with a comma in between.   If neither exist, cfmail if have a problem, so if that is possible, you will want to test the values before you get to cfmail
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 36599863
If neither exist, cfmail will have a problem, ...
0
 
LVL 13

Expert Comment

by:ansudhindra
ID: 36599880
"to" is a required field of cfmail tag... you have to provide one value for that.
so make forms EmailAddress as a required field.
 
<cfset toEmailIds=FORM.EmailAddress />
<cfif isdefined('FORM.SecondEmail') and len(trim(FORM.SecondEmail)) gt 0>
    <cfset toEmailIds=toEmailIds & "," & FORM.SecondEmail />
</cfif>

<cfmail 
server = "########"
username = "####"
password = "#######"
subject="#Form.subject#"
to='#toEmailIds#'
cc = "#FORM.CCemail#"
bcc="#FORM.bccemail#"
from='"Person Sending" <email@email.com>'
type="HTML">

Open in new window

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:jasch2244
ID: 36712115
ansudhindra:

Thank you this solution works great. My only reservation (even though you answered the question) is if the user selects the SecondEmail as the option and no the EmailAddress option it will fail stating there is no FORM.EmailAddress. Whould you use a catch statement for something like this? My head is spinning trying to think of the logic on this one :)

gdemaria:

The listAppend does not work in the cfmail tag for some reason.. the end user has to select both options for that to work


0
 
LVL 39

Expert Comment

by:gdemaria
ID: 36712197
> The listAppend does not work in the cfmail tag for some reason.. the end user has to select both options for that to work

Huh?   ... what does "not work" mean?  The result is empty, you get an error, you only get one email address?

There must be a little bug somewhere, can you just place it before the cfmail and see what you are getting...


<cfoutput>
  Email -1 : #FORM.EmailAddress#  <br>
  Email -2 : #FORM.SecondEmail#  <br>
  Both : #listAppend(FORM.EmailAddress, FORM.SecondEmail)#  <br>
</cfoutput>
<cfabort>

<cfmail
server = "########"
username = "####"
password = "#######"
subject="#Form.subject#"
to="#listAppend(FORM.EmailAddress, FORM.SecondEmail)#"
cc = "#FORM.CCemail#"
bcc="#FORM.bccemail#"
from="Person Sending <email@email.com>"
type="HTML">
v
0
 
LVL 1

Author Comment

by:jasch2244
ID: 36712332
Yes i'm getting: "Element SECONDEMAIL is undefined in FORM." I did want you mentioned and I'm getting the response for what check box is checked... if the second check box is left unchecked I get the error mentioned above. The form is not present because it is a check box.. if check box is unchecked no check box form element gets submitted.
0
 
LVL 39

Assisted Solution

by:gdemaria
gdemaria earned 250 total points
ID: 36712400
lol, you're supposed to use cfparam for checkboxes... it's not the fault of listAppend()

--- you should do this with any checkbox
<cfparam name="form.secondEmail" default="">

.. also, there is no reason to have two different named checkbox tags!

Your form field should just be this...

<input type="checkbox" name="emailaddress" value="#getList.email" >  #getList.Email#

<input type="checkbox" name="emailaddress" value="#getList.email" >  #getList.Email#

<input type="checkbox" name="emailaddress" value="#getList.email" >  #getList.Email#

<input type="checkbox" name="emailaddress" value="#getList.email" >  #getList.Email#

Then in the form field, FORM.EmailAddress you will already have a comma delimited list of email addresses, no need to appen different fields... that's how checkboxes work..
0
 
LVL 1

Author Comment

by:jasch2244
ID: 36713767
Ok, there is a reason as this is how the fields are set up in the database EmailAddress (record) and SecondEmail as this is how it was designed... in other words I can't query a list as there are only two spots for two e-mails. Thanks for teh cfparam suggestion though.
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 36713859

Ok, but it doesn't matter if they are stored as two fields, you can easily send them over as one...

  <input type="checkbox" name="emailaddress" value="#getList.emailAddress" >  #getList.emailAddress#

  <input type="checkbox" name="emailaddress" value="#getList.SecondEmail" >  #getList.SecondEmail#


Just an FYI to keep things simple.

either or both of the emails above will come over in the variable FORM.EmailAddress

0
 
LVL 1

Author Closing Comment

by:jasch2244
ID: 36714339
Both of these solutions will work just fine thank you both

gdemaria:
You were so correct thank you for taking the time to show me something different and to make me always remember "keep it simple".
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
Creating and Managing Databases with phpMyAdmin in cPanel.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now