RSS

›Php Newsletter Sytem Tutorial

This entry was posted on Nov 13 2010
Final Image

Today, in this tutorial i will teach you how to make a Php Newsletter System with subscribe and admin features. When someone subscribe to newsletter then Admin can send emails to the subscribers.

Lets Start
At first we have to create MySql Database

Creating Database

Creating a new database “mydb” (if not exist):

CREATE DATABASE mydb 

Creating Table

CREATE TABLE  `mydb`.`newsletters` (
`ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`email` VARCHAR( 225 ) NOT NULL
) ENGINE = MYISAM ;

Create a new php file “subscribe.php”.

Forms

Creating Forms

put the below code in the ‘body’ tag of your php page.

<div id="newsletter">
<h2>Our Newsletter</h2>
subscribe to our newsletter to receive daily articles and update through email!
<form method="post" name="newsletter">
<strong>Email:</strong> <input name="email" type="text" />
<input name="submit" type="submit" value="Subscribe" />
</form>
</div>

Styling the Forms using Css

put the below code in the ‘head’ tag of your php page.

<style type="text/css">
#newsletter{
	padding:15px;
	background-color: #E4E4E4;
	border:1px solid #CCC;
	width:280px;
}
#newsletter h2{
	margin:0;
	padding-bottom:3px;
	font-size:19px;
	color:#039;
}</style>

Example

Coding

Adding PHP Code

At first we have to be connected to Mysql Database:

<?php
$host = "localhost";
$username = "myuser";
$password = "mypass";
$database = "mydb";
$connect = @mysql_connect($host, $username, $password) or die (@mysql_error());
$selectdb = @mysql_select_db($database, $connect) or die (@mysql_error());
?>

Submitting forms data in Database

<?php
if(isset($_POST['submit'])){
	$email = $_POST['email'];
	if(empty($email)){
		echo "you must write your email!";
	}else{
		@mysql_query("INSERT INTO newsletter SET email='$email'");
		echo "Thanks, for subscribing to our newsletter";
	}
}
?>

Putting it All Together: (subscribe.php)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
#newsletter{
	padding:15px;
	background-color: #E4E4E4;
	border:1px solid #CCC;
	width:280px;
}
#newsletter h2{
	margin:0;
	padding-bottom:3px;
	font-size:19px;
	color:#039;
}</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Php Newsletter</title>
</head>

<body>
<?php
$host = "localhost";
$username = "myuser";
$password = "mypass";
$database = "mydb";
$connect = @mysql_connect($host, $username, $password) or die (@mysql_error());
$selectdb = @mysql_select_db($database, $connect) or die (@mysql_error());
if(isset($_POST['submit'])){
	$email = $_POST['email'];
	if(empty($email)){
		echo "you must write your email!";
	}else{
		@mysql_query("INSERT INTO newsletter SET email='$email'");
		echo "Thanks, for subscribing to our newsletter";
	}
}
?>
<div id="newsletter">
<h2>Our Newsletter</h2>
subscribe to our newsletter to receive daily articles and update through email!
<form method="post" name="newsletter">
<strong>Email:</strong> <input name="email" type="text" />
<input name="submit" type="submit" value="Subscribe" />
</form>
</div>
</body>
</html>

Admin Area

Administrator can Create and Send Newsletters to users, Create a new php file “sendnewsletter.php”.

Creating Forms

<form action="" method="post" name="newsletter">
<strong>Write Newsletter below:</strong><br />
    <textarea name="newsletter" cols="70" rows="25"></textarea><br />
    <input type="submit" name="send" id="send" value="Send Newsletter" />
</form>

Example

Sending Newsletters

Connecting to Database

<?php
$host = "localhost";
$username = "myuser";
$password = "mypass";
$database = "mydb";
$connect = @mysql_connect($host, $username, $password) or die (@mysql_error());
$selectdb = @mysql_select_db($database, $connect) or die (@mysql_error());
?>

Sending Newsletter to Users

if(isset($_POST['send'])){
	$message = $_POST['newsletter'];
	$subject = "Daily Newsletter";
	$From = "UrPhp.com Newsletter <support@urphp.com>";
	$emails = @mysql_query("SELECT * FROM newsletter");
	while ($show = @mysql_fetch_array($emails)) {
		@mail("$show[email]","$subject",$message,"From: $From");
	}
	echo"<h3>Newsletter has been Sent!</h3>";
}

Putting it All Together: (sendnewsletter.php)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sending Newsletter</title>
</head>
<body>
<?php
$host = "localhost";
$username = "myuser";
$password = "mypass";
$database = "mydb";
$connect = @mysql_connect($host, $username, $password) or die (@mysql_error());
$selectdb = @mysql_select_db($database, $connect) or die (@mysql_error());

if(isset($_POST['send'])){
	$message = $_POST['newsletter'];
	$subject = "Daily Newsletter";
	$From = "UrPhp.com Newsletter <support@urphp.com>";
	$emails = @mysql_query("SELECT * FROM newsletter");
	while ($show = @mysql_fetch_array($emails)) {
		@mail("$show[email]","$subject",$message,"From: $From");
	}
	echo"<h3>Newsletter has been Sent!</h3>";
}
?>
<h2>Send Newsletters</h2>
<form action="" method="post" name="newsletter">
<strong>Write Newsletter below:</strong><br />
    <textarea name="newsletter" cols="70" rows="25"></textarea><br />
    <input type="submit" name="send" id="send" value="Send Newsletter" />
</form>
</body>
</html>

Available for Download

Download

About admin
View all post written by admin
Hello, My name is Shah Hussain I am a Freelance Php Developer, i wrote free php tutorials
This author written about (20) articles


3 Responses to “Php Newsletter Sytem Tutorial”

  1. Is this script support if there is more than 100 subscribers? because most host doesn’t allow more than 100 emails to send in a hour.


  2. Hi, you can try it yourself, but i think it will works because there is no limit in sending mails..

    try it, it will works…

    Good bye


  3. You have a mistake in your sql code, its not newsletters, is newsletter..

    and why is this text from ‘Post a comment’ the same color as the background?i cant see shit


Post a Comment