Once you have created the database, create a DSN pointing to
the database by going to Data Sources (ODBC) in Administrative
Tools on Windows. Now we need to create the actual pages...
Here's the code for the Subscribe and Unsubscribe Page.
Subscribe.cfm
<CFIF IsDefined("Email")>
<!--- Insert Member Information Into
Database --->
<CFINSERT DATASOURCE="YourDSN"
TABLENAME="Newsletter"
Formfields="Name,
Email, Subscribe, EmailType">
<CFOUTPUT>
Thank you #Name#,
Your email address [#Email#] Has been entered into our mailing
list. You'll receive the next mailing!
</CFOUTPUT>
<CFELSE>
<!--- Prompt User To Enter information
--->
<form action="Subscribe.cfm"
method="post">
Name: <input type="Text"
Value="" Name="Name"><BR>
Email: <input type="Text"
Value="" Name="Email"><BR>
Receive in: Text <input type="Radio"
name="EmailType"
value="1">
HTML <input type="Radio"
name="EmailType"
value="0">
<input type="Submit"
Value="Join
Newsletter">
</FORM>
</CFIF>
The unsubscribe page:
unsubscribe.cfm
<CFIF IsDefined("Email")>
<CFQUERY DATASOURCE="YourDSN"
NAME="Unsubscribe">
DELETE
FROM Newsletter
WHERE Email = '#Email#'
</CFQUERY>
<CFOUTPUT>
Thank you,<br>
Your email address [#Email#] Has been removed from our mailing
list. You'll never receive mailing again!
</CFOUTPUT>
<CFELSE>
<!--- Display the unsubscribe form
--->
<form action="unsubscribe.cfm"
method="post">
Name: <input type="Text"
Value=""
Name="Name"><BR>
Email: <input type="Text"
Value=""
Name="Email"><BR>
<input type="Submit"
Value="Join">
</FORM>
</CFIF>
Finally, we need to create a page to send newsletter:
SendNewsletter.cfm
<CFIF IsDefined("Message")>
<CFQUERY NAME="GetMembers"
DATASOURCE="Your
DSN">
SELECT *
FROM Newsletter
<CFIF Form.EmailType EQ "1">
WHERE EmailType = 1
<CFELSE>
WHERE EmailType = 0
</CFIF>
ORDER BY MemberID
</CFQUERY>
<CFLOOP QUERY="GetMembers">
<!--- Determine if email going out
will be in HTML format or not --->
<CFIF #Form.EmailType# EQ
"1">
<CFMAIL To="#GetMembers.Email#"
From="newsletter@mysite.com"
Subject="This
Week's Newsletter!">
#FORM.Message#
Sent: #DateFormat(Now(), 'ddd. mmmm dd, yyyy')#
to Unsubcribe visit: http://www.mysite.com/unsubscribe.cfm
</CFMAIL>
<CFELSE>
<CFMAIL To="#GetMembers.Email#"
From="newsletter@mysite.com"
Subject="This
Week's Newsletter!"
type="HTML">
#FORM.Message#
Sent: #DateFormat(Now(), 'ddd. mmmm dd, yyyy')#
to Unsubcribe visit: http://www.mysite.com/unsubscribe.cfm
</CFMAIL>
</CFIF>
</CFLOOP>
<CFELSE>
<form action="sendnewsletter.cfm"
method="post">
The Message: <Textarea Name="Message"></textarea><BR>
Send as:
Text <input type="Radio"
name="EmailType"
value="1">
HTML <input type="Radio"
name="EmailType"
value="0"><br>
<input type="Submit"
Value="Mail
Newsletter">
</FORM>
</CFIF>
That's it! You've now created a fully interactive newsletter
system for your site!
Originally published at http://tutorial14.easycfm.com/
EasyCFM.Com introduces at least three new tutorials each week,
written by the webmaster (Pablo Varando) and also from individual
people who post their own tutorials for visitors to learn from.
For more information please visit: http://www.easycfm.com
[EasyCFM is Hosted by Colony One On-Line - http://www.colony1.net]