PHP Newsletter Script shows an one time per customer newsletter popup window with newsletter subscription form and suggestions to subscribe. After subscription, the user will get the latest blog updates in the website.

Here we are going to create an email newsletter from blog’s RSS feed.
We are going to follow 3 steps to send latest blogs to the subscriber:

(a): Here we  generate a newsletter popup through which we are going to save email address of the user.

(b) : We extract the data (title, description, links) of the latest blog post from the website feed.

(c) : Prepare the message and send to all subscriber.


Step-1:

Here we are going to create a popup window and to save its data in a text file.

(i)-Create a popup window with a form:

Below code creates a popup window at the time of loading of the page.


 

For a complete solution, you can refer to our service Mailget or you can purchase our product Send Email With SMTP – PHP Script.


PHP File : Subscription-popup.php

<!--Subscription-popup.php-->

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Show Popup on Page Load</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<link rel="stylesheet" type="text/css" href="https://www.formget.com/tutorial/createpopupform_livedemo/css/elements.css">
<script src="js/popup.js" type="text/javascript"></script>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<link href="css/style1.css" rel="stylesheet" type="text/css"/>
<meta name="robots" content="noindex, nofollow">
</head>
<body>
<div class='popup1'>
<div class='cnt223'>
<div style="height:30px;">
<a href=''><img class="close" src="close.png" style="width:24px; height:24px; float:right;"></a>
</div>
<!-- contact us form -->
<form action="processing-script.php" method="POST"style="border: 2px solid #ccc;" id="pop">
<h2>Subscribe Us!</h2><hr>
<p>Get Awesome updates delivered directly in your inbox .
<br/>Just Type Your Email Address Below And Click Subscribe!</p>
<input type="text" name="email" id="email" placeholder="Email" style="background-image:none;">
<div id="submitb">
<input id="submit" type="submit" name="submit" value="Subscribe">
</div>
</form>
</div>
</div>
<div id="main">
<h1>PHP Newsletter Script</h1>
<div id="login">
<h2>Get the Blogposts</h2>
<hr/>
<div>
<form action="index1.php" style="border:none;">

<input type="submit" name="submit" value="Click to Send Blogposts"/>
</form>
</div>
</div>
</div>
</body>
</html>


(ii)-Save its data in a text file:

Below code accepts the data from subscription-popup.php and save it in a text file.

Note: Before applying this code you first have to create a text file data.txt in your root directory, this file stores the name of subscribers separated by the comma.

PHP File : Processing-script.php

<!--save this code by the name of processing-script.php-->

<?php
if(isset($_POST['email'])) {
$data = $_POST['email'].',' ;
$ret = file_put_contents('data.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('There was an error writing this file');
}
else {
echo "$ret bytes written to file";
header('Location: subscription-popup.php');
}
}
else {
die('no post data to process');
}
?>

Step-2:

Create a text file name savecont.txt.
And write the title of blogs currently available on the website separated by a comma.
So only latest uploaded blogs can be sent, if we don’t do this all blogs will get send, at the first time execution of code.


Step-3:

(i)-Get the list of the new uploaded blogs:

We do this by comparing current blogs title of the website to title stored in savecont.txt

<?php
require 'PHPMailerAutoload.php';
$url = "https://www.formget.com/feed/";
$xml = simplexml_load_file($url);
$pollids = "savecont.txt"; // txt file contain title of already uploaded blog
$contents = file_get_contents($pollids); //string contains list of titles seprated by commas
$data="";
$new="";
for($i=0;$i<10;$i++)
{
$a= $xml->channel->item[$i]->title.","; //this method works to fetch data from xml array
$data.=$a; // $data contains titles of current uploaded blogs
if (strpos($contents,$a) !== false) {
}
else{
$new.=$a; //contains titles newly uploaded blogs
}
}
$newblogs = explode(',', $new); // array contains title of new blogs uploaded
fopen('savecont.txt', 'w+');
$ret = file_put_contents('savecont.txt', $data, FILE_APPEND | LOCK_EX); //update the list of blog title in text file
?>

(ii) Fetch the content of blog:

We picks up the title, description, and link of the blog through xml.

<?php
$url = "https://www.formget.com/feed/";
$xml = simplexml_load_file($url);
$max = sizeof($newblogs); // contains number of new blogs
for($m=0;$m<$max;$m++)
{
$n=$newblogs[$m]; //contains title of new uploaded blog
$t=$xml->channel->item[$m]->title; // contains titles of blogs of website
if($n==$t)
{
echo "same string";
$a=$xml->channel->item[$m]->title; //set name of title
$b=$xml->channel->item[$m]->description; // set discription of blog in mail
$c=$xml->channel->item[$m]->link; // set name of the link of discription

?>

(iii) Now we fetch the name of the subscriber from data.txt and send the mails:

Prepare the mail for the blog using title, description, link.
Send mail to all email address stored in the data.txt file.

For sending mail, we are using PHPMailer library. You can refer to Send Email via Gmail SMTP Server in PHP to get an idea about PHPMailer library and Gmail settings & PHP Send HTML Email Template for preparing format of mail.


<?php
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com;smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'Insert Gmail Email Id'; // SMTP username
$mail->Password = 'Insert Gmail Password'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = '[email protected]';
$mail->FromName = 'abc';
$pollids = "data.txt";
$contents = file_get_contents($pollids);
$cont = explode(',', $contents); // contains number of emails
foreach ($cont as $item) {
$mail->addAddress($item); // Add a recipient
}
$mail->isHTML(true);
$mail->SingleTo = true;// if u want to send email to multiple users
$mail->Subject = 'Here is the subject';
$mail->Body = '<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Single Extention </title>
</head>
<body style="font-size: 45px;font-family: proxima_nova_rgregular, sans-serif;font-size: 16px;line-height: 28px;">
<!-- Middle Section -->
<div align="center" style="border:1px,red,solid;">
<div align="center" id="padd" style="width:815px;border-style: solid;border-color: #D5D5D5;padding:30px;background-color:#F2F2F2;" >
<div style=""><img src="https://ci5.googleusercontent.com/proxy/uQRkR4RIRhQ7LPTHiI4BHEaGun2LR1WJRBOVFkRovNdPC55ysLASrCmwaf-aSXsv4M4B51-4IFjQmIvRCFPHxI4PLAvgL_oWkaeZJFZwEHrprw8P-UiZIfpFFy9u18SxCT0cLwFOJ4ffmjJScFPgClc=s0-d-e1-ft#https://www.formget.com/mailget/upload_files/1431944509-1601297505-Formget_welcome_email_png"/></div>
<div id="division1" style="width:585px;border-style: solid;border-color: #D5D5D5;background-color:#ffffff;" align="center">
<h2>'.$a.'</h2>
<div class="extension-desc" align="center">
<p style="margin:0 15px;">'.$b.'</p>
<p><b>for more details click here</b>'.$c.'</p>
</div>
</div>
</div>
</div>
</body>
</html>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
}
header('Location: subscription-popup.php');
?>

Note: We are using PHPMailer library to send the mail for more details about PHPMailer prefer our old blog post.


PHP File : index1.php (complete code for step 3)

Here is the complete code to send mail to the subscriber but it also requires the data.txt and savecont.txt file.

<?php
require 'PHPMailerAutoload.php';
$url = "https://www.formget.com/feed/";
$xml = simplexml_load_file($url);
$pollids = "savecont.txt"; // txt file contain title of already uploaded blog
$contents = file_get_contents($pollids); //string contains list of titles seprated by commas
$data="";
$new="";
for($i=0;$i<10;$i++)
{
$a= $xml->channel->item[$i]->title.","; //this method works to fetch data from xml array
$data.=$a; // $data contains titles of current uploaded blogs
if (strpos($contents,$a) !== false) {
}
else{
$new.=$a; //contains titles newly uploaded blogs
}
}
$newblogs = explode(',', $new); // array contains title of new blogs uploaded
fopen('savecont.txt', 'w+');
$ret = file_put_contents('savecont.txt', $data, FILE_APPEND | LOCK_EX); //update the list of blog title in text file
?>
<?php
$url = "https://www.formget.com/feed/";
$xml = simplexml_load_file($url);
$max = sizeof($newblogs); // contains number of new blogs
for($m=0;$m<$max;$m++)
{
$n=$newblogs[$m]; //contains title of new uploaded blog
$t=$xml->channel->item[$m]->title; // contains titles of blogs of website
if($n==$t)
{
echo "same string";
$a=$xml->channel->item[$m]->title; //set name of title
$b=$xml->channel->item[$m]->description; // set discription of blog in mail
$c=$xml->channel->item[$m]->link; // set name of the link of discription
?>
<?php
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com;smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '[email protected]'; // SMTP username
$mail->Password = 'formgetmb'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = '[email protected]';
$mail->FromName = 'Formget';
$pollids = "data.txt";
$contents = file_get_contents($pollids);
$cont = explode(',', $contents); // contains number of emails
foreach ($cont as $item) {
$mail->addAddress($item); // Add a recipient
}
$mail->isHTML(true);
$mail->SingleTo = true;// if u want to send email to multiple users
$mail->Subject = 'Here is the subject';
$mail->Body = '<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Single Extention </title>
</head>
<body style="font-size: 45px;font-family: proxima_nova_rgregular, sans-serif;font-size: 16px;line-height: 28px;">
<!-- Middle Section -->
<div align="center" style="border:1px,red,solid;">
<div align="center" id="padd" style="width:815px;border-style: solid;border-color: #D5D5D5;padding:30px;background-color:#F2F2F2;" >
<div style=""><img src="https://ci5.googleusercontent.com/proxy/uQRkR4RIRhQ7LPTHiI4BHEaGun2LR1WJRBOVFkRovNdPC55ysLASrCmwaf-aSXsv4M4B51-4IFjQmIvRCFPHxI4PLAvgL_oWkaeZJFZwEHrprw8P-UiZIfpFFy9u18SxCT0cLwFOJ4ffmjJScFPgClc=s0-d-e1-ft#https://www.formget.com/mailget/upload_files/1431944509-1601297505-Formget_welcome_email_png"/></div>
<div id="division1" style="width:585px;border-style: solid;border-color: #D5D5D5;background-color:#ffffff;" align="center">
<h2>'.$a.'</h2>
<div class="extension-desc" align="center">
<p style="margin:0 15px;">'.$b.'</p>
<p><b>for more details click here</b>'.$c.'</p>
</div>
</div>
</div>
</div>
</body>
</html>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
}
}
header('Location: subscription-popup.php');
?>

Making Some Structural Changes

1:-  style.css :- Hold the css for custom style.

2:- style1.css :- This file hold the css for popup window.

3:- popup.js :- This build the functionality of popup window.

CSS : Style.css

@import url(http://fonts.googleapis.com/css?family=Raleway);
h1{
text-align:center;
//color: black;
font-size: 2em;
//margin-left: 15%;
}
#main{
margin: 25px 100px;
font-family: 'Raleway', sans-serif;
}
h2{
background-color: #FEFFED;
text-align:center;
border-radius: 10px 10px 0 0;
margin: -10px -40px;
padding: 30px 40px;
color: black;
font-weight: bolder;
font-size: 1.5em;
margin-top: -1px !important;
//margin-bottom: -19px !important;
}
hr{
border:0;
border-bottom:1px solid #ccc;
margin: 10px -40px;
margin-bottom: 30px;
}
#login{
width:550px;
float: left;
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 0px 40px 0px;
margin-top: 70px;
margin: 50px;
margin-left: 27%;
}
input[type=text],input[type=email]{
width:99.5%;
padding: 10px;
margin-top: 8px;
margin-bottom: 18px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:raleway;
}
input[type=submit]{
width: 100%;
background-color:#FFBC00;
color: white;
border: 2px solid #FFCB00;
padding: 10px;
font-size:20px;
cursor:pointer;
border-radius: 5px;
margin-bottom: 12px;
}
#formget{
float:right;
}
h1 {
//margin-left: -85px;
}
li{
display: inline;
}
#inputurl{
//display: none;
margin-top: 40px;
}
#submitread,#submitinsert,#submitupdate,#submitdelete{
width: 99.5%;
}
#book_form_insert,#book_form_update{
display: none;
}
#database #tab td{
border: 1px solid #ccc;
padding: 7px 3px;
}
#rinsert,#rupdate,#rdelete{
display: none;
}
#id1,#id2,#id3,#id4,#id5{
display: none;
}
#enterid,#enteridd{
display: none;
}
#dummy{
text-transform: uppercase;
text-align: center;
font-weight: bolder;
}
#tab{
width: 450px;
}
#pop h2{
margin: -10px -50px;
margin-top: 0;
padding-top:40px;
}
#pop hr{
margin: 10px -50px;
}
#pop{
padding-top: 0px;
}

CSS : Style1.css

#overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: #000;
filter:alpha(opacity=70);
-moz-opacity:0.7;
-khtml-opacity: 0.7;
opacity: 0.7;
z-index: 100;
display: none;
}
.cnt223 a{
text-decoration: none;
}
.popup1{
width: 100%;
margin: 0 auto;
display: none;
position: fixed;
z-index: 101;
}
.cnt223{
width:600px;
min-height: 150px;
margin: 100px auto;
background: #f3f3f3;
position: relative;
z-index: 103;
padding: 10px;
border-radius: 5px;
box-shadow: 0 2px 5px #000;
}
.cnt223 p{
clear: both;
color: #555555;
text-align: justify;
}
.cnt223 p a{
color: #d91900;
font-weight: bold;
}
.cnt223 .x{
float: right;
height: 35px;
left: 22px;
position: relative;
top: -25px;
width: 34px;
}
.cnt223 .x:hover{
cursor: pointer;
}
#submit{
margin-top:30px;
}
#email{
float:left;
}
#submitb{
width:100px;
}
#div1{
border:1px solid red;
background-color:yellow;
}
form {
max-width: 1000px;
min-width: 250px;s
padding: 10px 50px;
border: 2px solid gray;
border-radius: 10px;
font-family: raleway;
background-color: white;
}
hr{
margin:none;}

Javascript : Popup.js

$(function(){
var overlay = $('<div id="overlay"></div>');
overlay.show();
overlay.appendTo(document.body);
$('.popup1').show();
$('.close').click(function(){
$('.popup1').hide();
overlay.appendTo(document.body).remove();
return false;
});

$('.x').click(function(){
$('.popup1').hide();
overlay.appendTo(document.body).remove();
return false;
});
});

 


Note: You must have to maintain data.txt and savecont.txt file while running this code.


Data.txt file contains email address separated by comma and savecont.txt file contains title of blogs available in the website feed

Tip: Check the data.txt and savecont.txt file after completion of code for debugging

To get the proper working code just download our zip file of code and use it.


Conclusion:

I hope that at this point you must be feeling yourself comfortable with Newsletter. Please comment for any query. Keep visiting our website.