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
========================================================

About This Tutorial
Author: Pablo Varando
Skill Level: Intermediate 
 
 
 
Platforms Tested: CF5
Total Views: 63,675
Submission Date: September 06, 2002
Last Update Date: June 05, 2009
All Tutorials By This Autor: 47
Discuss This Tutorial
  • I've used this to build email lists but found something lacking - I can add any email address to this and that person is subscribe weither they want it or not. How about adding something that emails them a message first before adding their email to the database. In the message they will have to click on a link to actually have their email added to the database. Sort of a "did you request this" confirmation.

  • like, that these are tutorials, made to learn from, not complete, fully featured applications to download and use because we're lazy? seems like every good tutorial I read, there are people complaining that they can't download it or it doesn't have a feature they want.... sheesh.

  • Your Newsletter system is not working, I have my domain on linux environment, it shows no error but does not actually send email, ehat to do????????????????

  • How do you prevent users from subscribing multiple times with the same email address to the newsletter system?

  • What if you had 1000 emails to send - would CF abort part way thru or send partial messages?

  • I can't believe its this simple to create a newsletter system with ColdFusion. I'm a newb and it only took like 10 minutes... Nice tutorial!

  • Could you please add some explanations for the subscribe, unsubscribe, and sendNewsletter pages? Thanks for another interesting tutorial.

Advertisement

Sponsored By...
Powered By...