ianhaney
10-18-2016, 03:17 PM
Hi
I have designed a email script that pings out a email when a date is within 7 days and then it updates the date_notified column in the database but just noticed it is updating all the records with the current date and time but only need it to update the records that has a email sent for the date that is within 7 days, hope that makes sense
below is the coding I put together
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
?>
<html>
<title>Automatic Email</title>
<body>
<?php
$db = mysqli_connect("" , "", "") or die("Check connection parameters!");
// Optionally skip select_db and use: mysqli_connect(host,user,pass,dbname)
mysqli_select_db($db,"") or die(mysqli_error($db));
if (mysqli_connect_error()) {
die ('Failed to connect to MySQL');
} else {
/*SUCCESS MSG*/
echo '';
}
$sqlCommand = "SELECT
u.id
, domain_name_owner
, url
, DATE_FORMAT(domain_expiry_date, '%e %M %Y') as domain_expiry_date
, domain_owner_email
FROM websites u
WHERE domain_expiry_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 14 DAY
AND IFNULL(date_notified_of_domain_expiry, '1901-01-01') < CURDATE()-INTERVAL 14 DAY
UNION
SELECT
u.id
, domain_name_owner
, url
, DATE_FORMAT(domain_expiry_date, '%e %M %Y') as domain_expiry_date
, domain_owner_email
FROM websites u
WHERE domain_expiry_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY
AND IFNULL(date_notified_of_domain_expiry, '1901-01-01') < CURDATE()-INTERVAL 7 DAY
";
$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));
//fetch tha data from the database
$current_visitor=0;
$current_email = '';
$headers = "From: email@example.co.uk\r\n";
$subject = "Domain Name Expiry Date(s)";
$message = '';
$renewals = array();
$notifications = array();
//fetch the data from the database
while ($row = mysqli_fetch_array($query)) {
// has visitor_id changed
if ($row['id'] != $current_visitor) {
// send email to current visitor
if ($current_visitor != 0) {
$to = $current_email;
$sendmail = mail($to, $subject, $message, $headers);
if ($sendmail) {
echo nl2br($message);
echo "<b>Email Successfully Sent</b><br><br>";
// success, so add renewal ids to notifications
$notifications = array_merge($notifications,$renewals);
} else {
echo "<b>Error in Sending of Email to $to</b><br><br>";
}
}
$current_visitor = $row['id'];
$current_email = $row['domain_owner_email'];
$message = "Domain Name Owner: {$row['domain_name_owner']} \n\n";
$renewals = array();
}
$message .= "Your Domain Name {$row['url']} expiry date is: {$row['domain_expiry_date']}\n";
}
// send email to final visitor
if ($current_visitor != 0) {
$to = $current_email;
$sendmail = mail($to, $subject, $message, $headers);
if ($sendmail) {
echo nl2br($message);
echo "<b>Email Successfully Sent</b><br><br>";
// success, so add to notifications
$notifications = array_merge($notifications,$renewals);
} else {
echo "<b>Error in Sending of Email to $to</b><br><br>";
}
}
// update successful notifications
$id = $notifications;
$sql = "UPDATE websites SET date_notified_of_domain_expiry = NOW() WHERE id=$id"; ---->line 116
$db->query($sql) ;
// Free the results
mysqli_free_result($query);
//close the connection
mysqli_close($db);
?>
</body>
</html>
with the code as it is, I get the following error
Notice: Array to string conversion in domain-expiry-auto-email.php on line 116
I know I need to adjust the WHERE code but not sure what to, sorry
I have designed a email script that pings out a email when a date is within 7 days and then it updates the date_notified column in the database but just noticed it is updating all the records with the current date and time but only need it to update the records that has a email sent for the date that is within 7 days, hope that makes sense
below is the coding I put together
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
?>
<html>
<title>Automatic Email</title>
<body>
<?php
$db = mysqli_connect("" , "", "") or die("Check connection parameters!");
// Optionally skip select_db and use: mysqli_connect(host,user,pass,dbname)
mysqli_select_db($db,"") or die(mysqli_error($db));
if (mysqli_connect_error()) {
die ('Failed to connect to MySQL');
} else {
/*SUCCESS MSG*/
echo '';
}
$sqlCommand = "SELECT
u.id
, domain_name_owner
, url
, DATE_FORMAT(domain_expiry_date, '%e %M %Y') as domain_expiry_date
, domain_owner_email
FROM websites u
WHERE domain_expiry_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 14 DAY
AND IFNULL(date_notified_of_domain_expiry, '1901-01-01') < CURDATE()-INTERVAL 14 DAY
UNION
SELECT
u.id
, domain_name_owner
, url
, DATE_FORMAT(domain_expiry_date, '%e %M %Y') as domain_expiry_date
, domain_owner_email
FROM websites u
WHERE domain_expiry_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY
AND IFNULL(date_notified_of_domain_expiry, '1901-01-01') < CURDATE()-INTERVAL 7 DAY
";
$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));
//fetch tha data from the database
$current_visitor=0;
$current_email = '';
$headers = "From: email@example.co.uk\r\n";
$subject = "Domain Name Expiry Date(s)";
$message = '';
$renewals = array();
$notifications = array();
//fetch the data from the database
while ($row = mysqli_fetch_array($query)) {
// has visitor_id changed
if ($row['id'] != $current_visitor) {
// send email to current visitor
if ($current_visitor != 0) {
$to = $current_email;
$sendmail = mail($to, $subject, $message, $headers);
if ($sendmail) {
echo nl2br($message);
echo "<b>Email Successfully Sent</b><br><br>";
// success, so add renewal ids to notifications
$notifications = array_merge($notifications,$renewals);
} else {
echo "<b>Error in Sending of Email to $to</b><br><br>";
}
}
$current_visitor = $row['id'];
$current_email = $row['domain_owner_email'];
$message = "Domain Name Owner: {$row['domain_name_owner']} \n\n";
$renewals = array();
}
$message .= "Your Domain Name {$row['url']} expiry date is: {$row['domain_expiry_date']}\n";
}
// send email to final visitor
if ($current_visitor != 0) {
$to = $current_email;
$sendmail = mail($to, $subject, $message, $headers);
if ($sendmail) {
echo nl2br($message);
echo "<b>Email Successfully Sent</b><br><br>";
// success, so add to notifications
$notifications = array_merge($notifications,$renewals);
} else {
echo "<b>Error in Sending of Email to $to</b><br><br>";
}
}
// update successful notifications
$id = $notifications;
$sql = "UPDATE websites SET date_notified_of_domain_expiry = NOW() WHERE id=$id"; ---->line 116
$db->query($sql) ;
// Free the results
mysqli_free_result($query);
//close the connection
mysqli_close($db);
?>
</body>
</html>
with the code as it is, I get the following error
Notice: Array to string conversion in domain-expiry-auto-email.php on line 116
I know I need to adjust the WHERE code but not sure what to, sorry