Jump to content

automatic php email


Go to solution Solved by Barand,

Recommended Posts

Hi

 

I have been looking into the timestamp to prevent the same emails being sent when I refresh the page or page load

 

would something like the following be close to what you mentioned about using timestamp to prevent receiving the same email

<?php
session_start();
$sTime = time() - 15;
if($sTime > $_SESISON['submit_timestamp']){
    // process form
} else {
    // some sort of error handling if you want it.
}
?>

I noticed as well this morning, I have refreshed the page and the date_last_modified column has updated to todays date and the time at 10:17 but I have not received any email

 

I adjusted the coding to be from 7 DAY to 14 DAY, don't think that would affect it as am I right in saying the code takes today's date and shows any expiry dates within 14 days from today's date

 

Is that right?

 

The coding is below for the INTERVAL etc.

$sqlCommand = "SELECT visitor_id, visitor_name, visitor_email, visitor_model, visitor_plate, visitor_tax, visitor_mot, visitor_insurance
, IF(visitor_tax BETWEEN CURDATE() AND CURDATE()+INTERVAL 14 DAY, DATE_FORMAT(visitor_tax, '%e %M %Y') , 'n/a') as taxdate
, IF(visitor_mot BETWEEN CURDATE() AND CURDATE()+INTERVAL 14 DAY, DATE_FORMAT(visitor_mot, '%e %M %Y') , 'n/a') as motdate
, IF(visitor_insurance BETWEEN CURDATE() AND CURDATE()+INTERVAL 14 DAY, DATE_FORMAT(visitor_insurance, '%e %M %Y') , 'n/a') as vidate 
FROM visitors 
WHERE visitor_tax BETWEEN CURDATE() AND CURDATE()+INTERVAL 14 DAY
OR visitor_mot BETWEEN CURDATE() AND CURDATE()+INTERVAL 14 DAY
OR visitor_insurance BETWEEN CURDATE() AND CURDATE()+INTERVAL 14 DAY";

The email script coding is below

$from = "noreply@irhwebsites.co.uk";
    $to = "{$row['visitor_email']}";
    $subject = "{$row['visitor_name']} Expiry Date(s)";
	$message = "Name: {$row['visitor_name']} \n Car Tax Expiry Date: {$row['taxdate']} \n Car MOT Expiry Date: {$row['motdate']} \n Insurance Expiry Date: {$row['vidate']}";
	$headers = "From:" . $from;
    $sendmail = mail($to,$subject,$message, $headers);
    echo "Email Successfully Sent";
	
	if($sendmail){
           $sqlCommand2 = "UPDATE visitors SET date_last_modified = NULL WHERE visitor_id = " . $row['visitor_id'];
            
			$query2 = mysqli_query($db, $sqlCommand2) or die (mysqli_error($db));
        } else {
		echo "do nothing";
		}

Consider this situation.

 

A visitor's mot is due in 7 days and his tax is due in 9 days time.

You create an email today for the mot and set notified to today.

You run the job tomorrow but check the notify date so you do not resend another reminder for the mot.

The next day when you run the job the tax is due so you check the notified date and you do not send.

Oops! You needed to send this new one.

So you decide you need a second notify date for tax and a third for insurance.

 

The case for normalizing the data now grows.

  • Table visitor : current visitor data without the dates
  • Table item : Descriptions for Insurance, tax and MOT with ids
  • Table renewal : visitor_id, item_id, renewal_date, date_notified

The query is also simplified and only returns the dates you need

SELECT 
v.visitor_id
, visitor_name
, visitor_email
, visitor_model
, visitor_plate
, item.description
, DATE_FORMAT(renewal_date, '%e %M %Y') as datedue
FROM visitors v
    INNER JOIN renewal USING (visitor_id)
    INNER JOIN item USING (item_id)
WHERE renewal BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY
    AND date_notified < CURDATE()-INTERVAL 7 DAY;

post-3105-0-28322800-1434885110_thumb.png

Think I got it right, I have attached screenshots of my database table structure for the 3 tables, hopefully I got it right but got a feeling I don't need the visitor_id, item_id and renewal_id in the renewal table as the coding looks like they are joining the columns together, is that right?

 

thought would confirm before I delete any columns in any of the tables

 

The coding as well I got is below

$sqlCommand = "SELECT 
v.visitor_id
, visitor_name
, visitor_email
, visitor_model
, visitor_plate
, item.description
, DATE_FORMAT(renewal_date, '%e %M %Y') as datedue
FROM visitors v
    INNER JOIN renewal USING (visitor_id)
    INNER JOIN item USING (item_id)
WHERE renewal_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 14 DAY
    AND date_notified < CURDATE()-INTERVAL 14 DAY;";

Hopefully is correct or am close

 

got a feeling I don't need the visitor_id, item_id and renewal_id in the renewal table

If you remove the visitor_id from the renewals table how the hell do you expect to match which renewals belong to which visitor? Ditto item_id and items.

 

With the keys you have

+------------+-----------+------------+      +------------+--------+----------------+
| vehicle_id | model     |   plate    |      | vehicle_id | item_id| renewal_date   |
+------------+-----------+------------+      +------------+--------+----------------+
|     1      |  Ford     |  AB 14 XYZ |      |      1     |    1   |  2015-06-28    | 
|     2      |  Jeep     |  CD 15 STU |      |      1     |    2   |  2015-06-30    |
+------------+-----------+------------+      |      1     |    3   |  2015-08-10    |
                                             |      2     |    1   |  2015-08-12    |
                                             |      2     |    2   |  2015-08-12    |
                                             |      2     |    3   |  2015-10-01    |
                                             +------------+--------+----------------+

Remove them and you have no idea what goes with what, just  a lot of dates

+------------+-----------+------------+      +----------------+
| vehicle_id | model     |   plate    |      | renewal_date   |
+------------+-----------+------------+      +----------------+
|     1      |  Ford     |  AB 14 XYZ |      |  2015-06-28    | 
|     2      |  Jeep     |  CD 15 STU |      |  2015-06-30    |
+------------+-----------+------------+      |  2015-08-10    |
                                             |  2015-08-12    |
                                             |  2015-08-12    |
                                             |  2015-10-01    |
                                             +----------------+

Ahh ok sorry got it, makes sense when you lay it out like you have just above, sorry

 

So that side of it is ok with the database set up correctly and the SELECT coding, just need to alter the INSERT coding which I just use muliple sql queries like the following

 

$sql1

 

$sql2

 

$sql3

 

That right as think I got that bit sussed? well hopefully

 

or just thought use one query but seperate the INSERT part by using ; at the end of the INSERT so bit like the following

INSERT INTO tbl(field, field) VALUES(val, val); INSERT INTO tbl2(field2, field2) VALUES(val2, val2); INSERT INTO tbl3(field3, field3) VALUES(val3, val3);

Would that work?

Edited by ianhaney50

If you have a new visitor

 

  • Insert visitor record
  • Get the new id using $newId = $mysqli->insert_id;
  • Insert the dates into renewals with
INSERT INTO renewals (visitor_id, item_id, renewal_date) VALUES
($newId, 1, '$insurance_date'),
($newId, 2, '$mot_date'),
($newId, 3, '$tax_date');

don't think I have done this right

$newId = $mysqli->insert_id;

$sql = "INSERT INTO visitors (visitor_id, visitor_name, visitor_email, visitor_firstline, visitor_secondline,visitor_town, visitor_county, visitor_postcode, visitor_tel, visitor_mobile, visitor_model, visitor_plate) VALUES
('".$_POST["$newId"]."','".$_POST["visitor_name"]."','".$_POST["visitor_email"]."','".$_POST["visitor_firstline"]."', '".$_POST["visitor_secondline"]."', '".$_POST["visitor_town"]."','".$_POST["visitor_county"]."','".$_POST["visitor_postcode"]."','".$_POST["visitor_tel"]."','".$_POST["visitor_mobile"]."','".$_POST["visitor_model"]."','".$_POST["visitor_plate"]."')";

"INSERT INTO item (item_id, description) VALUES
('".$_POST["1"]."','".$_POST["description"]."')";

"INSERT INTO renewals (visitor_id, item_id, renewal_date) VALUES
($newId, 1, '$insurance_date'),
($newId, 2, '$mot_date'),
($newId, 3, '$tax_date')";

or do I just put it as exactly as you have above

$newId = $mysqli->insert_id;

INSERT INTO renewals (visitor_id, item_id, renewal_date) VALUES
($newId, 1, '$insurance_date'),
($newId, 2, '$mot_date'),
($newId, 3, '$tax_date');

Sorry

The item table

+------------+------------------------+      
| item_id    |  description           |
+------------+------------------------+
|     2      |  Insurance             |
|     1      |  M.O.T.                |
|     2      |  Tax                   |
+------------+------------------------+

When you insert the visitor record exclude visitor_id column - that will be generated by the AUTO_INCREMENT

 

After inserting, get the new id that was generated

 

Then insert the 3 dates from your form into the renewals

 

You should never put POST values directly into a query. Sanitize them with mysqli_real_escape_string() or use a prepared statement.

is set up manually and probably will never change

Is the following right?

$newId = $mysqli->insert_id;

$sql = "INSERT INTO visitors (visitor_name, visitor_email, visitor_firstline, visitor_secondline,visitor_town, visitor_county, visitor_postcode, visitor_tel, visitor_mobile, visitor_model, visitor_plate) VALUES
('".mysqli_real_escape_string($visitor_name)."','".mysqli_real_escape_string($visitor_email)."','".mysqli_real_escape_string($visitor_firstline)."', '".mysqli_real_escape_string($visitor_secondline)."', '".mysqli_real_escape_string($visitor_town)."','".mysqli_real_escape_string($visitor_county)."','".mysqli_real_escape_string($visitor_postcode)."','".mysqli_real_escape_string($visitor_tel)."','".mysqli_real_escape_string($visitor_mobile)."','".mysqli_real_escape_string($visitor_model)."','".mysqli_real_escape_string($visitor_plate)."')";

"INSERT INTO item (item_id, description) VALUES
('".$_POST["item_id"]."','".mysqli_real_escape_string($description)."')";

"INSERT INTO renewals (visitor_id, item_id, renewal_date) VALUES
($newId, 1, '$insurance_date'),
($newId, 2, '$mot_date'),
($newId, 3, '$tax_date')";

Sort of getting there, got rid of most of the errors such as undefined variable

 

the last one I got is below

 

Notice: Undefined index: item_id in /home/sites/broadwaymediadesigns.co.uk/public_html/sites/the-tax-elephants/information-form.php on line 111

 

on line 111 is the following

('".$_POST["item_id"]."',mysqli_real_escape_string($description));

So far the whole coding looks like the following

<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
?>

<?php 
$title = "Information Form - The Tax Elephants";

$pgDesc="";

$pgKeywords="";

include ( 'includes/header.php' );
?>
<!--CONTENT-->
 
	<div id="column-left">
    <div class="form">
	<form action="" method="post">
<label>Name :</label>
<input type="text" name="visitor_name" required="required" placeholder="Please Enter Name"/>
<br /><br />
<label>Email :</label>
<input type="email" name="visitor_email" required="required" placeholder="john123@gmail.com"/>
<br/><br />
<label>Address Line 1 :</label>
<input type="text" name="visitor_firstline" required="required" placeholder="Please Enter First Line of your address"/>
<br><br>
<label>Address Line 2 :</label>
<input type="text" name="visitor_secondline" required="required" placeholder="Please Enter Second Line of your address"/>
<br><br>
<label>Town :</label>
<input type="text" name="visitor_town" required="required" placeholder="Please Enter your Town"/>
<br><br>
<label>County :</label>
<input type="text" name="visitor_county" required="required" placeholder="Please Enter Your County"/>
<br/><br />
<label>Postcode :</label>
<input type="text" name="visitor_postcode" required="required" placeholder="Please Enter Your Postcode"/>
<br><br>
<label>Telephone Number :</label>
<input type="text" name="visitor_tel" required="required" placeholder="Please Enter Your Telephone Number"/>
<br><br>
<label>Mobile Number :</label>
<input type="text" name="visitor_mobile" required="required" placeholder="Please Enter Your Mobile Number"/>
<br><br>
<label>Model of Car/Van :</label>
<input type="text" name="visitor_model" required="required" placeholder="Please Enter the Model of your Car or Van"/>
<br><br>
<label>License Plate Number :</label>
<input type="text" name="visitor_plate" required="required" placeholder="Please Enter your License Number Plate"/>
<br><br>
<label>Car Tax Renewal Date :</label>
<input type="text" id="datepicker" name="visitor_tax" required="required" placeholder="Please Enter your Car Tax Renewal Date"/>
<br><br>
<label>MOT Expiry Date :</label>
<input type="text" id="datepicker2" name="visitor_mot" required="required" placeholder="Please Enter your MOT Expiry Date"/>
<br><br>
<label>Insurance Expiry Date :</label>
<input type="text" id="datepicker3" name="visitor_insurance" required="required" placeholder="Please Enter your Insurance Expiry Date"/>
<br><br>
<input type="submit" value=" Submit " name="submit"/>
</form>
    </div>
    </div>
 
	<div id="column-right">
    
    </div>

<!--CONTENT-->

<?php
if(isset($_POST["submit"])){
$servername = "";
$username = "";
$password = "";
$dbname = "";
 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$visitor_name = '';
$visitor_email = '';
$visitor_firstline = '';
$visitor_secondline = '';
$visitor_town = '';
$visitor_county = '';
$visitor_postcode = '';
$visitor_tel = '';
$visitor_mobile = '';
$visitor_model = '';
$visitor_plate = '';

$newId = '';
$description = '';

$insurance_date = '';
$mot_date = '';
$tax_date = '';

$sql = "INSERT INTO visitors (visitor_name, visitor_email, visitor_firstline, visitor_secondline,visitor_town, visitor_county, visitor_postcode, visitor_tel, visitor_mobile, visitor_model, visitor_plate) VALUES
(mysqli_real_escape_string($visitor_name), mysqli_real_escape_string($visitor_email), mysqli_real_escape_string($visitor_firstline), mysqli_real_escape_string($visitor_secondline), mysqli_real_escape_string($visitor_town), mysqli_real_escape_string($visitor_county), mysqli_real_escape_string($visitor_postcode), mysqli_real_escape_string($visitor_tel), mysqli_real_escape_string($visitor_mobile), mysqli_real_escape_string($visitor_model), mysqli_real_escape_string($visitor_plate);

INSERT INTO item (item_id, description) VALUES
('".$_POST["item_id"]."',mysqli_real_escape_string($description));

INSERT INTO renewals (visitor_id, item_id, renewal_date) VALUES
($newId, 1, '$insurance_date'),
($newId, 2, '$mot_date'),
($newId, 3, '$tax_date')";
 

/*$sql = "INSERT INTO visitors (visitor_name, visitor_email, visitor_firstline, visitor_secondline, visitor_town, visitor_county, visitor_postcode, visitor_tel, visitor_mobile, visitor_model, visitor_plate, visitor_tax, visitor_mot, visitor_insurance)
VALUES ('".$_POST["visitor_name"]."','".$_POST["visitor_email"]."','".$_POST["visitor_firstline"]."', '".$_POST["visitor_secondline"]."', '".$_POST["visitor_town"]."','".$_POST["visitor_county"]."','".$_POST["visitor_postcode"]."','".$_POST["visitor_tel"]."','".$_POST["visitor_mobile"]."','".$_POST["visitor_model"]."','".$_POST["visitor_plate"]."','".$_POST["visitor_tax"]."','".$_POST["visitor_mot"]."','".$_POST["visitor_insurance"]."')";*/
 
if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('Your Information has been added successfully to our database');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}
 
$conn->close();
}
?>

<?php include( 'includes/footer.php' ); ?>

Think I sorted the undefined index issue but the form is not adding no data to the database, below is my coding

<?php
if(isset($_POST["submit"])){
$servername = "";
$username = "";
$password = "";
$dbname = "";
 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$visitor_name = '';
$visitor_email = '';
$visitor_firstline = '';
$visitor_secondline = '';
$visitor_town = '';
$visitor_county = '';
$visitor_postcode = '';
$visitor_tel = '';
$visitor_mobile = '';
$visitor_model = '';
$visitor_plate = ''; 

$item_id = ''; 
if( isset( $_POST['item_id'])) {
    $item_id = $_POST['item_id']; 
}

/*if(isset($_POST['item_id'])){ $item_id = $_POST['item_id']; }*/

$description = '';

$newId = '';
$insurance_date = '';
$mot_date = '';
$tax_date = '';

$sql = "INSERT INTO visitors (visitor_name, visitor_email, visitor_firstline, visitor_secondline,visitor_town, visitor_county, visitor_postcode, visitor_tel, visitor_mobile, visitor_model, visitor_plate) VALUES
(mysqli_real_escape_string($visitor_name), mysqli_real_escape_string($visitor_email), mysqli_real_escape_string($visitor_firstline), mysqli_real_escape_string($visitor_secondline), mysqli_real_escape_string($visitor_town), mysqli_real_escape_string($visitor_county), mysqli_real_escape_string($visitor_postcode), mysqli_real_escape_string($visitor_tel), mysqli_real_escape_string($visitor_mobile), mysqli_real_escape_string($visitor_model), mysqli_real_escape_string($visitor_plate);

INSERT INTO item (item_id, description) VALUES
(mysqli_real_escape_string($item_id), mysqli_real_escape_string($description);

INSERT INTO renewals (visitor_id, item_id, renewal_date) VALUES
($newId, 1, '$insurance_date'),
($newId, 2, '$mot_date'),
($newId, 3, '$tax_date')))";
 

/*$sql = "INSERT INTO visitors (visitor_name, visitor_email, visitor_firstline, visitor_secondline, visitor_town, visitor_county, visitor_postcode, visitor_tel, visitor_mobile, visitor_model, visitor_plate, visitor_tax, visitor_mot, visitor_insurance)
VALUES ('".$_POST["visitor_name"]."','".$_POST["visitor_email"]."','".$_POST["visitor_firstline"]."', '".$_POST["visitor_secondline"]."', '".$_POST["visitor_town"]."','".$_POST["visitor_county"]."','".$_POST["visitor_postcode"]."','".$_POST["visitor_tel"]."','".$_POST["visitor_mobile"]."','".$_POST["visitor_model"]."','".$_POST["visitor_plate"]."','".$_POST["visitor_tax"]."','".$_POST["visitor_mot"]."','".$_POST["visitor_insurance"]."')";*/
 
if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('Your Information has been added successfully to our database');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}
 
$conn->close();
}
?>

Sorry about this, am trying to work it out myself, thought it would of thrown up a error message if anything wrong with the coding etc.

I have changed the coding now to the following as realised I messed up the sql query coding

<?php
if(isset($_POST["submit"])){
$servername = "";
$username = "";
$password = "";
$dbname = "";
 
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// escape variables for security
$visitor_name = mysqli_real_escape_string($conn, $_POST['visitor_name']);
$visitor_email = mysqli_real_escape_string($conn, $_POST['visitor_email']);
$visitor_firstline = mysqli_real_escape_string($conn, $_POST['visitor_firstline']);
$visitor_secondline = mysqli_real_escape_string($conn, $_POST['visitor_secondline']);
$visitor_town = mysqli_real_escape_string($conn, $_POST['visitor_town']);
$visitor_county = mysqli_real_escape_string($conn, $_POST['visitor_county']);
$visitor_postcode = mysqli_real_escape_string($conn, $_POST['visitor_postcode']);
$visitor_tel = mysqli_real_escape_string($conn, $_POST['visitor_tel']);
$visitor_mobile = mysqli_real_escape_string($conn, $_POST['visitor_mobile']);
$visitor_model = mysqli_real_escape_string($conn, $_POST['visitor_model']);
$visitor_plate = mysqli_real_escape_string($conn, $_POST['visitor_plate']);

$description = mysqli_real_escape_string($conn, $_POST['description']);

$visitor_id = mysqli_real_escape_string($conn, $_POST['visitor_id']);
$insurance_date = mysqli_real_escape_string($conn, $_POST['insurance_date']);
$mot_date = mysqli_real_escape_string($conn, $_POST['mot_date']);
$tax_date = mysqli_real_escape_string($conn, $_POST['tax_date']);

$item_id = '$item_id'; 
if( isset( $_POST['item_id'])) {
    $item_id = $_POST['item_id']; 
}

/*if(isset($_POST['item_id'])){ $item_id = $_POST['item_id']; }*/

$sql = "INSERT INTO visitors (visitor_name, visitor_email, visitor_firstline, visitor_secondline,visitor_town, visitor_county, visitor_postcode, visitor_tel, visitor_mobile, visitor_model, visitor_plate) VALUES
('$visitor_name', '$visitor_email', '$visitor_firstline', '$visitor_secondline', '$visitor_town', '$visitor_county', '$visitor_postcode', '$visitor_tel', '$visitor_mobile', '$visitor_model', '$visitor_plate');

INSERT INTO item (item_id, description) VALUES
('$item_id', '$description');

INSERT INTO renewal (visitor_id, item_id, renewal_date) VALUES
($visitor_id, 1, '$insurance_date'),
($visitor_id, 2, '$mot_date'),
($visitor_id, 3, '$tax_date')";
 

/*$sql = "INSERT INTO visitors (visitor_name, visitor_email, visitor_firstline, visitor_secondline, visitor_town, visitor_county, visitor_postcode, visitor_tel, visitor_mobile, visitor_model, visitor_plate, visitor_tax, visitor_mot, visitor_insurance)
VALUES ('".$_POST["visitor_name"]."','".$_POST["visitor_email"]."','".$_POST["visitor_firstline"]."', '".$_POST["visitor_secondline"]."', '".$_POST["visitor_town"]."','".$_POST["visitor_county"]."','".$_POST["visitor_postcode"]."','".$_POST["visitor_tel"]."','".$_POST["visitor_mobile"]."','".$_POST["visitor_model"]."','".$_POST["visitor_plate"]."','".$_POST["visitor_tax"]."','".$_POST["visitor_mot"]."','".$_POST["visitor_insurance"]."')";*/
 
 /*echo $sql;*/
 
if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('Your Information has been added successfully to our database');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}
 
$conn->close();
}
?>

I am just getting the following errors now

 

Notice: Undefined index: description in /home/sites/broadwaymediadesigns.co.uk/public_html/sites/the-tax-elephants/information-form.php on line 101

Notice: Undefined index: visitor_id in /home/sites/broadwaymediadesigns.co.uk/public_html/sites/the-tax-elephants/information-form.php on line 103

Notice: Undefined index: insurance_date in /home/sites/broadwaymediadesigns.co.uk/public_html/sites/the-tax-elephants/information-form.php on line 104

Notice: Undefined index: mot_date in /home/sites/broadwaymediadesigns.co.uk/public_html/sites/the-tax-elephants/information-form.php on line 105

Notice: Undefined index: tax_date in /home/sites/broadwaymediadesigns.co.uk/public_html/sites/the-tax-elephants/information-form.php on line 106

 

Sorry

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.