Wednesday, 14 August 2013

Email subscription concept using php and mysqli

This concept user can register our email id to the particular site. for the purpose of when the site administrator post on any messages or updates its directly send the email on subscribe users. coding function used on when user can be register our email id in website form its post and store to the database.

email subscription img
Create blow type table database connect file using on Mysqli connection through the site

  CREATE TABLE `subscripe` (
     `id` int(11) NOT NULL AUTO_INCREMENT,
     `email` VARCHAR( 25 ) NOT NULL
)



PHP

<?php
if(isset($_POST['submit'])!="")
{
$subscriper=$_POST['user'];
$res1=$conn->query("select * from subscripe where email='$subscriper'");
$search=mysqli_num_rows($res1);
if($search)
{
echo "already subscripe $subscriper this mail";
}
else
{
$res2=$conn->query("insert into subscripe(email) values('$subscriper')");
if($res2)
{
$to = $subscriper;
$from = "mostlikers@gmail.com";
$subject = "Email verfications";
$message= "Hello &nbsp;" .$subscriper." <br/><br/><br/>
Thanks to subscripe on Mostlikers"
;
$headers = "From: $from\r\n";
$headers .= "Content-type: text/html\r\n";
mail($to, $subject, $message, $headers);
echo "thanks to subscripe mostlikers";
}
}
}
?>

HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email</title>
<style type="text/css">
.login { margin:0 auto; border:dashed 1px #000; width:500px; }
.login form { width:300px; margin:0 auto; }
</style>
</head>
<?php
$conn=new mysqli('localhost','root','','login');
?>
<body>
<div class="login">
<form method="post">
<label>Enter your Email ID</label>
<input type="email" name="user" placeholder="Enter your Email" autocomplete="on" required="required" >
<p align="center">
<input type="submit" value="Submit" name="submit" class="submit" id="submit"></p>
</form>
</div>
</body>
</html>


Live demo

basically email subscription concept based on feed burner so i provide live demo on my site email subscription. just subscribe here and check the mail.
    

5 comments:

  1. never trust user input, escape the email address, currently this code is a serious security flaw!!1

    ReplyDelete
    Replies
    1. Mysqli_real_escape_string

      Delete
    2. Yes, I always use that function to filer my user input.

      Anyway,recently my friend recommended me to use this library www.owasp.com. But i don't try it yet.

      Delete
  2. how can i use for wordpress

    ReplyDelete
    Replies
    1. just add newsletter plugin in your site its automatically send mail

      Delete