Blog » PHP » how to create email subscribe newsletter using php and mysql database

how to create email subscribe newsletter using php and mysql database

In This article will show you how to create a email subscribe newsletters script using PHP and MYSQL database. This script is showing you how to create subscribe, unsubscribe and send email etc.

first of all you have to create database name and table name. database name is subscribe and table name is emails.

--
-- Database: `subscribe`
--

-- --------------------------------------------------------

--
-- Table structure for table `emails`
--

CREATE TABLE IF NOT EXISTS `emails` (
`id` int(55) NOT NULL,
 `email` varchar(100) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;


creating config.php file


 <?php
$hostname = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "subscribe";
$con = new mysqli($hostname, $dbuser, $dbpass, $dbname);
if($con->connect_error){
die("Error connect to db");
}
?>


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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Subscribe</title>
</head>
<body>
<form action="" method="post"> 
<label>Enter Email to Subscribe :</label><br><input type="text" name="email" />
<input type='submit' name='submit' />
</form>
<?php
include("config.php");
if($_SERVER['REQUEST_METHOD'] == "POST"){
$email=$_POST['email'];
$stmt = $con->prepare("insert into emails (email) values (?)");
$stmt->bind_param("s", $email);
if($stmt->execute()){
echo "subscribe successfully.<br/>";
}else{
echo "subscribe failed<br/>";
}
}
?>
</body>
</html>


unsubscribe.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>Unsubscribe</title>
</head>
<body>
<?php 
include("config.php");
if (isset($_POST['submit'])){
$email= $_POST['email']; 
$stmtdelete = $con->prepare("delete from emails where email=?");
$stmtdelete->bind_param('s', $_POST['email']);
$stmtdelete->execute();
if($stmtdelete->execute()){
echo "Unsubscribed";
  }
}
?> 
<form action="" method="POST"> 
<label>Enter Your Email to Unsubscribe :</label>
<input type="text"id="email" name="email"/> <br> 
<input type="submit" name="submit" value="Submit"/> 
</form> 
</body>
</html>


send-mail.php


we can submit link to all subscribed emails, at once.


<!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>Newsletters</title>
</head>
<body>
<form action="" method="post"> 
<label>Title :</label><br><input type="text" name="title" /><br> 
<label>Link :</label><br><textarea cols="20" rows="15" name="link"></textarea><br> 
<input type='submit' name='submit' />
</form>
<?php
include("config.php");
if (isset($_POST['submit'])){
$from= 'newsletters@yourdomain.com';
$subject = "Articles";
$title= $_POST['title']; 
$link= $_POST['link']; 
$stmt = $con->prepare("select * from emails");
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$email= $row['email']; 
$message= "Title : $title,\n$link"; 
mail($email, $subject, $message, 'From:' . $from); 
echo 'Email Sent'; 
  }
 }
}
?>
</body>
</html>
ARTICLE POSTED BY : Admin

Hi, I'am k.g uththara. I'm a professional web developer. I write blog posts in my free time. if you want to contact me, send a message on contact page, thanks.