PHP mail sending when product got expire - php

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")

Related

array is added to email address

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?
...
}

php email reminder script issue

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);
?>

cron job, birthday check, and sending email (just making sure)

I have created send_bday_email.php which send birthday email 2 weeks prior to customer's birthday.
<?php
$today = date('Y-m-d');
$todayMD = date('m-d');
include_once "../../mysqli_connect.php";
$sql= mysqli_query($dbc, "SELECT * FROM Customer_Info");
$mail_body = '';
while ($row= mysqli_fetch_array($sql)) {
$id = $row["id"];
$email = $row["email"];
$birthday = $row['birthday'];
$bday = new DateTime($birthday);
$bday->sub(new DateInterval('P14D'));
$twoWeekbday = $bday->format('m-d');
if ($todayMD==$twoWeekbday) {
if(empty($row["firstName"])){
$name = 'Customer';
} else {
$name = $row["firstName"];
}
$mail_body = file_get_contents('../forms/bday_email_form.html');
}
}
This is my part of code which has two weeks prior and if customer's two week prior is equal to today send birthday email.
If I run this code daily using cron job, will it send emails perfectly?
I wanted to make sure before I actually test it out with my customer.

Error message: You have an error in your SQL syntax; check [duplicate]

This question already has answers here:
How can I prevent SQL injection in PHP?
(27 answers)
Closed 7 years ago.
Please help! I'm pretty new to this so this problem completely baffles me!
Error message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's Black Son',twitter_account_date='2015-10-21 22:36:06' WHERE twitter_id='257771' at line 1
Code:
<?php
//ini_set('display_errors', 0);
date_default_timezone_set('GMT');
require_once('config.php');
require("twitter/twitteroauth.php");
$date=date('Y-m-d H:i:s');
$time=date('H:i:s');
/* MarketPlace Email Notification*/
$sql_t="SELECT description FROM email_template WHERE template_id=15";
$result_tmp=mysql_query($sql_t) or die(mysql_error());
$roT=mysql_fetch_array($result_tmp);
$tempL=$roT['description'];
$sql_m="SELECT t.screen_name,t.seller_twitter_account_id,s.email,t.created_at,t.mail_status FROM seller_twitter_account as t LEFT JOIN seller_master as s ON t.seller_id=s.seller_id WHERE t.tweet_price=0";
$result_m=mysql_query($sql_m) or die(mysql_error());
while($roM=mysql_fetch_array($result_m)){
$screen_name=$roM['screen_name'];
$twitter_act_id=$roM['seller_twitter_account_id'];
$email=$roM['email'];
$mail_status=$roM['mail_status'];
$date1=strtotime($roM['created_at']);
$date2=strtotime(date('Y-m-d H:i:s'));
$diffHours = round(($date2 - $date1) / 60);
if($diffHours>=1440 && $mail_status==0){
$to = $email;
$subject = "Add a Tweet Price";
$message = str_replace("[SCREEN_NAME]",$screen_name,$tempL);
$header = "From:info#tweetvend.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true )
{
echo "Message sent successfully...";
mysql_query("UPDATE seller_twitter_account set mail_status=1 WHERE seller_twitter_account_id='$twitter_act_id'");
}
else
{
echo "Message could not be sent...";
}
}
if($diffHours>=10080 && $mail_status==1){
$to = $email;
$subject = "Add a Tweet Price";
$message = str_replace("[SCREEN_NAME]",$screen_name,$tempL);
$header = "From:info#tweetvend.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true )
{
echo "Message sent successfully...";
mysql_query("UPDATE seller_twitter_account set mail_status=2 WHERE seller_twitter_account_id='$twitter_act_id'");
}
else
{
echo "Message could not be sent...";
}
}
}
/* END code*/
/*UPDATE TWITTER ACCOUNT CODE*/
$sql_tac="SELECT screen_name,oauth_access_token,oauth_access_token_secret,twitter_account_date FROM seller_twitter_account";
$result_TAC=mysql_query($sql_tac);
while($roWTA=mysql_fetch_array($result_TAC))
{
$twitterAccountDate=$roWTA['twitter_account_date'];
$day1 = strtotime(date("Y:m:d H:i:s"));
$day2 = strtotime($twitterAccountDate);
$diffHours = round(($day1 - $day2) / 3600);
if($diffHours>=24){
$screen_name=$roWTA['screen_name'];
$oauth_access_token =$roWTA['oauth_access_token'];
$oauth_access_token_secret=$roWTA['oauth_access_token_secret'];
$consumer_key='37pRttXuKrGZawYsNp6Tu6DSL';
$consumer_secret='566TXt7ldNDJFkZazshwhgy3JILh104DP4KIUyrSguH5MZ54o1';
$request = array(
'screen_name' => $screen_name,
'count' => '1'
);
$twitterObj = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_access_token, $oauth_access_token_secret);
$twitterInfo=$twitterObj->get('statuses/user_timeline',$request);
//echo '<pre>';
//print_r($twitterInfo);
//$twitterInfo->error==''
if(!empty($twitterInfo)){
//if(isset($twitterInfo->errors) && count($twitterInfo->errors)==0){
if($twitterInfo->errors[0]->message==''){
$twitter_id=$twitterInfo[0]->user->id;
$name=$twitterInfo[0]->user->name;
$num_of_followers=$twitterInfo[0]->user->followers_count;
$num_of_followings=$twitterInfo[0]->user->friends_count;
$num_of_tweets=$twitterInfo[0]->user->statuses_count;
$twitter_profile_pic_url=$twitterInfo[0]->user->profile_image_url;
$sqltUpdt="UPDATE seller_twitter_account SET num_of_followers='$num_of_followers', num_of_followings='$num_of_followings', num_of_tweets='$num_of_tweets', twitter_profile_pic_url='$twitter_profile_pic_url', twitter_name='$name',twitter_account_date='$date' WHERE twitter_id='$twitter_id'";
mysql_query($sqltUpdt) or die(mysql_error());
}
}
}
}
/*END TWITTER ACCOUNT CODE*/
/*Tweet Approved Automatically*/
/*$sql_tweet="SELECT od.order_id,m.item_id,t.tweet_asap,t.retweet_asap,t.tweet_post_date_time,t.retweet_post_date,t.tweet_order_date from order_master as od, item_order_master as m,tweet_order as t WHERE od.order_id=m.order_id AND m.item_id=t.item_id AND m.tweet_status=0 AND od.order_completed=1 AND od.is_active=1 group by m.item_id";
$tweetResult=mysql_query($sql_tweet);
while($rows=mysql_fetch_array($tweetResult)){
$tweet_asap=$rows['tweet_asap'];
$retweet_asap=$rows['retweet_asap'];
$tweet_post_date=$rows['tweet_post_date_time'];
$retweet_post_date=$rows['retweet_post_date'];
$tweet_asap_post_date=$rows['tweet_order_date'];
$orderId=$rows['order_id'];
$itemId=$rows['item_id'];
if($tweet_asap==1 || $retweet_asap==1){
$day1 = strtotime(date("Y:m:d H:i:s"));
$day2 = strtotime($tweet_asap_post_date);
$diffHours = round(($day1 - $day2) / 3600);
if($diffHours>=48){
$sql_up="UPDATE item_order_master set tweet_status=1 WHERE item_id='$itemId'";
mysql_query($sql_up) or die(mysql_error());
$sql_upp="UPDATE tweet_order set tweet_order_date='$date' WHERE item_id='$itemId'";
mysql_query($sql_upp) or die(mysql_error());
}
}
if($tweet_asap==0){
$day1 = strtotime(date("Y:m:d H:i:s"));
$day2 = strtotime($tweet_post_date);
$diffHours = round(($day1 - $day2) / 3600);
if($diffHours>=48){
$sql_up="UPDATE item_order_master set tweet_status=1 WHERE item_id='$itemId'";
mysql_query($sql_up) or die(mysql_error());
$sql_upp="UPDATE tweet_order set tweet_post_date_time='$date' WHERE item_id='$itemId'";
mysql_query($sql_upp) or die(mysql_error());
}
}
if($retweet_asap==0){
$day1 = strtotime(date("Y:m:d H:i:s"));
$day2 = strtotime($retweet_post_date);
$diffHours = round(($day1 - $day2) / 3600);
if($diffHours>=48){
$sql_up="UPDATE item_order_master set tweet_status=1 WHERE item_id='$itemId'";
mysql_query($sql_up) or die(mysql_error());
$sql_upp="UPDATE tweet_order set retweet_post_date='$date' WHERE item_id='$itemId'";
mysql_query($sql_upp) or die(mysql_error());
}
}
}*/
/*End Code*/
echo 'Completed';
?>
You need to escape all the inputs, in case they contain quote characters.
$twitter_id = mysql_real_escape_string($twitterInfo[0]->user->id);
$name = mysql_real_escape_string($twitterInfo[0]->user->name);
... and so on
It would be better if you switched to PDO or mysqli, and used prepared statements with bound parameters, instead of substituting variables into query strings. See How can I prevent SQL injection in PHP?

Mail could not be send using php

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

Categories