Making A Super Solid Contact Form
Nov192010
Tags: ColdFusion, Databases, Web Programming
If you want to ensure the delivery of your email (or just save the data) from a contact form, here's a technique, to help.
A typical contact form processes a script which sends an email to you with the information that was filled out. See example below:
<cfmail failto="user@example.com" from="user@example.com" password="#request.EmailPassword#"
server="mail.example.com" spoolenable="yes" subject="Contact Form" to="#form.email#" username="user@example.com">
#form.message#
#form.email#
#form.who#
</cfmail>
A better method is to save the information first in a database and then run a script that sends out an email.
<cfquery datasource="#request.dsn#">
INSERT INTO contact_us (email, message, who)
VALUES (
<cfqueryparam cfsqltype="cf_sql_char" value="#form.email#">,
<cfqueryparam cfsqltype="cf_sql_char" value="#form.message#">,
<cfqueryparam cfsqltype="cf_sql_char" value="#form.who#"> )</cfquery>
<!--- After you save the data, shoot the email --->
<cfmail failto="user@example.com" from="user@example.com" password="#request.EmailPassword#"
server="mail.example.com" spoolenable="yes" subject="Contact Form" to="#form.email#" username="user@example.com">
#form.message#
#form.email#
#form.who#
</cfmail>
This method is better because the data is saved first in a database. If the mail server is down, you still have access to the information. The database is used as a fail-safe.
You can then write a script to send you a daily list with all emails to verify that you received everything. On your server, setup a scheduled task to run a script, like so:
<cfquery datasource="#request.dsn#" maxrows="50" name="recordset1">
select * from contact_us
where date > #dateadd(d,-1,"dateformat(now(),"dd/mm/yyyy)")#
</cfquery>
<cfmail failto="user@example.com" from="user@example.com" password="#request.EmailPassword#"
server="mail.example.com" spoolenable="yes" subject="Today's Email List" to="#form.email#" username="user@example.com">
<cfloop query="recordset1">
<cfdump expand="yes" label="text" var="#recordset1#"></cfdump>
</<cfloop>
</cfmail>
If you haven't you can login to your database to check out the emails. The code below creates a simple dump page with the last 50 results from your contact form.
<cfquery datasource="#request.dsn#" maxrows="50" name="recordset1">
select * from contact_us
order by id desc
</cfquery>
<cfoutput><div align="left">
<cfdump expand="yes" label="text" var="#recordset1#">
</cfdump></div></cfoutput>
A user will not will not see a difference between the methods.
Not only does this ensure that you receive all emails, you can also find your emails later in case you accidentally deleted them in your inbox.
This method can also be valuable if your mail server wrongly finds itself on a spam list. It this was to happen your own spam filter may not let the emails through!
There are a ton of different ways (and reasons) to set something like this up. The moral of the story, if you want to ensure that you have the info, save it in a database.

Comments
Post new comment