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

24 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.

  3. the mail part does not work

  4. tosin adenuga Says:

    thanx a lot i got thru the tutorials but wasnt really clear and practical.
    it gooes thus
    - i have a form for which i have created,purposely to be sent to an email in box.(macromedia dreamweaver)
    -i need to secure the informations in the form,and give a return msg of form being successfully sent else error page.
    -how do i go about this using php,where do i embed the php code,how does the information in the various fields get catured and sent into the specified inbox.
    pls tutor me on the practical step step procedures to take.
    thank you

  5. [...] addthis_pub = ‘ultra’; addthis_brand = ‘Ultrashock.com’; addthis_options = ‘favorites, google, delicious, digg, facebook, fark, furl, live, myweb, myspace, newsvine, reddit, slashdot, stumbleupon, technorati, twitter, more ‘; Dreamweaver, PHP, and MySQL 1 Minute Ago I have been doing an online tutorial for email newsletter subscriptions using Dreamweaver, PHP, and MySQL. My only problem is that when it sends a confirmation email it sends it twice to the user. I can’t seem to figure out what I am doing wrong. I have provided the link below if anyone wants to walk through and do the tutorial. I have done the tutorial three times and I still get two confirmation emails. http://dreamweaverspot.com/email-new…aver/#more-156 [...]

  6. Hey, great tutorial.

    Before I start implementing this on my project I´d like to ask if I only want to collect e-mail address and not names or anything else. Could I just strip out these lines of code and use the rest?
    Also How will a site owner access the records? Perhaps that is explained already but if the site owner is a non tecchie is there some accessible way for her to see the records of the database?

    Appreciate your site
    /Jake

  7. I get two confirmation emails instead of one!

  8. hey,
    I’m not too familiar with creating databases so getting a little stuck creating the 4 fields for the entry (ID, Name, Email, Subscription).

    I have my phpadmin open, my database is created and I’ve gotten to the stage to edit my 4 table fields.

    I’m not sure what you put for, Lengt/values, Collation, Attributs, Null, Default, Extra, and have 3 options to choose, Primary, Index, and Unique.

    I undertand for ID, its auto_increment in the drop down menu in extra, and its a primary. But thats about it.

    Are all types except for Email VARCHAR?

    Hope this is not too much to ask.
    Thanks for your time.

    Oliver

  9. Good posting.Do u want to see Auto Forum and Community ? u can visit my blog friend :D.ow yeah your blog has alread bookmarked by me

  10. Very well written post however, I would recommend that you turn the No Follow off in your comment section.

    Keep up the good work.

  11. Thanks a whole lot i find your tutorial helpful for a starter now i can learn my php peacefully . I Found many tutorials but yours had clarity and easy to follow In an hours time i was up and running.Many thanks.

  12. It was interesting. You seem very knowledgeable in ypour field.

  13. First time checking out this blog, but I got to say I’ll probably be here more often

  14. I was hoping I could ask you a rhetorical question about yourself, would that be ok? Its always good to share a great post.

  15. It was interesting to browse trough :-) keep up the good work and thanks for sharing. I will be checking regulary now as the stuff here on your site looks to be very helpful. Good myspace stuff.

  16. Good stuff, bookmarked for further reading.

  17. I normally don’t leave comments but I like this show sooo much! I bookmarked your site on dig!

  18. A mate encoraged me to read this site, nice post, interesting read… keep up the nice work!

  19. This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article!

  20. compelling read. Have a extremely good month!

  21. Wonderful site, I really discovered it to be interesting. I’m looking forward to coming back once again to learn what is recent.

  22. Great looking Blog! Found it through Yahoo. Just as an FYI, it didnt display right when I opened it in the Opera Interet Browser.

  23. I like what you have to say

  24. Hi sweetie, nice website! I really appreciate this article.. I was curious about this for a long time now. This cleared a lot up for me! Do you have a rss feed that I can add?

Leave a Reply