• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 607
  • Last Modified:

Cfmail tag that has conditional statement?

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
jasch2244
Asked:
jasch2244
  • 6
  • 5
  • 3
2 Solutions
 
ansudhindraCommented:
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
 
ansudhindraCommented:
How you did is wrong, coldfusion does not allow this type of nestings..
0
 
gdemariaCommented:

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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jasch2244Author Commented:
gdemaria:

What if the FORM.EmailAddress or the FORM.SecondEmail don't exist ( Either / or...I'm using check boxes as form inputs)?
0
 
gdemariaCommented:
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
 
gdemariaCommented:
If neither exist, cfmail will have a problem, ...
0
 
ansudhindraCommented:
"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
 
jasch2244Author Commented:
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
 
gdemariaCommented:
> 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
 
jasch2244Author Commented:
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
 
gdemariaCommented:
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
 
jasch2244Author Commented:
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
 
gdemariaCommented:

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
 
jasch2244Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now