Knowledgebase

How to Create a CFMail Form that Uses​ SMTP Authentication

Posted by on November 24 2013 10:28 AM

Authentication is required at Hostmysite. You can use the mail server assigned to your account as long as you use an email address and password that you've setup on that server. Alternatively, you can use any valid email address and password combination that exists on an external mail server, as long as that server supports SMTP connections and you obtain the server or host name and port information for it. The below example consists of 2 files. The first is the actual form your customers will fill out and the second is the action file which will authenticate and send the email.

**This form can be configured to your liking**



I. Create the Form: Copy the below code into your editor of choice and save it as contactform.cfm
________________________________________

<html>
<cfform name="contactform" method="post" action="send_form_mail.cfm">
<table width="450px">
<tr>
<td valign="top">
<label for="first_name">First Name *</label>
</td>
<td valign="top">
<cfinput type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="last_name">Last Name *</label>
</td>
<td valign="top">
<cfinput type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top"">
<label for="home_address">Home Address</label>
</td>
<td valign="top">
<textarea name="home_address" maxlength="100" cols="25 Rows="6"></textarea>
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email Address *</label>
</td>
<td valign="top">
<cfinput type="text" name="email" maxlength="80" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="telephone">Telephone Number</label>
</td>
<td valign="top">
<cfinput type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
<label for="comments">Comments *</label>
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center"> * - Denotes a required field
<input type="submit" value="Submit">
</td>
</tr>
</table>
</cfform>
</html>

________________________________________

 

II. Create the Send Form Script: Copy the below code and save it as send_form_mail.cfm (do not change the file name unless you changed it in the contactform.cfm code above)
________________________________________

<cfset email_to = "name@yourdomain.xyz">  <!--Enter the email you want to send the form to -->
<cfset email_subject = "This is a test">  <!--You can put whatever subject here -->
<cfset host = "mail.yourdomain.xyz">   <!--The name of your mail server. (Commonly mail.yourdomain.com if your mail is hosted with HostMySite) -->
<cfset username = "name@yourdomain.xyz">  <!--A valid email address you have setup -->
<cfset from_address = "name@yourdomain.xyz">  <!-- If your mail is hosted with HostMySite this has to match the email address above --> 

<cfset password = "your_password">  <!--Password for the above email address -->
<cfset reply_to = "name@yourdomain.xyz">  <!--Enter the email you want customers to reply to -->
<cfset port = "25"> <!--This is the default port. Try port 50 if this port gives you issues and your mail is hosted with HostMySite -->

<cffunction name="Died" access="public" >
    <cfargument name="error" type="string" required="true">
    <cfoutput>#error#</cfoutput>
    <cfabort>
</cffunction>

<cfif NOT Len(Trim(FORM.email)) GT 0 OR NOT Len(Trim(FORM.first_name)) GT 0 OR NOT Len(Trim(FORM.last_name)) GT 0 OR NOT Len(Trim(FORM.comments)) GT 0>
    <cfset die = #Died("Required field/s have not been entered")# >
</cfif>

<cfset errors =''>
<cfset first_name = FORM.first_name> <!-- required -->
<cfset last_name = FORM.last_name> <!-- required -->
<cfset home_address = FORM.home_address> <!-- not required -->
<cfset email_from = FORM.email> <!-- required -->
<cfset telephone = FORM.telephone> <!-- not required -->
<cfset comments = FORM.comments> <!-- required -->

<cfset test = REfind("^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$", email) >
<cfif test EQ 0>
    <cfset errors = errors & "The Email Address you entered does not appear to be valid.<br />" >
</cfif>

<cfset test = REfind("^[A-Za-z .'-]+$", first_name) >
<cfif test EQ 0>
    <cfset errors = errors & "The First Name you entered does not appear to be valid.<br />" >
</cfif>

<cfset test = REfind("^[A-Za-z .'-]+$", last_name) >
<cfif test EQ 0>
    <cfset errors = errors & "The Last Name you entered does not appear to be valid.<br />" >
</cfif>

<cfif LEN(comments) LT 2>
    <cfset errors = errors & "The Comments you entered do not appear to be valid.<br />" >
</cfif>

<cfif LEN(errors) GT 0>
    <cfset die = #Died(errors)#>
</cfif>

<cffunction name="CleanString" access="public" returntype="string">
    <cfargument name="str" type="string" required="true">
    <cfset bad = "content-type,bcc:,to:,cc:,href">
    <cfreturn ReplaceList(str, bad, "")>
</cffunction>

<cfset email_message = "Form details below.<br /><br />">
<cfset email_message = email_message & "First Name: #CleanString(first_name)# " >
<cfset email_message = email_message & "Last Name: #CleanString(last_name)# <br>" >
<cfset email_message = email_message & "Home Address: #CleanString(home_address)# <br>" >
<cfset email_message = email_message & "Email: #CleanString(Email)# <br>" >
<cfset email_message = email_message & "Telephone: #CleanString(telephone)# <br>" >
<cfset email_message = email_message & "Comments: #CleanString(comments)#" >

<!-- This section send the email -->
<cftry>
    <cfmail from="#from_address#" to="#email_to#" subject="#email_subject#" server="#host#" username="#username#" password="#password#" replyto="#reply_to#" port="#port#">
        <cfmailpart type="html">
            #email_message#
        </cfmailpart>
    </cfmail>
    <cfoutput>

        <!-- include your own success message html here -->
        Thank you for contacting us. We will be in touch with you very soon.

    </cfoutput>
    <cfcatch type="any">

        An error has occurred.<br> CATCH: #CFCATCH.type# <br>
        Message: #CFCATCH.message#
    </cfcatch>
</cftry>

________________________________________

 

III. Upload the Files: To test the files you created, FTP to your domain and create a test folder (i.e. formtest). Upload the 2 files to that directory. Once you have uploaded the files, browse to http://www.yourdomain.xyz/formtest/contactform.cfm (or whatever you named the contact form from Step 1)

As long as you used an email address that you are hosting with HostMySite along with the proper host mail server, the email message should have arrived without error.

(10 vote(s))
This article was helpful
This article was not helpful

Comments (0)
Copyright © 2016 HostMySite