Sending cf mail from a static page to single recipient

155 views Asked by At

Trying to send <cfmail> from a contact us static page. It will have a single recipient and I don't want to save it on the back end.

<cfcase value="contact">
        <cfset caller.mainTitle = "Contact Us">

        <div id="contact_form">
            <cfform method="post" action="contact2" id="usrform">
                First Name<br>
                <input class="textbox" type="text" name="firstName" value="First Name" onfocus="if (this.value=='First Name') this.value='';">
                    <br>
                Last Name<br>
                <input class="textbox" type="text" name="lastName" value="Last Name" onfocus="if(this.value=='Last Name') this.value ='';">
                    <br>
                Email<br>
                <input class="textbox" type="text" name="email" value="Email" onfocus="if(this.value=='Email') this.value='';">
                    <br>
                Phone Number<br>
                <input class="textbox" type="text" name="phone" value="Phone Number" onfocus="if(this.value =='Phone Number') this.value='';">
                    <br>

                <input type="submit" class='submitBtn'>
            </cfform>
            <br>
        </div>
        <div class="commentsTop">
            <p style="color:black; font-size:18px; text-align:left;">We would love to hear from you!<p><br>
            <textarea class="comments" rows="10" cols="100" name="comment" form="usrform" onfocus="if(this.value=='Enter your message here...') this.value='';">Enter your message here...</textarea>
        </div>


    </cfcase>
  <cfcase value="contact2">

        <cfmail to="[email protected]" from="[email protected]" Subject="Message From Contact Us" type="HTML">


</cfmail>

</cfcase>

I have a form that I want to have attached as the body of the email. Wasn't sure if I need to have the form as a <cfform> or if that doesn't matter.

2

There are 2 answers

3
Keshav jha On BEST ANSWER

Here is what I will do:

I will use Normal html form (cfform is also fine) Give action to form (it can be same page or you can have separate submit page.)

On submit page I will write logic to send mail.(if its simple mail sending and nothing complex is happening then cfm page is fine otherwise CFC is preferred)

Contactus.cfm

<form method="post" action="submitform.cfm" id="usrform">
    First Name<br>
    <input class="textbox" type="text" name="firstName" value="First Name" onfocus="if (this.value=='First Name') this.value='';">
    <br>
    Last Name<br>
    <input class="textbox" type="text" name="lastName" value="Last Name" onfocus="if(this.value=='Last Name') this.value ='';">
    <br>
    Email<br>
    <input class="textbox" type="text" name="email" value="Email" onfocus="if(this.value=='Email') this.value='';">
    <br>
    Phone Number<br>
    <input class="textbox" type="text" name="phone" value="Phone Number" onfocus="if(this.value =='Phone Number') this.value='';">
    <br>
    <input type="submit" class='submitBtn'>
</form>

Submitform.cfm

Make sure you are passing correct credential and server details in cfmail

<cfmail to="[email protected]" from="[email protected]" Subject="Message From Contact Us" type="HTML">

<!--- Your message body (you can use your form variable here) --->
FistName: #form.firstName#
LastName: #form.lastName#

</cfmail>
0
James A Mohler On

One file solution

<form method="post" action="?">
    First Name<br>
    <input class="textbox" type="text" name="firstName" value="First Name" onfocus="if (this.value=='First Name') this.value='';">
    <br>
    Last Name<br>
    <input class="textbox" type="text" name="lastName" value="Last Name" onfocus="if(this.value=='Last Name') this.value ='';">
    <br>
    Email<br>
    <input class="textbox" type="text" name="email" value="Email" onfocus="if(this.value=='Email') this.value='';">
    <br>
    Phone Number<br>
    <input class="textbox" type="text" name="phone" value="Phone Number" onfocus="if(this.value =='Phone Number') this.value='';">
    <br>


    <p style="color:black; font-size:18px; text-align:left;">We would love to hear from you!<p><br>
        <textarea class="comments" rows="10" cols="100" name="comment"  onfocus="if(this.value=='Enter your message here...') this.value='';">Enter your message here...</textarea>

    <input type="submit" class='submitBtn'>
</form>


<cfif cgi.request_method EQ "post"> 

  <cfmail to="[email protected]" from="[email protected]" Subject="Message From Contact Us" type="HTML">

    <!--- Your message body (you can use your form variable here) --->
    <cfloop index="i" list="#Form.FieldNames#" delimiters=",">
      #i# = #Form[i]#<br>
    </cfloop>
 </cfmail>

</cfif>

Note: the Comments field was not inside of the form

Also see:

Display CFLoop Items in Order from Form