The previous lesson of PHP Lessons Series was about generating unique random numbers using a php function and in this lesson we will learn to make a simple email marketing system in PHP.
This project consists of five panels:
1. New Template: Used to create new email templates.
2. Manage Templates: Used to edit and delete existing email templates.
3. New Email List: Used to create new email lists.
4. Manage Email Lists: Used to edit and delete existing email lists.
5. Send Mail: Used to send the selected email template to the selected email list.
You can download the complete project here.
Create a MySql Table for email templates.
Create a MySql Table for email lists.
Create a MySql Table for email addresses.
Download PHP Mailer class and put it in your project directory.
There is only one css file (style.css) file in the css folder.
style.css
All the PHP files of the project are given below:
config.php
DBEngine.php
header.php
index.php
templates.php
lists.php
manage_templates.php
manage_lists.php
edit.php
edit_list.php
send_mail.php
This project consists of five panels:
1. New Template: Used to create new email templates.
2. Manage Templates: Used to edit and delete existing email templates.
3. New Email List: Used to create new email lists.
4. Manage Email Lists: Used to edit and delete existing email lists.
5. Send Mail: Used to send the selected email template to the selected email list.
You can download the complete project here.
Screenshots:
MySql part of the email marketing system:
In the MySql part, first of all create a MySql database named `email_campaign` and run the queries given below to create tables in the database.Create a MySql Table for email templates.
CREATE TABLE templates (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
template_name VARCHAR(50),
subject VARCHAR(250),
body TEXT
)
Create a MySql Table for email lists.
CREATE TABLE lists (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
list_name VARCHAR(50)
)
Create a MySql Table for email addresses.
CREATE TABLE emails (
id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
list_id INT(11) NOT NULL,
email_address VARCHAR(50)
)
PHP part of the email marketing system:
The library used in the project to send emails is PHP Mailer. The directory structure of simple email marketing system is given below:Download PHP Mailer class and put it in your project directory.
There is only one css file (style.css) file in the css folder.
style.css
h3{
font-family:Tahoma;
}
b{
font-family:Arial;
}
p{
font-family:arial;
}
nav{
background:grey;
}
ul{
overflow:hidden;
padding:0px;
}
li{
list-style:none;
float:left;
color:#FFFFFF;
background:#000000;
border-right:2px solid white;
padding:15px;
}
li a{
color:#FFFFFF;
font-family:Arial;
}
th td{
background:black;
color:white;
}
.green{
color:green;
}
.red{
color:red;
}
.input-text{
width:720px;
}
.manageTemplates{
margin-top:70px;
margin-bottom:70px;
}
.manageTemp, .manageTemp td{
border:1px solid black !important;
border-collapse:collapse;
}
.holder{
border:1px solid black;
padding:15px;
border-radius:10px;
width:700px;
margin-top:50px;
}
All the PHP files of the project are given below:
config.php
<?php
date_default_timezone_set('America/New_York');
include 'DBEngine.php';
$query = new DBEngine();
DBEngine.php
<?php
class DBEngine
{
public $con;
public function __construct($host="localhost",$db = "email_campaign",$user="root",$pass="")
{
try {
$this->con = new PDO("mysql:host=$host;
dbname=$db",$user,$pass,
array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
}
catch (Exception $e) {
echo 'Connection Failed';
return 0;
}
}
public function Fetch($_sql)
{
$query = $this->con->prepare($_sql);
$query->execute();
if($query->rowCount() > 0) {
while($rows = $query->fetch(PDO::FETCH_ASSOC)) {
$array[] = $rows;
}
}
return (isset($array) && $array !== 0 && !empty($array))? $array: 0;
}
public function Write($_sql)
{
$query = $this->con->prepare($_sql);
$query->execute();
}
}
?>
header.php
<html>
<head>
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<h3>Email Management System</h3>
<nav><ul>
<li><a href="index.php">Home</a></li>
<li><a href="templates.php">New Template</a></li>
<li><a href="manage_templates.php">Manage Templates</a></li>
<li><a href="lists.php">New Email List</a></li>
<li><a href="manage_lists.php">Manage Email Lists</a></li>
<li><a href="send_mail.php">Send Mail Panel</a></li>
</ul>
</nav>
index.php
<?php include 'header.php'; ?>
<div class="holder">
<p>
This is a simple email marketing system. You can create a new email template in
<b>New Template Panel.</b> A new template once created can be
managed (updated and deleted) from <b>Manage Templates Panel</b>.
A new email list can be created in <b>New List Panel.</b>
A new list created can be managed (updated and deleted) from
<b>Manage Lists Panel</b>. In the <b>Send Mail Panel</b>, to send an
email template to a list, first select an email template, then select an email list
to send the emails to and then click the <b>Send Mail</b> button to start
processing emails.</p><br /><br />
Follow <a href="http://www.informationbitz.com/">InformationBitz.com</a> for more
web tutorials, tips and demos.<br /><br />
</div>
</body>
</html>
templates.php
<?php
include 'config.php';
include 'header.php';
if(isset($_POST['submit'])){
$template_name = $_POST['template_name'];
$template_subject = $_POST['template_subject'];
$template_body = $_POST['template_body'];
if(!empty($template_name) && !empty($template_subject) && !empty($template_body)){
$insert_template = $query->Write("INSERT INTO `templates` VALUES('',
'$template_name',
'$template_subject',
'$template_body'
)");
if($insert_template)
$insert_new_template = 'true';
else
$insert_new_template = 'false';
}else{
echo 'Please Fill all necessary data';
}
}
if(isset($insert_new_template) && $insert_new_template == true){
echo 'New Template Added Successfully.';
}else if(isset($insert_new_template) && $insert_new_template == false){
echo 'Adding New Template Failed.';
}
?>
<div class="addTemplates">
<h4>Add New Template</h4>
<form action="" method="POST">
<table>
<tr><td>Template Name:</td>
<td><input type="text" name="template_name" class="input-text" /></td></tr>
<tr><td>Subject:</td>
<td><input type="text" name="template_subject" class="input-text" /></td></tr>
<tr><td>Body:</td>
<td><textarea name="template_body" rows="50" cols="100"></textarea></td></tr>
<tr><td>
</td><td><input type="submit" value="Add New Template" name="submit" /></td></tr>
</table>
</form>
</div>
</body>
</html>
lists.php
<?php
include 'config.php';
include 'header.php';
if(isset($_POST['submit'])){
$list_name = $_POST['list_name'];
$list_emails = $_POST['list_emails'];
$emailArr = explode(',',$list_emails);
if(!empty($list_name) && !empty($emailArr)){
$insert_list = $query->Write("INSERT INTO `lists` VALUES('',
'$list_name')");
if($insert_list)
$insert_new_list = 'true';
else
$insert_new_list = 'false';
$last_insert_list = $query->Fetch("SELECT * FROM `lists` ORDER BY `id` DESC LIMIT 1");
$last_insert = $last_insert_list[0]['id'];
foreach($emailArr As $email){
$insert_new_email = $query->Write("INSERT INTO `emails` VALUES('',
'$last_insert',
'$email')");
}
}else{
echo 'Please Fill all necessary data';
}
}
if(isset($insert_new_list) && $insert_new_list == true){
echo 'New List Added Successfully.';
}else if(isset($insert_new_list) && $insert_new_list == false){
echo 'Adding New List Failed.';
}
?>
<h4>Email List Panel</h4>
<form action="" method="POST">
<table>
<tr><td>List Name:</td>
<td><input type="text" name="list_name" class="input-text" /></td></tr>
<tr><td>List Emails:</td>
<td>
<input type="text" name="list_emails" class="input-text" placeholder="Comma Separated Emails" />
</td></tr>
<tr><td></td><td><input type="submit" value="Add New List" name="submit" /></td></tr>
</form>
</body>
</html>
manage_templates.php
<?php
include 'config.php';
include 'header.php';
$templates = $query->Fetch("SELECT * FROM `templates`");
?>
<div class="manageTemplates">
<h4>Manage Templates</h4>
<table cellpadding="5" class="manageTemp">
<tr>
<td>#</td>
<td>Template Name</td>
<td>Email Subject</td>
<td>Actions</td>
</tr>
<?php
$count = 0;
foreach($templates as $templates_row){
$count++;
?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo $templates_row['template_name']; ?></td>
<td><?php echo $templates_row['subject']; ?></td>
<td>
<a href="edit.php?action=update&id=<?php echo $templates_row['id']; ?>">Update</a>
<a href="edit.php?action=delete&id=<?php echo $templates_row['id']; ?>">Delete</a>
</td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
manage_lists.php
<?php
include 'config.php';
include 'header.php';
$lists = $query->Fetch("SELECT * FROM `lists`");
?>
<div class="manageTemplates">
<h4>Manage Email Lists</h4>
<table cellpadding="5" class="manageTemp">
<tr>
<td>#</td>
<td>List Name</td>
<td>Actions</td>
</tr>
<?php
$count = 0;
foreach($lists as $lists_row){
$count++;
?>
<tr>
<td><?php echo $count; ?></td>
<td><?php echo $lists_row['list_name']; ?></td>
<td>
<a href="edit_list.php?action=update&id=<?php echo $lists_row['id']; ?>
&name=<?php echo $lists_row['list_name']; ?>">Update</a>
<a href="edit_list.php?action=delete&id=<?php echo $lists_row['id']; ?>
&name=<?php echo $lists_row['list_name']; ?>">Delete</a>
</td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
edit.php
<?php
include 'config.php';
include 'header.php';
if(isset($_GET['action']) && !empty($_GET['action'])){
$action = $_GET['action'];
$templateid = $_GET['id'];
if($action == 'update'){
$template_details = $query->Fetch("SELECT * FROM `templates`
WHERE `id` = '$templateid'");
}else if($action == 'delete'){
$delete_template = $query->Write("DELETE FROM `templates`
WHERE `id` = '$templateid'");
header('Location: templates.php');
}
}
if(isset($_POST['submit'])){
$id = $_POST['id'];
$template_name = $_POST['template_name'];
$template_subject = $_POST['template_subject'];
$template_body = $_POST['template_body'];
if(!empty($template_name) && !empty($template_subject) && !empty($template_body)){
$update_template = $query->Write("UPDATE `templates` SET
`template_name` = '$template_name',
`subject` = '$template_subject',
`body` = '$template_body'
WHERE `id` = '$id'");
header('Location: manage_templates.php');
}else{
echo 'Please Fill all necessary data';
}
}
if($action == 'update'){ ?>
<div class="addTemplates">
<h4>Edit Template</h4>
<form action="edit.php" method="POST">
<table>
<tr><td>Template Name:</td>
<td>
<input type="text" name="template_name"
value="<?php echo $template_details[0]['template_name']; ?>" class="input-text" />
</td></tr>
<tr><td>Subject:</td>
<td>
<input type="text" name="template_subject"
value="<?php echo $template_details[0]['subject']; ?>" class="input-text" />
</td></tr>
<tr><td>Body:</td>
<td>
<textarea name="template_body" rows="50" cols="100">
<?php echo $template_details[0]['body']; ?></textarea>
</td></tr>
<tr><td></td><td><input type="submit" value="Edit Template" name="submit" /></td></tr>
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
</table>
</form>
</div>
</body>
</html>
<?php } ?>
edit_list.php
<?php
include 'config.php';
include 'header.php';
if(isset($_GET['action']) && !empty($_GET['action'])){
$action = $_GET['action'];
$listid = $_GET['id'];
$listname = $_GET['name'];
if($action == 'update'){
$list_details = $query->Fetch("SELECT * FROM `emails` WHERE `list_id` = '$listid'");
$count = 0;
$emails = '';
foreach($list_details as $list_details_row){
if($count == 0)
$emails = $list_details_row['email_address'];
else
$emails = $emails.','.$list_details_row['email_address'];
$count++;
}
}else if($action == 'delete'){
$delete_emails = $query->Write("DELETE FROM `emails` WHERE `list_id` = '$listid'");
$delete_list = $query->Write("DELETE FROM `lists` WHERE `id` = '$listid'");
header('Location: manage_lists.php');
}
}
if(isset($_POST['submit'])){
$id = $_POST['id'];
$list_name = $_POST['list_name'];
$list_emails = $_POST['list_emails'];
if(!empty($list_name) && !empty($list_emails)){
$delete_emails = $query->Write("DELETE FROM `emails` WHERE `list_id` = '$id'");
$emailArr = explode(',',$list_emails);
foreach($emailArr As $email){
$insert_email = $query->Write("INSERT INTO `emails` VALUES('','$id','$email')");
}
$update_list = $query->Write("UPDATE `lists` SET `list_name` = '$list_name'
WHERE `id` = '$id'");
header('Location: manage_lists.php');
}else{
echo 'Please Fill all necessary data';
}
}
?>
<h4>Edit Email List</h4>
<form action="" method="POST">
<table>
<tr><td>List Name:</td>
<td>
<input type="text" name="list_name" value="<?php echo $listname; ?>" class="input-text" />
</td></tr>
<tr><td>List Emails:</td>
<td>
<input type="text" name="list_emails" value="<?php echo $emails; ?>" class="input-text" />
</td></tr>
<tr><td></td><td><input type="submit" value="Edit List" name="submit" /></td></tr>
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>" />
</table>
</form>
</body>
</html>
send_mail.php
<?phprequire 'PHPMailer-master/PHPMailerAutoload.php';
include 'config.php';
include 'header.php';
$lists = $query->Fetch("SELECT * FROM `lists`");
$templates = $query->Fetch("SELECT * FROM `templates`");
if(isset($_POST['submit']) && !empty($_POST['templates']) && !empty($_POST['lists'])){
$templateid = $_POST['templates'];
$listid = $_POST['lists'];
$template_details = $query->Fetch("SELECT * FROM `templates`
WHERE `id` = '$templateid'");
$emails = $query->Fetch("SELECT * FROM `emails` WHERE `list_id` = '$listid'");
$subject = $template_details[0]['subject'];
$body = $template_details[0]['body'];
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'mail.test.com'; // SMTP Host
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'admin@test.com'; // SMTP username
$mail->Password = 'pass123'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->From = 'admin@test.com';
$mail->FromName = 'Test Admin';
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $body;
foreach($emails as $email){
$mail->addAddress($email['list_email']);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
$mail->ClearAllRecipients();
}
}else if((isset($_POST['templates']) && empty($_POST['templates'])) ||
(isset($_POST['lists']) && empty($_POST['lists']))){
echo 'Please select a template and a list.';
}
?>
<h4>Send Mail Panel</h4>
<form action="" method="POST">
<table cellpadding="5">
<tr><td>Select Template:</td>
<td>
<select name="templates" class="input-text">
<option value="">Please select a template.</option>
<?php
foreach($templates as $template){
?>
<option value="<?php echo $template['id']; ?>">
<?php echo $template['template_name']; ?>
</option>
<?php
}
?>
</select>
</td>
</tr>
<tr><td>Select List:</td>
<td>
<select name="lists" class="input-text">
<option value="">Please select a List.</option>
<?php
foreach($lists as $list){
?>
<option value="<?php echo $list['id']; ?>"><?php echo $list['list_name']; ?<</option>
<?php
}
?>
</select>
</td>
</tr>
<tr><td></td><td><input type="submit" value="Send Mail" name="submit" /></td></tr>
</table>
</form>
</body>
</html>
No comments:
Post a Comment