1

I'm trying to use an old/legacy version of ColdFusion MX7 to submit form results, and does not run PHP. I have it working for just one email. However, I actually need it to send to different emails depending on what someone chooses from a dropdown menu. I just don't know what code to use to send the finished HTML-based form to the correct email.

Code:

<cfif isdefined("FORM.send") and FORM.send eq "Send">  
    <cfmail from="ContactForm" to="contact@simpleform.com" subject="SimpleForm" type="html">
    Name: #FORM.TXTNAME#
    Business Name: #FORM.TXTBUSINESSNAME#
    Email: #FORM.TXTEMAIL#
    Phone: #FORM.TXTPHONE#
    Comment: #FORM.TXTCOMMENT#
    Date / Time Sent: #dateformat(now(), "yyyy/mm/dd")# at #timeformat(now(), "HH:mm:ss tt")#
    </cfmail>
</cfif>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple Contact Form</title>
</head>

<body>
<fieldset>
    <legend>Contact Form</legend>
    <form id="simpleForm" name="simpleForm" method="post" action="">
    <table width="100%" border="0" cellspacing="0" cellpadding="4">
        <tr>
            <td width="20%" align="right">Name:</td>
            <td width="80%"><input type="text" name="txtName" id="txtName" /></td>
        </tr>
        <tr>
            <td align="right">Business Name:</td>
            <td><input type="text" name="txtBusinessName" id="txtBusinessName" /></td>
        </tr>
        <tr>
            <td align="right">Email:</td>
            <td><input type="text" name="txtEmail" id="txtEmail" /></td>
        </tr>
        <tr>
            <td align="right">Phone:</td>
            <td><input type="text" name="txtPhone" id="txtPhone" /></td>
        </tr>
        <tr>
            <td align="right" valign="top">Comment:</td>
            <td><textarea name="txtComment" id="txtComment" cols="45" rows="5"></textarea></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><hr width="100%" size="1" /></td>
        </tr>
      <tr>
          <td>&nbsp;</td>
            <td><input type="submit" name="send" id="send" value="Send" /></td>
      </tr>
    </table>


<SELECT SIZE="1" name="team">
<OPTION>Select your team</OPTION>
<OPTION VALUE="teama" name="teama">Team A</OPTION>
<OPTION VALUE="teamb" name="teamb">Team B</OPTION>
<OPTION VALUE="teamc" name="teamc">Team C</OPTION>
<option value="teamc" name="teamc">Team D</option>
</SELECT>
    </form>
</fieldset>
</body>
</html>
Matt Busche
  • 14,216
  • 5
  • 36
  • 61
Liz Reeder
  • 155
  • 1
  • 1
  • 4
  • once your `form` is submitted you can access `form.team` to determine what the user selected. Is that what you're after? – Matt Busche May 01 '13 at 14:04
  • If the user selects Team A, then the form needs to be emailed to the team A supervisor. Same with teams B-D. I guess the logic would go something like: [insert form results here] – Liz Reeder May 01 '13 at 14:09

1 Answers1

4

What Matt said... basically something like:

<cfif structKeyExists(FORM, "send") and FORM.send eq "Send">  
    <cfswitch expression="#Form.team#">
        <cfcase value="teama">
            <cfset emailTo = "joe.bloggs@example.com">
        </cfcase>
        <cfcase value="teamb">
            <cfset emailTo = "teamB.email@example.com">
        </cfcase>
        ... etc
        <cfdefaultcase> <!--- default if all else fails --->
            <cfset emailTo = "contact@simpleform.com">
        </cfdefaultcase>
    </cfswitch>

    <cfmail from="ContactForm" to="#emailTo#" subject="SimpleForm" type="html">
   ...
    </cfmail>
</cfif>

Also note the use of structKeyExists instead of isDefined - generally considered better practice.

duncan
  • 31,401
  • 13
  • 78
  • 99
  • I added the code to what I already have, but although I'm not getting any errors, I'm also not getting any emails. – Liz Reeder May 01 '13 at 15:36
  • 1
    try changing from "contactform" to from="actual@email.com" coldfusion or your SMTP server is probably moving it to your undeliverable folder. I always wrap my mail statemenets with ---- – steve May 01 '13 at 15:41
  • Tried that, but I was actually getting email when it was still from="contactform", but to="me@email.com". – Liz Reeder May 01 '13 at 15:58
  • Looks like it works. It was simply taking a long time (10min) for the emails to come through. – Liz Reeder May 01 '13 at 16:21
  • In the older versions of CF such as 7, they allowed you to put invalid email addresses into the TO or FROM fields. I believe in 8+, an invalid email address error will be displayed on screen and the email won't send. Glad its working! – steve May 01 '13 at 16:37