Build a Newsletter System With PHP and MySQL: New Plus Tut

Build a Newsletter System With PHP and MySQL: New Plus Tut

Jul 8th in News by Alex Coomans

Today, we are going to be building a newsletter system using PHP with a MySQL background. The tutorial will cover building a system that allows for multiple newsletter lists and the sending of messages to specific lists. Join today!

PG

Author: Alex Coomans

This is a NETTUTS contributor who has published 4 tutorial(s) so far here. Their bio is coming soon!

Example
Example

Join Tuts Plus

For those unfamiliar, the family of TUTS sites runs a premium membership service called "TUTSPLUS". For $9 per month, you gain access to exclusive premium tutorials, screencasts, and freebies at nettuts+, psdtuts+, and vectortuts+! For the price of a pizza, you'll learn from some of the best minds in the business. Join today!

  • Subscribe to the NETTUTS RSS Feed for more daily web development tuts and articles.


Related Posts

Check out some more great tutorials and articles that you might like

Enjoy this Post?

Your vote will help us grow this site and provide even more awesomeness

Plus Members

Source Files, Bonus Tutorials and
More for $9 a month for all TUTS+
sites in one subscription.

Join Now

User Comments

( ADD YOURS )
  1. PG

    Zy July 8th

    Wish I could have a plus account…

    ( Reply )
    1. PG

      Web010 July 8th

      Me too. But i’m from Serbia, no PayPal availability for me :(

      ( Reply )
    2. PG

      Pixie July 9th

      Also wish i had a plus account. Same here in Kenya, no paypal. This tutorial looks well worth it though, its exactly what i need for a client, have been searching the web for a comprehensive all in one like this one.

      ( Reply )
    3. PG

      Supreet July 11th

      same.. i wish i could have had a tuts account.. am a student from india with no job right now thus no money to pay for it.. but these tutorials sure could have helped me..

      ( Reply )
  2. PG

    Lucci July 8th

    i want buy this +Tut with a SMS-Message! fix this please!

    ( Reply )
  3. PG

    pixelsoul July 8th

    I would do a plus account but I don’t see enough action there to justify $9 a month. The tutorials look like they are done with a lot of quality though.

    ( Reply )
    1. PG

      leo July 30th

      Exactly i think it should be cheaper $9 dollar a month is quiet allot when it comes to member from South america, Asian etc…

      ( Reply )
  4. PG

    Paul du Long July 8th

    I quit my PLUS account, you get 4 PLUS tutorials a month. Most of the time they are all the same.

    ( Reply )
    1. PG

      Anthony Woods July 8th

      Sorry but i care to disagree, each PLUS tutorial i’ve read has been significantly different in terms of idea and how to achieve the idea.

      Thanks for yet another good PLUS tutorial nettuts, will be sure to give it a read through when I get the chance.

      @anthonywoods

      ( Reply )
      1. PG

        Paul du Long July 8th

        It is most of the time the same standard stuff PHPandn MYSQL (INSERT, UPDATE, DELETE). I joined because of the unique tuts, but everywhere on the net i can find Newsletter tuts and create a own CMS. There are few things that make this tut unique because it isn’t OOP or written in MVC pattern.
        I also miss more PRO tutorial like; Smarty, CodeIgniter, MCV, OOP, Communication (instant messenger and chat (irc)) stuff like that.
        For beginners this is a top site but for a pro it isn’t interesting anymore :(

      2. PG

        Brad July 11th

        If you’re so pro Paul, why do you care how quality the tutorials are? I’m always astonished to see that so many people gripe about $9 a month.

        If you cared even a tiny bit about your trade, you’d be willing to make an investment in your education without hesitation. Not that I’m complaining, considering the fact that the majority of the people who don’t care enough to invest $9 a month will be out of business in a very short while and will leave more work for the rest of us.

    2. PG

      TimJones July 8th

      Just order it once every 6 months then you get all the past tutorials.

      ( Reply )
    3. PG

      Breeze July 8th

      Well if you’re a pro and bored why not try your hand at writing some tutorials? At a certain level of skill all tutorials sites will become boring, that’s in my opinion, what we all are striving for.

      ( Reply )
  5. PG

    barat July 8th

    Is this sending mails in background? Using CRON or self calling php script? Or maybe it’s just basic system where You have browser open or it sends all mails in one mail() ?

    ( Reply )
    1. PG

      Robert July 8th

      I doubt it would use mail(). That built-in function of PHP is very inadequate to the task of mass-mailings.

      ( Reply )
  6. PG

    Paul du Long July 8th

    It’s using the Swift_Mail library. As far as i know thats sending emails in the background but i’m not sure ;)

    ( Reply )
    1. PG

      Alex Coomans July 8th

      Well the actual sending is a simple GET request that then uses swift mailer to send it, however there is no background process. The actual sending is done using whatever SMTP server you set up, and in this case it is locally on the machine, though check the documentation for more info. A feature you could add would be a JS request to perform the page request but I did not cover in respect of the length. 2 hours is a lot of time.

      ( Reply )
  7. PG

    unformatik July 8th

    any Moneybookers account to get an account please!!!

    ( Reply )
  8. PG

    DP July 8th

    Is it just me or is the video lagging behind the audio when streaming? The lag was pretty bad about 30mins into it, so I stopped watching it.

    ( Reply )
    1. PG

      Jeff Adams July 8th

      yeah its teh same for me, after about 10 mins or so it lags. I had to stop watching i too which sucks since it’s a Plus tut and they are always awesome.

      Can someone please take a look maybe?

      ( Reply )
  9. PG

    Deborah Gray July 8th

    Good tutorial! This is exactly what I needed for a new site we’re working on. Thanks!

    ( Reply )
  10. PG

    gway July 8th

    wow,
    so happy that i just signed for a + account,
    content is very good.

    and this tut is very complete, 2 hours + screencast wow
    this gona take me days to build lol

    thanx a lot

    ( Reply )
  11. PG

    Ashley July 8th

    I found a lot of errors in the source code (plus account) everything from incorrect syntax to capitalization problems (calling newsletters table). I’ve so far spent about three hours with the code weeding out these errors. Has anyone else found the same problems? Is there a revision that I am not aware of? I’m surprised this code got through the line without more error checking

    ( Reply )
    1. PG

      Jonathan C July 11th

      I found many errors, I had to contact Tuts+ Support to get this fixed.. I have to wait for their reply now.

      The NEWSLETTERS has to be changed to newsletters

      and

      the log out link is giving “Parse error: syntax error, unexpected T_VARIABLE in /admin/logout.php on line 3

      Also template_edit.php is not reading the full database name it reads user3 but not user3_news so after the _ is not read at all making the template edits unable to save the html to the database. Same happens with the messages.php

      ( Reply )
      1. PG

        Ricardo July 20th

        in logout.php

        require_once ‘config.php’

        missing ;

        require_once ‘config.php’;

    2. PG

      genji July 30th

      yep.. there are so many errors in the source files.

      The most annoying one for me is the fact that when I try to create a message and click continue to actually type the content of the message, it just refreshes the page instead of taking my to the second step.

      Can anyone help me out?

      Also, the code for the body of the template is nowhere so I can copy paste it. It should be in the source code imo.

      ( Reply )
  12. PG

    Jarryd July 8th

    I've been doing a lot of newsletter work recently and I was just looking at the screenshot for the e-mail template. Alex is using <divs> to seperate his content and has his styling on the header. I know it's not important for the purpose of this tutorial, but generally speaking using tables and complete inline styling is the way to go, just because e-mail clients are still so far behind web browsers that some don't even support <link> or <style> tags.

    Other than that it looks like an interesting tutorial, thanks Alex!

    ( Reply )
    1. PG

      Alex Coomans July 8th

      Yeah Jarryd you are exactly right, and I believe I mentioned this in the screencast. Email clients really are awful at standards compliance, and yes tables are the way to go, along with inline styling because most (or all, cannot remember) do not support CSS stylesheets. I had just used a stripped down version of the application template to simply demo the features.

      ( Reply )
  13. PG

    Yoosuf July 8th

    Hello Tuts+, in this kind of scenario I really appreciate the Plus, because its really valuable article!

    ( Reply )
  14. PG

    Diego SA July 8th

    Plus now would be excellent…

    ( Reply )
  15. PG

    awake July 8th

    Finally a plus tutorial that I think is worth it.

    It’s a long video, but will be helpful for newb’s trying to take their PHP skills to the next level.

    ( Reply )
    1. PG

      Alex Coomans July 8th

      Thanks awake! I’m glad that you enjoyed this tutorial, especially after all of the work! And also FYI anyone wondering, this video is about 2 hours.

      ( Reply )
  16. PG

    insic July 8th

    this is nice. I have also an ongoing project like this but Im having a problem in displaying directly the images in ms outlook. My boss wants the image to appear by the time the mail is opened he want to iliminate the right click and download image in outlook.

    ( Reply )
    1. PG

      Rob July 9th

      Hard to do as this is a security feature built into Outlook, also with rumours of the next version of Outlook reverting to using Word standard as it’s email display, that’ll be a whole new set of rules your html emails will have to adhere to.

      Some web mail apps also incorporate the ‘click to view images’ security feature, so I think you’re stuck with it for the time being!

      ( Reply )
  17. PG

    Jake Holman July 9th

    You really shouldn’t be sending out a large volume of emails from your own website or company IP address nor from your own SMTP unless you have one set up by a provider who knows what you are doing.

    The likelihood of you becoming blacklisted is rather high and you probably won’t make it into many subscribers inboxes, you’re better off finding a provider that can do it for you.

    Full disclosure: I do work for these type of providers, but I’m not here to pitch, just telling the reality.

    ( Reply )
  18. PG

    George Burrell July 9th

    HTML emails cannot use standard HTML coding. Tags such as DIV’s and inserting stylesheets within the header will just cause misformatting once the email has been sent out.

    Rendering in email clients is much like coding a site for different browsers. Take a look at the Email Standard Projects for a general overview:
    http://www.email-standards.org/

    ( Reply )
  19. PG

    Martyn Web July 9th

    Ive been using mailchimp recently which has been great but something like this would be a lot better so I will defiantly discuss a tutsplus account.

    Something like this would be so useful for me.

    ( Reply )
    1. PG

      Benjamin Reid July 9th

      Learning how to do it is good, and that knowledge can be applied to other applications but as Jake Holman (a few comments above) stateed, your best off to stay with MailChimp because of the implications you’ll run into when doing it yourself.

      ( Reply )
  20. PG

    Michael July 9th

    thx for the great tut..

    Can someone please create an xml tutorial?
    read, write, update and save an xml file with xml_writer and simplexml ;)

    ( Reply )
  21. PG

    Chris July 9th

    Why are all the cool Tuts for Plus members?

    ( Reply )
  22. PG

    Benn July 9th

    Im struggling to get this to work. For some reason i can not login to what i’ve created. After following the video and going through the documentation i still cannot login with the password which was set in the video.

    Anyone shed some light please?

    ( Reply )
  23. PG

    Jeff Adams July 9th

    Any fix on the audio/video sync yet?

    ( Reply )
  24. PG

    Serhio July 9th

    OK Now i’m angry, please can u integrate 2checout or sms paymet for countries without paypal support! I would like to be a + member but… i can’t

    Tnx :)

    ( Reply )
  25. PG

    Eire32 July 9th

    I got a plus account just to read this. I would call myself a intermediate user, got eh basics down but not the cool things. There are some nice object oriented PHP MySQL queries in there. Took me forever to read but its all good stuff. Worth the long read.

    ( Reply )
  26. PG

    paw July 10th

    Great post, i wish i was plus member!

    ( Reply )
  27. PG

    Henrik Pedersen July 10th

    Omg I hate that I couldn’t create a PLUS account just because I don’t have a Credit card ….

    Learn something paypal, if you want a cash flow, it isn’t that way to go!!!

    I used paypal because i didn’t want a credit card, and now i need to validate using such a stubbet thing of plastic :S

    ( Reply )
  28. PG

    Jonathan C July 11th

    The code should be tested because making it public. Many errors and not fully working.

    ( Reply )
    1. PG

      Jonathan C July 11th

      BEFORE***

      ( Reply )
      1. PG

        Alex Coomans July 13th

        Sorry to hear you are having problems. Can I help?

  29. PG

    josh July 11th

    Was considering subscribing for this till the comments about trash code and trash newsletter markup.

    oh well.

    ( Reply )
    1. PG

      Jonathan C July 13th

      It is not trashy code, everyone makes mistakes.

      ( Reply )
  30. PG

    Matthew July 13th

    I’m having an issue with this tutorial… On my new templates… I get an error when using mysql_real_escape_string… It inserts my name and colums… but my body doesn’t save. Here’s the error, any ideas?

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2) in /home/.killed/…/admin/templates_new.php on line 7

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/.killed/…/admin/templates_new.php on line 7
    INSERT INTO templates (name, columns, body) VALUES ( ‘Demo’ , 1, ” )

    ( Reply )
  31. PG

    Jonathan C July 13th

    Hello Alex,

    It looks like on a test server that I have on my laptop it works without problems, but then on a live working server it is not working properly.

    I opened a ticket with Envato and someone from the Staff Team will be forwarding you the problem I’m having I guess.

    They just told me they are going to contact you.

    Thank you.

    ( Reply )
    1. PG

      Jonathan C July 13th

      Test server = Envato’s Staff server.

      I tried on my test server and I still have the same problem that I have on my live server.

      ( Reply )
  32. PG

    Alex Coomans July 13th

    Hey guys,
    So another guy had contacted me about the problem Matthew has mentioned, and the mysql_real_escape_string should actually be mysqli_real_escape_string($link, ….

    It seems that for some reason it was working on my end but it doesnt on other people’s machines…

    If anyone has any problems shoot me an email at alex AT alexcoomans.com and I’ll do my best to help :)

    ( Reply )
    1. PG

      Matthew July 14th

      That worked! Thanks man, great tutorial

      ( Reply )
  33. PG

    koen buysse July 14th

    i wish i could pay a plus account with my marketplaces money…i do not have a credit card = no paypal = no plus tuts for me …

    ( Reply )
  34. PG

    Jonathan C July 14th

    Alex,

    All mysql_real_escape_string should be changed to mysqli_real_escape_string?

    ( Reply )
    1. PG

      Alex Coomans July 14th

      Yeah. So instead of mysql_real_escape_string($_POST['body']) they should be mysqli_real_escape_string($link, $_POST['body'])

      ( Reply )
  35. PG

    Cesar July 15th

    Good tutorial… But what’s with all the noise??? Is the Autor’s microphone nailed to the desk or what???

    ( Reply )
  36. PG

    Matt July 16th

    I am going through the tutorial now, but at 22:31 where you are supposed to load the index.php, mine looks nothing like what was supposed to come up. No problem, just a typo, right? So I open up the tutorial’s index.html page and copy/paste the code in to verify. Guess what? Same result.

    A text line that reads:
    Current Stats Our Home Page EOF; include ‘layout.php’; ?>

    Load up layout.php and pretty much get the same kind of result.

    Definitely frustrated and think I’ve just wasted an hour going through this first step. Don’t really want to go on from here with the groundwork not working correctly.

    ( Reply )
    1. PG

      Matt July 16th

      Uhhh… Let me remove my foot from my mouth and get some sleep… After about a half hour of looking and not finding, uploaded the dir to a live server and it pointed me right to my, yes my, error. Had a typo in classes.php. Wonder what is different in my MAMP config that didn’t show me the error.

      Basically, line 52 had
      $link->close{};
      iinstead of
      $link->close();

      ( Reply )
      1. PG

        Alex Coomans July 17th

        No problem Matt :) Everyone makes mistakes, the problem that the error wasn’t showing was because of the last line in the config.php file…

  37. PG

    Matt July 17th

    Okay, now I got one that I can’t get past. When I go to create a message, I complete the first step and then it goes right back to the first step (messages_new.php). If I click on the Messages tab, it comes back with the success message and shows the new message listed, but it never gets to messages_new_step2.php. I did copy and paste the code from the tutorial directly in on all the messages pages, so don’t think it’s a typo this time around… :)

    ( Reply )
    1. PG

      Matt July 17th

      Turned on all PHP errors and it shows this:

      Notice: Trying to get property of non-object in …/newsletter/admin/messages_new.php on line 11

      Warning: Cannot modify header information – headers already sent by (output started at …/newsletter/admin/messages_new.php:11) in …/newsletter/admin/messages_new.php on line 13

      Strange thing is, lines 9 & 10 in the source files and included HTML tutorial look nothing like what is typed in the video. Line 9 doesn’t exist in the video at all and line 10 is completely different.

      What should messages_new.php really look like?

      ( Reply )
      1. PG

        Ricardo July 20th

        Getting to step 2…

        // header(’Location: messages_new_step2.php?id=’ + $id);
        header(’Location: messages_new_step2.php?id=’.$id);

        Now looking at step 2…

      2. PG

        Alex Coomans July 26th

        Hey Matt – Sorry about the problem and the late replay. Shoot me an email and I’ll do my best to help you out :)

  38. PG

    Jonathan C July 17th

    I have been waiting for a solution to my errors and nothing till now and I’m still waiting…

    This is getting frustrating..

    ( Reply )
    1. PG

      Alex Coomans July 26th

      Hey Jonathan – Sorry you are having problems. Shoot me an email and I’ll help you one-on-one.

      ( Reply )
  39. PG

    bb July 22nd

    you should have added a dbschema.sql file to the source zip.
    and the error message “error” is not really talkative at all!!

    ( Reply )
  40. PG

    Jusko July 25th

    Is this safe enough to use one a future project? I have a client who wants a Neswletter System. Can I use this information to build it or does this have a limit number of emails that can be sent at a time?
    Thanks

    ( Reply )
  41. PG

    genji July 30th

    Why not reupload the source files? It’s a great tutorial man but just some minor errors in the source files.

    Reupload them :)

    ( Reply )
  42. PG

    Dan August 1st

    So many errors :(

    ( Reply )
  43. PG

    Alex August 15th

    I am nervous about this one! Just joined the + system (acutely this was the one that made me join it) as I have been such an avid reader of the tuts+.
    Is this working or has there been a new source code/vid uploaded with the fixes?

    ( Reply )
  44. PG

    Red62 October 22nd

    Amnesty International should note that action sounds louder than word! ,

    ( Reply )
  1. Arrow
    Gravatar

    Your Name
    October 22nd