Download the EasyCFM.COM Browser Toolbar!
Creating a Newsletter System....

This tutorials will show you how to create a fully automated system to allow visitors to subscribe and unsubscribe to your newsletter. It also shows the administration panel to mass mail all members. Let's Begin:

Here's what's involved.
1) Create the database
2) Create a page for people to sign up.
3) Create a page to unsubscribe.
4) Create a page to send newsletter.

First, lets create the database. Create a table called: Newsletter. Next, create the following fields.

Field Name Field Type
MemberID Autonumber
Name Text
Email Text
EmailType Number

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!

Question? Comments? Email me.... webmaster@easycfm.com

========================================================
Modifications: (10/4/2002)
A bug was brought to my attention by Chad Veldhouse. The bug was within
the "SendNewsletter.cfm" page when sending emails. The Variables have
been modified and this is a working copy. Thanks Chad :)     - Pablo
========================================================

All ColdFusion Tutorials By Author: Pablo Varando