Solved

Cfmail tag that has conditional statement?

Posted on 2011-09-26
14
571 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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
 
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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL Memory Keeps Increasing 4 92
Combining Queries 7 54
How to extract database info from current month and year 7 45
Coldfusion builder and IIS 2 39
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

732 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