I am building a script that checks for dates that will expire within 7 days and sends out a email reminder to the admin owner and also update a date notified column in the database, I have got it sort of working, it updates the date notified column and sends the email out but it only lists one date in the email where as there should be two as I set two dates to expire within 7 days, can anyone help please, below is the coding I have
<?php
$db = mysqli_connect("localhost" , "", "") 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_expiration_date
, domain_owner_email
FROM websites u
WHERE domain_expiry_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY
";
$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));
//fetch the data from the database
while ($row = mysqli_fetch_array($query)) {
$arr_ids[] = $row['id'];
$email = '';
$headers = "From: noreply#domain.co.uk\r\n";
$subject = "Domain Name Expiry Date(s)";
$message = '';
$id = $row['id'];
$owner = $row['domain_name_owner'];
$email = $row['domain_owner_email'];
$message = "Domain Name Owner: {$row['domain_name_owner']} \n\n";
$message .= "Your Domain Name {$row['url']} expiry date is: {$row['domain_expiration_date']}\n";
$to = $email;
$sendmail = mail($to, $subject, $message, $headers);
if ($sendmail) {
echo nl2br($message);
echo "<b>Email Successfully Sent</b><br><br>";
} else {
echo "<b>Error in Sending of Email to $to</b><br><br>";
}
}
if (isset($arr_ids)){
$sql = "UPDATE websites SET date_notified_of_domain_expiry = NOW() WHERE id IN (";
$sql .= implode("," , $arr_ids);
$sql .= ");";
print $sql;
}
//$db->query($sql);
$db->query($sql) or die(mysqli_error($db));
// Free the results
mysqli_free_result($query);
//close the connection
mysqli_close($db);
?>
Thank you in advance
Your Script is working fine, it should send one email per expiry date (based on your code), if the domain owner owns two domains, he will receive two separate emails that each one of them is related to a certain domain.
If you want to send the two domains on the same email, use the below script:
<?php
$db = mysqli_connect("localhost" , "", "") 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_expiration_date
, domain_owner_email
FROM websites u
WHERE domain_expiry_date BETWEEN CURDATE() AND CURDATE()+INTERVAL 7 DAY
";
$query = mysqli_query($db, $sqlCommand) or die (mysqli_error($db));
//fetch the data from the database
$message = '';
$email = '';
$prev_Email ='';
while ($row = mysqli_fetch_array($query)) {
$arr_ids[] = $row['id'];
$prev_Email = $email;
$headers = "From: noreply#domain.co.uk\r\n";
$subject = "Domain Name Expiry Date(s)";
$id = $row['id'];
$owner = $row['domain_name_owner'];
$email = $row['domain_owner_email'];
if($prev_Email == ""){
$prev_Email = $email;
}
if($email == $prev_email && $email != ""){
$message. = "Domain Name Owner: {$row['domain_name_owner']} \n\n";
$message .= "Your Domain Name {$row['url']} expiry date is: {$row['domain_expiration_date']}\n";
}else{
if($prev_Email != ""){
$to = $prev_Email;
$sendmail = mail($to, $subject, $message, $headers);
if ($sendmail) {
echo nl2br($message);
echo "<b>Email Successfully Sent</b><br><br>";
} else {
echo "<b>Error in Sending of Email to $to</b><br><br>";
}
$message = "Domain Name Owner: {$row['domain_name_owner']} \n\n";
$message .= "Your Domain Name {$row['url']} expiry date is: {$row['domain_expiration_date']}\n";
}
}
}
if (isset($arr_ids)){
$sql = "UPDATE websites SET date_notified_of_domain_expiry = NOW() WHERE id IN (";
$sql .= implode("," , $arr_ids);
$sql .= ");";
print $sql;
}
//$db->query($sql);
$db->query($sql) or die(mysqli_error($db));
// Free the results
mysqli_free_result($query);
//close the connection
mysqli_close($db);
?>
Related
if(isset($_POST["request"])){
$email = $_POST['email'];
$p_name = $_POST['p_name'];
$noc = $_POST['noc'];
$year = $_POST['year'];
$get_email = "select email from book where email = '$email' ";
$run_email = mysqli_query($con,$get_email);
$check = mysqli_num_rows($run_email);
if($check==1){
echo "<script>alert('You Have Already Booked') </script>";
exit();
}
$get_name = "SELECT capacity from party where type_party='$p_name' ";
$run_name = mysqli_query($con,$get_name);
$checkk = $run_name > '$noc';
if(checkk){
echo "<script>alert('Out Of Bound') </script>";
exit();
}
How to Compare capacity from party table with that the value of $noc??
Please help me out. Thank You!
You have missed the $ in last condition.
<?php
$email = $_POST['email'];
$p_name = $_POST['p_name'];
$noc = $_POST['noc'];
$year = $_POST['year'];
$get_email = "select email from book where email = '$email' ";
$run_email = mysqli_query($con,$get_email);
$check = mysqli_num_rows($run_email);
if($check==1){
echo "<script>alert('You Have Already Booked') </script>";
exit();
}
$get_name = "SELECT capacity from party where type_party='$p_name' ";
$run_name = mysqli_query($con,$get_name);
$checkname = mysqli_num_rows($run_name );
//$checkk = $run_name > '$noc';
if($checkname > $noc){
echo "<script>alert('Out Of Bound') </script>";
exit();
}
?>
Just change you code like below.
$get_name = "SELECT capacity from party where type_party='$p_name' ";
$run_name = mysqli_query($con,$get_name);
$row_cnt = mysqli_num_rows($run_name);
$rows = mysqli_fetch_assoc($run_name );
// please use any one condition which meet your requirement.
$checkk = $run_name > $noc;
// or
$checkk = $rows['capacity'] > $noc;
// if it's not working try below
$checkk = $rows[0]['capacity'] > $noc;
if($checkk){
echo "<script>alert('Out Of Bound') </script>";
exit();
}
I get the number of results found from the query using "mysqli_num_rows" and then compare with $noc for proper result. Or else use the second condition to compare $noc with "$run_name['capacity']".
i was using this codes to send newsletter to members , now the problem is that when i send emails it doesn't send . i tracked the emails in cpanel and found that the email address has the word 'array' before it . that means that when i send to 'email#domain.com' it changes to 'arrayemail#domain.com'. and this is the code :
<?php
include("../include/config.php");
include_once("../include/functions/import.php");
verify_login_admin();
$adminurl = $config['adminurl'];
$thebaseurl = $config['baseurl'];
$sql = "SELECT USERID, username, email FROM members";
$executequery = $conn->Execute($sql);
$results= $executequery->getrows();
if($_POST['submitform'] == "1")
{
if(isset($_POST['USERID']))
{
foreach($_POST['USERID'] as $key)
{
$subject = $_POST['subject'];
$sendername = $config['site_name'];
//$bodymessage = "Dear " . $_POST['username'];
//$sendmailbody = "HI". $results[$i]. $key . $results[$i].$_POST['username'] .",";
$sendmailbody .= $_POST['message'];
$sendmailbody .= "";
$from = $config['site_name'].'<'.$config['site_email'].'>';
$sendto = $results[$i]. $key.',';
mailme($sendto,$sendername,$from,$subject,$sendmailbody,$bcc);
}
}
$message = "E-Mails / Newsletters sent successfully.";
Stemplate::assign('message',$message);
}
$mainmenu = "7";
$submenu = "2";
$bodymsg = $_POST['message'];
Stemplate::assign('subject',$subject);
Stemplate::assign('bodymsg',$bodymsg);
Stemplate::assign('mainmenu',$mainmenu);
Stemplate::assign('submenu',$submenu);
Stemplate::assign('results',$results);
STemplate::display("administrator/global_header.tpl");
STemplate::display("administrator/mass_newsletter.tpl");
STemplate::display("administrator/global_footer.tpl");
?>
It looks like you are getting all rows from your query:
$results= $executequery->getrows();
^ plural
But you never actually loop over all rows, so $results[$i] will contain one complete row from the result set: An array containing the USERID, username and email.
And when you concatenate an array with a string, the array will result in the text array.
And that is assuming that $i is an integer but in your code it is not defined at all.
You probably want to loop over the result-set somewhere and if you use a foreach you will not need the $i variable at all:
foreach ($results as $result) {
// Build your e-mail addresses string?
...
}
The $time variable here displays as null when I use var_dump. Yet in the db the time shows as 9:00 Am. I am trying to email the time that the passenger wants to be picked up. Why is $time null and how do I fix this?
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$host="localhost"; // Host name
$username="user"; // Mysql username
$password="password"; // Mysql password
$db_name="mydb"; // Database name
$tbl_name="pickuprequests"; // Table name
// Connect to server and select database.
$link = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name", $link)or die("cannot select DB");
//names of the passengers in option tags
$name = $_POST['name'];
//this query gets all the records from pickuprequests of the passenger selected
$query="SELECT * FROM $tbl_name WHERE name = '$name'";
$result=mysql_query($query, $link);
// get the location of the passenger from $result
$search_loc = mysql_fetch_array($result);
$loc = $search_loc['location'];
//get time of pickup
$search_time = mysql_fetch_array($result);
$time = $search_time['time'];
//find how many rows are returned with the passenger name
$search_row = mysql_query($query, $link);
$count = mysql_num_rows($search_row);
//deletes a record assoc with passenger name
$delete_query = "DELETE FROM pickuprequests WHERE name = '$name'";
//message for the passenger
$msg_passenger = "Your driver is on the way! You will receive one last email when the driver arrives.";
$subject_passenger = "Driver on the way";
$mailheaders = "MIME-Version: 1.0"."\r\n";
$mailheaders .= "Content-type: text/html; charset=iso-8859-1"."\r\n";
$mailheaders .= "From: TripoZipo <tripozipo.com>"."\r\n";
//passengers email in diff table assoc with name selected from option tags
$query_email = "SELECT * FROM tzmember WHERE name = '$name'";
$get_email = mysql_query($query_email,$link);
$search_email = mysql_fetch_array($get_email);
$email = $search_email['email'];
//message for the driver
$msg_driver = sprintf(
"
<html>
<body>
<table border='1'>
<tr>
<td>Passenger</td><td>Location</td><td>Time</td>
</tr>
<tr>
<td>%s</td><td>%s</td><td>%s</td>
</tr>
</table>
</body>
</html>
",
$name,
$loc, $time);
$subject_driver = "Passenger Location";
//drivers email is the session name for driver.php
$driver = $_SESSION['driver'];
//on submit and if a passenger is selected run code
if(isset($_POST['submit']) && isset($_POST['name'])) {
//$count returns all the rows assoc with passenger name
if($count) {
//mail($email, $subject_passenger, $msg_passenger, $mailheaders); // email passenger your ride is on the way
mail($driver, $subject_driver, $msg_driver, $mailheaders); // email driver the location
//mysql_query($delete_query, $link); // finally delete the request as its not pending anymore
//echo "<script type='text/javascript'> document.location = 'http://mobile.tripozipo.com/confirmation2.php'; </script>";
}
else { //else if no rows are returned
}
}
?>
A call to mysql_fetch_array returns a complete ROW from the result set, not just a single column. So you only need to call it once instead of evert time you try and get a value from the row.
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$host="localhost"; // Host name
$username="username"; // Mysql username
$password="password"; // Mysql password
$db_name="dbname"; // Database name
$tbl_name="pickuprequests"; // Table name
// Connect to server and select database.
$link = mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name", $link)or die("cannot select DB");
//names of the passengers in option tags
$name = $_POST['name'];
//this query gets all the records from pickuprequests of the passenger selected
$query="SELECT * FROM $tbl_name WHERE name = '$name'";
$result=mysql_query($query, $link);
// get the location of the passenger from $result
$row = mysql_fetch_array($result);
$loc = $row['location'];
//get time of pickup
//$search_time = mysql_fetch_array($result);
$time = $row['time'];
//find how many rows are returned with the passenger name
//$search_row = mysql_query($query, $link);
$count = mysql_num_rows($result);
//deletes a record assoc with passenger name
$delete_query = "DELETE FROM pickuprequests WHERE name = '$name'";
//message for the passenger
$msg_passenger = "Your driver is on the way! You will receive one last email when the driver arrives.";
$subject_passenger = "Driver on the way";
$mailheaders = "MIME-Version: 1.0"."\r\n";
$mailheaders .= "Content-type: text/html; charset=iso-8859-1"."\r\n";
$mailheaders .= "From: TripoZipo <tripozipo.com>"."\r\n";
//passengers email in diff table assoc with name selected from option tags
$query_email = "SELECT * FROM tzmember WHERE name = '$name'";
$get_email = mysql_query($query_email,$link);
$search_email = mysql_fetch_array($get_email);
$email = $search_email['email'];
//message for the driver
$msg_driver = sprintf(
"
<html>
<body>
<table border='1'>
<tr>
<td>Passenger</td><td>Location</td><td>Time</td>
</tr>
<tr>
<td>%s</td><td>%s</td><td>%s</td>
</tr>
</table>
</body>
</html>
",
$name,
$loc, $time);
$subject_driver = "Passenger Location";
//drivers email is the session name for driver.php
$driver = $_SESSION['driver'];
//on submit and if a passenger is selected run code
if(isset($_POST['submit']) && isset($_POST['name'])) {
//$count returns all the rows assoc with passenger name
if($count) {
//mail($email, $subject_passenger, $msg_passenger, $mailheaders); // email passenger your ride is on the way
mail($driver, $subject_driver, $msg_driver, $mailheaders); // email driver the location
//mysql_query($delete_query, $link); // finally delete the request as its not pending anymore
//echo "<script type='text/javascript'> document.location = 'http://mobile.tripozipo.com/confirmation2.php'; </script>";
}
else { //else if no rows are returned
}
}
?>
Please dont use the mysql_ database extensions, it is deprecated (gone for ever in PHP7)
Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions,
and here is some help to decide which to use
Hope all of my experts are fine. Buddy's i stuck in a very simple code. Actually i have to insert a form and then fetch values from it to send mail to the user who fills the form. All the values are inserting into the database and also fetch from database but mail is not sending. The same code was sending mail one day ago. But today it is not sending mail. Please help me out in this.
<?php
require("dbconnect.php");
require("DBConnection.php");
session_start();
if(isset($_POST['postadd'])){
$title = $_POST['adtitle'];
$area = $_POST['area'];
$addesc = $_POST['addesc'];
$email = $_POST['email'];
$showemail = $_POST['showemail'];
$userpic = ($_FILES['pic1']['tmp_name']);
$compath = "UploadPictures/".md5($_FILES['pic1']['name']);
$comFileType=$_FILES['pic1']['type'];
$comFileSize=$_FILES['pic1']['size'];
$comFileSize=$comFileSize/1024;
if($comFileSize<1000)
{
$arrFileType=array("image/jpeg","image/png","image/gif","image/bmp");
if(in_array($comFileType,$arrFileType))
{
move_uploaded_file($userpic,$compath);
}
else
{
("Invalid Image Format");
}
}
else
{
("File Size Error");
}
$pic2 = ($_FILES['pic2']['tmp_name']);
$compath2 = "UploadPictures/".md5($_FILES['pic2']['name']);
$comFileType2=$_FILES['pic2']['type'];
$comFileSize2=$_FILES['pic2']['size'];
$comFileSize2=$comFileSize2/1024;
if($comFileSize2<1000)
{
$arrFileType2=array("image/jpeg","image/png","image/gif","image/bmp");
if(in_array($comFileType2,$arrFileType2))
{
move_uploaded_file($pic2,$compath2);
}
else
{
("Invalid Image Format");
}
}
else
{
("File Size Error");
}
$pic3 = ($_FILES['pic3']['tmp_name']);
$compath3 = "UploadPictures/".md5($_FILES['pic2']['name']);
$comFileType3=$_FILES['pic3']['type'];
$comFileSize3=$_FILES['pic3']['size'];
$comFileSize3=$comFileSize3/1024;
if($comFileSize3<1000)
{
$arrFileType3=array("image/jpeg","image/png","image/gif","image/bmp");
if(in_array($comFileType3,$arrFileType3))
{
move_uploaded_file($pic3,$compath3);
}
else
{
("Invalid Image Format");
}
}
else
{
("File Size Error");
}
$pic4 = ($_FILES['pic4']['tmp_name']);
$compath4 = "UploadPictures/".md5($_FILES['pic4']['name']);
$comFileType4=$_FILES['pic4']['type'];
$comFileSize4=$_FILES['pic4']['size'];
$comFileSize4=$comFileSize4/1024;
if($comFileSize4<1000)
{
$arrFileType4=array("image/jpeg","image/png","image/gif","image/bmp");
if(in_array($comFileType4,$arrFileType4))
{
move_uploaded_file($pic4,$compath4);
}
else
{
("Invalid Image Format");
}
}
else
{
("File Size Error");
}
$agree = $_POST['checkbox'];
$subcat = $_SESSION['subcat'];
$cat = $_SESSION['cat'];
$rand = rand();
$datecreated = date("Y-m-d h:i:s");
$obj = new DBConnection();
$arr_Field = array("title","location","post","email","radio","pic1","pic2","pic3","pic4","agree","cat","subcat","random","datecreated");
$arr_values = array("$title","$area","$addesc","$email","$showemail","$compath","$compath2","$compath3","$compath4","$agree", "$cat", "$subcat" ,"$rand","$datecreated");
$obj->InsertRecord("ads",$arr_Field,$arr_values) or die (mysql_error());
$object = new DBConnection();
$condition = "ORDER BY id DESC LIMIT 1";
$selquery = $object->SelectRecord(array("*"),"ads","$condition") or die(mysql_error());
while($get = mysql_fetch_array($selquery)){
$email = $get['email'];
$id = $get['id'];
}
//echo $email;
//exit();
$to = $email;
$subject = "Admin";
$message = "
<html>
<head>
<title>Admin</title>
</head>
<body>
<p>Please Click on this Link to verify your post</p>
<p><a href='http://almughnisolutions.com/almughniclassified/summary.php?summary=".$id."'>http://almughnisolutions.com/almughniclassified/summary.php?summary=".$id."</a></p>
</body>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
// More headers
$headers .= 'From: <admin#almughniclassfied.com>' . "\r\n";
mail($to,$subject,$message,$headers) or die("Mail Cannot sent");
//header("Location:verifyadd.php");
}
?>
It would probably be a safe assumption that your code is not to blame. A cursory review of it seems like it shuold work if the SMPT server is behaving correctly. I would recommend testing the server. There are a some web based tools to do this:
https://www.wormly.com/test_smtp_server
You could also just use telnet to test from your machine if it's something that will need to be done internally:
http://technet.microsoft.com/en-us/library/aa995718%28v=exchg.65%29.aspx
i want to send the mail , when product got expire from before and ondate and after day, in php i was used datediff mysql function with php, but if product expire date comes like 31-1-2012 , the differ value is not suit for my coding , plz help how to solve it ,
This is my coding
<?php
$sql = mysql_query("SELECT * FROM supplier_product_det");
$results = mysql_num_rows($sql);
//echo '<script>alert("'.$results.'")</script>';
if ($results > 0)
{
for($i=0;$i<$results;$i++)
{
while($rows = mysql_fetch_array($sql))
{
/* print_r($rows['expired_on']);?><br /> <?*/
$exdate = $rows['expired_on'];
$curdate = date('Y-m-d');
$datedif = mysql_query("select datediff('".$curdate."','".$exdate."')");
while( $date = mysql_fetch_array($datedif) )
{
//echo $date['0'];
//echo $rows['pro_code'];
if (($date['0'])== 1)
{
if(($rows['mailcount'])!== $curdate)
{
$to = $rows['supplier'];
$subject = "Product Expire Intimation";
$message = "Dear Mr/Miss/Mrs
The Following Your Product Expired
Product Code:".$rows['pro_code']."
Product Name:".$rows['product_name']."
Product Expire Date:".$rows['expired_on']."";
$from = "tone#bgrow.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
$checkdate = mysql_query("update supplier_product_det set mailcount ='".$curdate."' where id='".$rows['id']."'") or die(mysql_error());
}
//echo $rows['supplier'];
}
if (($date['0'])== 0)
{
if(($rows['mailcount'])!== $curdate)
{
$to = $rows['supplier'];
$subject = "Product Expire Intimation";
$message = "Dear Mr/Miss/Mrs
The Following Your Product Expired
Product Code:".$rows['pro_code']."
Product Name:".$rows['product_name']."
Product Expire Date:".$rows['expired_on']."";
$from = "tone#bgrow.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
$checkdate = mysql_query("update supplier_product_det set mailcount ='".$curdate."' where id='".$rows['id']."'") or die(mysql_error());
}
//echo $rows['supplier'];
}
if (($date['0'])== -1)
{
if(($rows['mailcount'])!== $curdate)
{
$to = $rows['supplier'];
$subject = "Product Expire Intimation";
$message = "Dear Mr/Miss/Mrs
The Following Your Product will Expire Today
Product Code:".$rows['pro_code']."
Product Name:".$rows['product_name']."
Product Expire Date:".$rows['expired_on']."";
$from = "tone#bgrow.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
$checkdate = mysql_query("update supplier_product_det set mailcount ='".$curdate."' where id='".$rows['id']."'") or die(mysql_error());
}
//echo $rows['supplier'];
}
}
}
}
}
?>
Write your second select query as follows
SELECT DATEDIFF($exdate, CURRENT_DATE) as diff;
Also, no need to write separate select query you can get same column in first query itself as
SELECT column1, column2, column3, DATEDIFF(expired_on, CURRENT_DATE) as diff
FROM supplier_product_det
You may need to change position of expired_on and CURRENT_DATE to get correct diff value
Try something like this...
PHP: $now = date('Y-m-d', strtotime('now'));
-
SQL: WHERE $now >= DATE_FORMAT(expires, "%Y-%m-%d")