How to create a "Subscribe to Newsletter" with MySQL using PHP

Written by sue smith Google
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to create a "Subscribe to Newsletter" with MySQL using PHP
Email newsletters provide the opportunity to create a continuous dialogue with customers and clients. (Getty Thinkstock)

If you run a website or service, you can keep potential and existing users engaged with your activities using an email newsletter. With PHP and a MySQL database running on your web server, you can store the email addresses of people on your mailing list. By creating an HTML form on your website, you will also provide the facility for users to sign up to your newsletter. With a PHP script pulling the details of subscribed users from the database you created, you can then send the email out to everyone who has signed up.

Skill level:
Moderately Challenging

Other People Are Reading


  1. 1

    Create a database. Log into your MySQL account via your web host. Create a new database and add a table to it, or add a table to an existing database if you prefer. Depending on the web interface provided by your web host, you may be able to create the table using the graphical user interface controls. If you need to use SQL to create the table, use the following syntax:

    CREATE TABLE Subscriptions(subscriberID INT NOT NULL AUTO_INCREMENT, subscriberEmail(VARCHAR(255) NOT NULL), PRIMARY KEY(subscriberID));

    This defines a table named "Subscriptions" with two columns in it. The first column is the primary key for the table, an ID number which will automatically increment each time a new record is added to the table. The second column is the subscriber email address, which is a text string. Both columns must have a value for each record added.

  2. 2

    Create a sign-up form. Create a web page on your site with a form allowing people to sign-up to your newsletter. Use the following HTML5 outline:

    <!DOCTYPE html> <html> <body> <p>Sign up to our newsletter:</p> <form action="subscribe.php" method="post"> Email address: <input type="email" name="email_address"/><br/> <input type="submit" value="Sign Up"/> </form> </body> </html>

    The form will send user data, via the post method, to the specified PHP script, in which you can add the new subscriber to the database. The form contains a text input field for the user to enter their email address. Using the HTML5 email input type means that you do not need to provide your own email validation function. The submit button will prompt the user email to be submitted.

  3. 3

    Retrieve user data. Create a file with the name you included as action attribute to your HTML form, "subscribe.php" to match the sample code. Add a PHP outline:

    <?php /code here/ ?>

    The remainder of code should be placed between these opening and closing tags. First get the submitted email address: $user_email = $_POST['email_address'];

    You use the name attribute from the email input element. Now only proceed if the email address has been received: if(!(!isset($user_email) || trim($user_email)==='')){ /submit to database/ }

    The remainder of the code in the file should be placed between these curly brackets.

  4. 4

    Add subscribers to the database. Inside the "if" block brackets in your subscribe PHP script, connect to your database:

    $connection = mysqli_connect("host", "username", "password", "database") or die("ERROR");

    Edit the code to use your own host address, username, password and database name. Now create your insert query using the user email:

    $insert_query = "INSERT INTO Subscriptions VALUES (null, '".$user_email."')";

    Now execute the query:

    mysqli_query($connection, $insert_query); mysqli_close($connection);

    Finally output a message to the user:

    echo "<p>Thanks for signing up!</p>";

    You can enclose this output message as part of a full HTML web page with other visible elements to match your site.

  5. 5

    Send your newsletter. Create a another PHP script with a name of your choice and ".php" extension. Add the outline:

    <?php /code here/ ?>

    Between the brackets, connect to your database and retrieve the user emails, altering the connection details again:

    $connection = mysqli_connect("host","username","password","database") or die("ERROR"); $query_result = mysqli_query($connection, "SELECT subscriberEmail FROM Subscriptions");

    Now read the email addresses into an array and store them in a variable to use in your emailing "to" field, with commas between them:

    $email_addresses = array(); while ($email_row = mysqli_fetch_row($query_result)){ $email_addresses[] = $email_row[0]; } $to_field = implode(", ", $email_addresses);

    Now send the email:

    mail($to_field, "Newsletter", "Email content");

    Place the body of your email in the third parameter to the mail method, with the subject text in the second parameter. Running your PHP script should now send the email out.

Tips and warnings

  • You can use automated email subscription libraries if you do not want to implement your own.
  • If subscribers wish to be removed from your list you will need to remove them from your database.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.