Email Newsletter Subscriptions Using PHP and Dreamweaver

Good PR from Apple: If You're Happy We're Happy
Creative Commons License photo credit: Yandle

I had an interesting email from a reader recently who said that he designs simple sites for non profit organizations and whether or not the Contact Forms Using PHP or ASP would be the way to go for a Email Newsletter opt in?

In my opinion, I wouldn’t use those forms for that purpose but in this article I will show you how you can achieve this through the use of Dreamweaver CS3 and PHP.

NOTE:This is a 2 Part Series so make sure you Subscribe so you don’t miss the next article.

First thing we need to do is create a database to insert the records into. Now that I have XAMPP Installed on my Windows Vista box I can do all this locally. For this example I have created a database called EmailSubscriptions with one table called ESubscriptions.

This table has 4 fields, ID, Name, Email, Subscription. The ID column is the Primary Key and it auto incremented. The Name field is there to hold the name of the user, The Email field is a text field and will hold the users email address and the Subscription field will handle whether the user is subscribed or not, it is a Boolean field and 1 indicates subscribed, 0 means unsubscribed.

Next Step is to open up Dreamweaver and connect to the newly created Database. Here we go.


1. The Applications Panel

In the Applications Panel on the right hand side of Dreamweaver and select the Databases Tab. Cick the + icon and select MySQL Connection. Give the connection a name, pop in the name of the database server (in my case and most cases this will be localhost), provide a username and password of a user that has access to that database, and then select the database from the list by clicking the SELECT Button. Click TEST and see if you have connection. If not then double check you have entered the correct values into the fields.



Let’s no have a go at building the subscription page……

2. Insert a Form and Add Some Fields, Then Create an Insert Record Server Behavior.

Insert a Form onto a New Page and save the page Subscribe.php. Inside the form place a label for Name and a Text Box with an ID if txtName. Also place a label for Email Address and a Text Box with an ID of txtEmail.

We now need to insert a Hidden Field so that the Subscription Field in the database is populated with a value of 1. On the Insert Menu at the Top select Hidden Field. This then inserts a hidden field onto the page.



If you double click the Hidden Field icon on the page the Property Inspector will open up. Give the field a name of Subscription and a value of 1.



Let’s now Create an Insert Record Server Behavior. First we need to create a Binding. So from the Application Tool Pane on the right select the Binding Tab and create a Record Set, give it a name and then select the EmailSubscription Connection you created earlier and there should only be one table in there. Click the TEST button to test the connection.

Then from the Server Behaviour Tab click the + Icon and then select the Insert Record Behaviour. Select the correct Form, Connection, and Table from the drop down menus (there should only be one item in each anyway).

Populate the Database Column With The Correct Data

In the Columns section we need to make sure that the database fields are being populated with the correct data. To do this select the line that starts with Email and then from the Value: drop down menu select FROM.txtEmail. Do the same for Name. The ID is auto generated and the Subscription field is hidden and we have already given that a value. In the After inserting goto text box type in confirm.php and then create a new php page with that name. That is the page the visitor will get after they click the submit button.



Create The Conformation Page

Let’s open up the confirmation page now and add some text to it. Now I want to make this page a bit more personal so I am going to add the users name and email address that they supplied as well to the message. To do that we need to add the fields through a RecordSet.

From the Application Tool Pane > Bindings Tab Create a New Record Set and give it a name, choose your connection, and select the table. Next SORT the ID Column by Descending order. Click Advanced button to open up the advanced sql options.

In the SQL text area you will see the line that reads: ORDER BY ID DESC
add LIMIT 1 to the end of it. What that will do is return just one result and it will be the last one entered. Click OK.

Now place your cursor in the spot that you would like the users name to appear and from the Bindings Tab on the right expand the record set that you created and click on Name and then click INSERT.

Ok, this is where the good stuff starts!…….

Using PHP’s mail() method we can send the email to the end user. The method looks like this:

mail(address, subject, message[, header[,parameters]])

The first 3 parameters are required. Next I want to create a few variables to make things a lot easier.

$id = $row_Subscriber['ID'];
$to = $row_Subscriber[Email'];
$subject = “Subscription confirmation”;

The first 2 pull data from the newly inserted record and the 3rd will be the subject line of the email. Where it says Subscriber, this is the name of your Record Set.

Below is the entire code block that I used, and placed in between the tags.

<?php
$id = $row_Subscriber['ID'];
$to = $row_Subscriber['Email'];
$subject = "Subscription confirmation";
$body = "<html><body>" .
        "<h2>Thank you for subscribing to our newsletter!</h2>" .
        "<p>To unsubscribe, click here.</p>" . "</body></html>";
$headers =  "From: Subscription Manager <someone@mycompany.com>\r\n" .
            "MIME-Version: 1.0\r\n" .
            "Content-type: text/html; charset=UTF-8";
if (!mail($to, $subject, $body, $headers)) {
  header( 'Location: http://[YOUR SERVER & PATH HERE]/error_subscribe.php' ) ;
 }
?>

The line that needs adjusting in the one that starts with header. This will be the page that the user gets taken to if there is an error sending the email. So create a page for this and put the path in there.

There we have it. Now I will let you know that I tested this using Blue Host so if you are using a different host I would love you to share with the readers which host you used and what the outcome was.

In the Follow Up Article I will go through how you can let the user unsubscribe from the list so make sure you SUBSCRIBE to the RSS Feed to get that one.


| del.icio.us | Digg it | Furl | reddit | StumbleUpon | Yahoo MyWeb

You can follow any responses to this entry through the RSS feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

2 Responses to “Email Newsletter Subscriptions Using PHP and Dreamweaver”

  1. Hi Daniel,

    You’re a cornucopia of web development info! I don’t see your About page but I’m guessing you’re a developer - great job with your tutorials, you’d make a good teacher. :)

  2. thank you for your incite on this program you have been lots of help to me.

Leave a Reply