Sending Email Reminder (while loop) - php

I'm testing this code out. I assigned two reminders with test date and time values. Two reminders should be sent out to two different people. However when I run it, one will get an email containing their reminder, while the other person receives an email containing both his and the other reminder in the body of the email
Any help would be much appreciated.
Thanks!
<?
date_default_timezone_set("America/Los_Angeles");
//$trigger_date = date('h:i \P\S\T');
//$trigger_time = date('h:i A');
//test date and time
$trigger_date = date('Y-m-d');
$trigger_time = date('04:51');
$your_reminder = mysqli_query($conn,"SELECT * FROM reminders WHERE reminder_date = '$trigger_date' AND reminder_time = '$trigger_time'");
$todaydate = date("Y-m-d");
$row = $your_reminder->num_rows;
if ($row == 0 ){
echo "Nothing to Send</div>";
} else {
while($row = mysqli_fetch_array($your_reminder))
{ $reminder_owner = $row["reminder_owner"];
//echo $reminder_owner;
//echo "<br>Title:".$row["reminder_title"];
$send_owner = mysqli_query($conn,"SELECT * FROM admin WHERE username='$reminder_owner'");
$row_owner = mysqli_fetch_array($send_owner,MYSQLI_ASSOC);
$email = $row_owner["admin_email"];
$headers = 'From: TeamInfoPage';
$email_subject ="Reminder Test: ".$row["reminder_title"];
$reminder_details .= "Hi ".$row_owner["admin_fn"]."\n";
$reminder_details .= "Event: ".$row["reminder_title"]."\n";
// $reminder_details .= $row["tasks_notes"]."\n\n";
//Send out Reminder mail
'X-Mailer: PHP/' . phpversion();
#mail($email, $email_subject, $reminder_details, $headers);
}
echo "Success";}
?>

Plz reset the value of reminder_details in the loop for each mail..

You need to reset the reminder_details to blank between loop iterations.
Simply change these lines:
while($row = mysqli_fetch_array($your_reminder))
{ $reminder_owner = $row["reminder_owner"];
//echo $reminder_owner;
To this:
while($row = mysqli_fetch_array($your_reminder))
{ $reminder_owner = $row["reminder_owner"];
$reminder_details = "";
//echo $reminder_owner;

Related

Send learners reminder email and admin

Below code should send email to learners but the code is giving me error:
"Fatal error: Call to undefined method mysqli_result::fetch() in /home/train4/public_html/hocotest/cron-email-expire-1.php on line 46"
I replaced fetch(PDO::FETCH_OBJ) with fetch_object() then the file runs fine no error but the learners are not getting emails.
there is 2 parts of this email,
1. it will send email to learners
2. Send email to admin that to whom the system have system have sent the email.
the second part run fine, admin get the email but there is no info to whom the system have sent emails to, as part 1 is not working.
I tried running the script without array, so the system send 1 email for each course, if the learners are enrolled in 7 courses and not completed 5 courses then they will get 5 emails.. it work fine. but i want to send only one email with all not completed course details.
<?php
include 'db.php';
function check_emailaddress($email) {
// First, we check that there is one # symbol, and that the lengths are right
if (!ereg("^[^#]{1,64}#[^#]{1,255}$", $email))
{
// Email invalid because wrong number of characters in one section, or wrong number of # symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("#", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++)
{
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i]))
{
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) // Check if domain is IP. If not, it should be valid domain name
{
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2)
{
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++)
{
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i]))
{
return false;
}
}
}
return true;
}
$extraParam = "";
if (isset($_GET["ex"]) ) {
$extraParam = $_GET["ex"]."---";
}
//get system sender email address from settings table
$srs = $db->query("SELECT adminemail, systememail FROM tbl07systemsettings");
$srow = $srs->fetch(PDO::FETCH_OBJ);
$from = $srow->systememail; //"From: info#visiondesigngroup.ca\r\n";
$email_from = "From: $from\r\n";
$admin_email = "toralhc6#gmail.com";
$email_to = "" ;//"respite#safeguards-training.net";
$tempsql = "SELECT a06subject, a06templatebody, usetemplate FROM tbl06emailtemplates WHERE a06name = 'autoreminder'";
$rs = $db->query($tempsql);
$rowemail = $rs->fetch(PDO::FETCH_OBJ);
$usetemplate = $rowemail->usetemplate;
if ($usetemplate == 0) exit; // not send email if course reminder email template was set not to send email *************
$email_subject = $rowemail->a06subject;
//$from = "From: info#visiondesigngroup.ca\r\n";
$eb = $rowemail->a06templatebody;
$strSQL_expire = "SELECT * FROM tbl01user, tbl04usercourses, tbl03courses
WHERE a01User=a04UserId AND a03CourseId=a04CourseId
AND DATEDIFF(CURDATE(),a04StartDate) > 0 AND a04Completion=0
AND a01UserRoll=0 AND a01Status=1 AND a04Status=1";
$query = $db->query($strSQL_expire) or die ("Error querying database.<br>$strSQL_expire");
$nofinish = array();
$course = "";
$pre_email = "";
$pre_fn = "";
$n = 0;
while($email_row=$query->fetch(PDO::FETCH_OBJ)){
$fn = $email_row->a01FirstName;
$email = $email_row->a01Email;
$password = $email_row->a002password;
if ($pre_email == $email){
$course .= "web url" . $email_row->a03CourseName . "</a><br>";
$pre_email = $email;
$pre_fn = $fn;
$pre_password = $password;
}else{
$nofinish[] = array('firstname'=>$pre_fn, 'email'=>$pre_email, 'courses'=>$course, 'password'=>$pre_password);
$course = "web url" . $email_row->a03CourseName . "</a><br>";
$pre_email = $email;
$pre_fn = $fn;
$pre_password = $password;
}
}
$nofinish[] = array('firstname'=>$pre_fn, 'email'=>$pre_email, 'courses'=>$course, 'password'=>$pre_password);
array_shift($nofinish);
set_time_limit(600);
$eb1 = nl2br($eb);
$i = 0;
foreach($nofinish as $no){
$email_from = $from;
$email_address = $no['email'];
// $email_address = "lyan3000#gmail.com";
// if ($i++ >= 4) exit;
// need to comment the above two lines before go live***********************************************
$email_subject = "Course Completion Reminder";
$top = "<div style='font-family:Arial'>";
$top .= "<div style='background-color:#045FB4; color:white;width:100%;padding:10px 10px;'><h1>Service Centre</h1></div>";
$variable = array(
'{$firstname}' => $no['firstname'],
'{$course}' => $no['courses'],
'{$password}'=> $no['password'],
'{$email}'=> $no['email']
);
$email_body = strtr($eb1, $variable);
$bottom = "<p><img src='cid:logoimg'></p>";
$bottom .="<div style='background-color:#045FB4; height:25px;width:100%'> </div></div>";
$email_message = $top . $email_body . $bottom;
/*
echo $email_from . "<br>";
echo $email_address . "<br>";
echo $email_subject . "<br>";
echo $email_message;
echo "<hr>";
*/
if (mail($email_address, $email_subject, $email_message, $email_from))
{
//echo "Reminder email sent to: " . $no['firstname'] . "<br>";
echo "Yes. <br>";
}else{
//echo "Sorry, There is a mail server error. Please try it again later " . $no['firstname'] . "<br>";
echo "No. <br>";
}
}
$file_name = "record_for_cron_expire_email.txt";
$tz = 'EST';
$timestamp = time();
$dt = new DateTime("now", new DateTimeZone($tz)); //first argument "must" be a string
$dt->setTimestamp($timestamp); //adjust the object to correct timestamp
$date = $dt->format('Y-m-d h:i:s A');
$text = "This script runs on $date " . PHP_EOL;
file_put_contents ( $file_name , $text, FILE_APPEND);
//send summurized email to admin
$sql = "SELECT a06subject, a06templatebody FROM tbl06emailtemplates WHERE a06name = 'remindertoadmin'";
$rs = $db->query($sql);
$row = $rs->fetch_object();
$email_subject = $row->a06subject;
//$from = "From: $email_from\r\n";
$date = date('Y-m-d');
$eb = $row->a06templatebody;
$variable = array(
'{$learnerCourse}' => $learnercourse,
'{$date}' => $date
);
$email_body = strtr($eb, $variable);
mail($admin_email, $email_subject, $email_body, $email_from);
//SET inactive all expired courses
$expiredRightNow = date("Y-m-d");
$strSQL_setExpire = "UPDATE tbl04usercourses
SET a04Status = 0
WHERE a04ExpirationDate <= '$expiredRightNow'
AND a04Completion = 0";
$query = $db->query($strSQL_setExpire) or die ("Error querying database.<br>$strSQL_setExpire");
?>
mysqli_result::fetch_all() requires MySQL Native Driver (mysqlnd).
chances are you might be missing it.
have a look at this posts, that might help you.
mysqli fetch_all() not a valid function?
below is the minimal code I can think of:
$strSQL_expire = "SELECT * FROM tbl01user, tbl04usercourses, tbl03courses
WHERE a01User=a04UserId AND a03CourseId=a04CourseId
AND DATEDIFF(CURDATE(),a04StartDate) > 0 AND a04Completion=0
AND a01UserRoll=0 AND a01Status=1 AND a04Status=1";
$query = $db->query($strSQL_expire) or die ("Error querying database.<br>$strSQL_expire");
$nofinish = array();
$course = "";
$pre_email = "";
$pre_fn = "";
$n = 0;
while($email_row=$query->fetch(PDO::FETCH_OBJ)){
$fn = $email_row->a01FirstName;
$email = $email_row->a01Email;
$password = $email_row->a002password;
if ($pre_email == $email){
$course .= "web url" . $email_row->a03CourseName . "</a><br>";
$pre_email = $email;
$pre_fn = $fn;
$pre_password = $password;
}else{
$nofinish[] = array('firstname'=>$pre_fn, 'email'=>$pre_email, 'courses'=>$course, 'password'=>$pre_password);
$course = "web url" . $email_row->a03CourseName . "</a><br>";
$pre_email = $email;
$pre_fn = $fn;
$pre_password = $password;
}
}
$nofinish[] = array('firstname'=>$pre_fn, 'email'=>$pre_email, 'courses'=>$course, 'password'=>$pre_password);
array_shift($nofinish);
set_time_limit(600);
$eb1 = nl2br($eb);
$i = 0;
foreach($nofinish as $no){
$email_from = $from;
$email_address = $no['email'];
$email_subject = "Course Completion Reminder";
$top = "<div style='font-family:Arial'>";
$top .= "<div style='background-color:#045FB4; color:white;width:100%;padding:10px 10px;'><h1>Service Centre</h1></div>";
$variable = array(
'{$firstname}' => $no['firstname'],
'{$course}' => $no['courses'],
'{$password}'=> $no['password'],
'{$email}'=> $no['email']
);
$email_body = strtr($eb1, $variable);
$bottom = "<p><img src='cid:logoimg'></p>";
$bottom .="<div style='background-color:#045FB4; height:25px;width:100%'> </div></div>";
$email_message = $top . $email_body . $bottom;
if (mail($email_address, $email_subject, $email_message, $email_from))
{
echo "Yes. <br>";
}else{
echo "No. <br>";
}
}

Send over 2000 e-mails with PHP mail function

I am trying to create a system that sends an e-mail to everyone of the users of my webpage.
It seems though that there are too many emails - about 2400 emails, to send for it to work.
Is there a smarter way, or a way to send 10 at a time or something like that?
Here is my code:
<?php
include('../../includingThis.php');
$fra = "ME <me#me.com>";
$emne = "";
$sql = "SELECT * FROM sendThese WHERE id=1";
if ($result = mysqli_query($con, $sql)) {
while($row = mysqli_fetch_assoc($result)) {
$emne = $row["subject"];
$message = $row["message"];
$mailsHere = $row["mailsHere"];
$til = explode(",", $mailsHere);
}
}
$besked = file_get_contents('sendThese.html');
$besked = str_replace("%%message%%", $message, $besked);
// HEADERS
$headers = "MIME-Version:1.0\r\n";
$headers .= "Content-type: text/html; charset=UFT-8\r\n";
$headers .= "From: ".$fra."\r\n";
// Mail til ejeren af siden
$mailsSend = 0;
foreach ($til as $value) {
mail($value,$emne,$besked,$headers);
$mailsSend++;
echo $value . "<br>";
sleep(2);
}
// Sender vi brugeren videre til en side
//header("Location: index.php");
echo "Done - " . $mailsSend . " e-mails sendt.";
die();
?>

how to send an emails to a list while updating MYSQL Database

i have wrote a php script to send an emails.In this script it checks whether current date is equal with the emailfly_date and email is = o.if that satisfies email will sent to the user.this script is working for one user at a one time.that if database has five records that that matches current criteria it sends the email only to the last id.what is the reason for this?
id
250
251
252
253
it will send the email only to the ID 253.but not to the others.
Here is mycode
<?php
include_once 'dbconnect.php';
$query = "SELECT ID,email, emailfly_date, CURRENT_DATE AS nowtime FROM xxx WHERE reqnum = '' AND email_sent = '0'";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$regid = $row['ID'];
$activemail= $row['nowtime'];
/*echo $regid . '<br />';
echo $activemail. '<br />';*/
}
$query = mysql_query("SELECT ID, email, reqnum, emailfly_date, email_sent FROM xxx WHERE ID = '$regid' AND emailfly_date = '$activemail'");
$rowdata = mysql_fetch_array($query);
$ID = $rowdata['ID'];
$email = $rowdata['email'];
$reqnum = $rowdata['reqnum'];
$emailfly_date = $rowdata['emailfly_date'];
$email_sent = $rowdata['email_sent'];
if ($reqnum != '') {
/* echo "not sucess";*/
} elseif ($email_sent == '1') {
/* echo "email already sent";*/
} elseif ($email_sent == '1' && $reqnum != '') {
/* echo "no way to send";*/
}
elseif($email_sent == '0' && $reqnum == '' ){
$ulink = "http://xxx.eewfewt.net/tttt/yyyy.php?ID=$regid";
$to = $email;
$subject = 'hoo'; // Give the email a subject
$message = '
Thanks for Using Trial Version!
If You Wish to Continue with the Monthly Paid Package.
------------------------
Click Below
Site Renewal link: ' . $ulink . '
'.$emailfly_date.'
------------------------ ';
$headers .= 'From:werwerwer.aaa.net' . "\r\n";
$headers .= 'Bcc:ewrwerer.aaa.net' . "\r\n";
mail($to, $subject, $message, $headers);
$upadtequery = mysql_query("UPDATE xxx SET email_sent ='1' WHERE ID = '$regid'");
echo "sucess";
}
else{
echo "bye";
}
?>
From $query = mysql_query(.....bla...bla... to last else{
echo "bye";
} Put all this inside your while loop.
Also if you fetch data from same table try to use single query.
mysql_() is depreciated now. So try to use mysqli_()
include_once 'dbconnect.php';
$query = "SELECT ID,email, emailfly_date, CURRENT_DATE AS nowtime FROM xxx WHERE id in ('250','251','252','253') AND email_sent = '0'";
$result = mysql_query($query) or die(mysql_error());
while($rowdata = mysql_fetch_array($result))
{
$ID = $rowdata['ID'];
$email = $rowdata['email'];
$reqnum = $rowdata['reqnum'];
$emailfly_date = $rowdata['emailfly_date'];
$email_sent = $rowdata['email_sent'];
$ulink = "http://xxx.eewfewt.net/tttt/yyyy.php?ID=$regid";
$to = $email;
$subject = 'hoo'; // Give the email a subject
$message = '
Thanks for Using Trial Version!
If You Wish to Continue with the Monthly Paid Package.
------------------------
Click Below
Site Renewal link: ' . $ulink . '
'.$emailfly_date.'
------------------------ ';
$headers .= 'From:werwerwer.aaa.net' . "\r\n";
$headers .= 'Bcc:ewrwerer.aaa.net' . "\r\n";
mail($to, $subject, $message, $headers);
$upadtequery = mysql_query("UPDATE xxx SET email_sent ='1' WHERE ID = '$ID'");
echo "sucess";
}

Using mail() to send multiple emails from MySQL Query

I got this code and it's old and I know I need to convert it to PDO (still learning) but I need to get this working right now.
I am trying to send multiple emails out based on the query. So in this particular query I get two results. So I need to send out two different emails to the group. If the query had 4 results, then I would need to send 4. Right now it is only sending the first result. How can I get it to loop through and send multiple emails.
Here is the code:
<?
// connection string ......
$production = "SELECT value1 FROM parameters WHERE name = 'IS_PRODUCTION' ";
$query = "SELECT accounts.username, email_activate.email, accounts.user_id, accounts.alt_id FROM accounts INNER JOIN email_activate ON accounts.user_id = email_activate.user_id WHERE dt_welcome = '0000-00-00 00:00:00' AND dt_start <= DATE_SUB(NOW(), INTERVAL 3 DAY)";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$message = "Hello ".$row['username'].", <br/><br/>";
$message .= "You recently subscribed to <bblah blah blah</b> ";
$message .= '(http://), ' ;
$message .= "the official ";
$message .= "source of <br /><br /> ";
if ($production == 'Y' ) {
$subject = "How can we Assist!";
$to = "".$row['email']."";
$result2 = mysql_query("UPDATE email_activate SET dt_welcome = NOW() WHERE user_id = '".$row['user_id']."' ");
} else {
$subject = "[TEST] Offer You Assistance";
$to = 'email1#email.com' . ', ';
$to .= 'email2#email.com ' . ', ';
$to .= 'email3#email.com';
$result2 = mysql_query("UPDATE email_activate SET dt_welcome = NOW() WHERE user_id = '".$row['user_id']."' ");
}
$headers = "Content-Type: text/html; charset=utf-8\r\n";
$queryEmail = "SELECT alt_id FROM lookup_roles WHERE spec_tag = 'public_email'";
$result3 = mysql_query($queryEmail) or die(mysql_error());
while ($row12 = mysql_fetch_array($resul3t)) {
mail($to, $subject, $message, $headers, "-f ".$row12[alt_id]."");
}
}
?>
So I fixed it, the above code works just fine. I forgot to use a different $result variable ---> Changed it from $result to $result3, this way it doesn't blank out the previous result which is why I was only getting 1 email. I knew it was something simple, just couldn't see it.

php mail with variables

I am attempting to setup a mail script which will first run a simple select from mysql, and use these array variables in the message. However all the variables are not output to the message body, only one row of variables. Here is my script:
$sql1 = "SELECT * FROM videos WHERE checked_out = '1'";
$result1 = $dbLink->query($sql1);
while($row1 = $result1->fetch_assoc()) {
$name = $row1['name'];
$tape_no = $row1['tape_no'];
$member_name = $row1['member_name'];
$date_out = date("F j, Y", strtotime($row1['date_out']));
}
//email function to administrator
$to = "nouser#mail.com";
$subject = "Daily Video Rental Summary";
$message = "$name $tape_no $date_out $member_name
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
$from = "no_replies_please#mail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
appreciate any insight anyone cares to share on this.
Thanks,
--Matt
Try this instead:
$sql1 = "SELECT * FROM videos WHERE checked_out = '1'";
$result1 = $dbLink - > query($sql1);
while ($row1 = $result1 - > fetch_assoc()) {
$name = $row1['name'];
$tape_no = $row1['tape_no'];
$member_name = $row1['member_name'];
$date_out = date("F j, Y", strtotime($row1['date_out']));
//email function to administrator
$to = "nouser#mail.com";
$subject = "Daily Video Rental Summary";
$message = "$name $tape_no $date_out $member_name
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
$from = "no_replies_please#mail.com";
$headers = "From:".$from;
mail($to, $subject, $message, $headers);
}
This will do a mail per row.
thats because you're while keeps overwriting the variables, so it'l get only the last one. You might want to just build the $message variable in the while loop by doing. that will send 1 email with everything
$sql1 = "SELECT * FROM videos WHERE checked_out = '1'";
$result1 = $dbLink->query($sql1);
while($row1 = $result1->fetch_assoc()) {
$name = $row1['name'];
$tape_no = $row1['tape_no'];
$member_name = $row1['member_name'];
$date_out = date("F j, Y", strtotime($row1['date_out']));
$message .= "$name $tape_no $date_out $member_name"
}
//email function to administrator
$to = "nouser#mail.com";
$subject = "Daily Video Rental Summary";
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
$from = "no_replies_please#mail.com";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
You are overwriting your variables in the loop so $name, etc. will contain the values found in the last row after the loop.
What you could do is construct your message in the loop:
$message = '';
while($row1 = $result1->fetch_assoc()) {
$message .= $row1[...]; // whatever you need
}
Try this:
$message = NULL;
$sql1 = "SELECT * FROM videos WHERE checked_out = '1'";
$result1 = $dbLink->query($sql1);
while($row1 = $result1->fetch_assoc()) {
$name = $row1['name'];
$tape_no = $row1['tape_no'];
$member_name = $row1['member_name'];
$date_out = date("F j, Y", strtotime($row1['date_out']));
$message .= "$name $tape_no $date_out $member_name" }
And remove the other $message variable under the loop. Notice the . before the = in the $message. This tells php to continually add on to the $message
That's because you're assigning the variables in the loop, but using them in the $message variable outside the loop. So your $message contains only the items from the last row/record. Try moving and appending values in the $message variable inside the while loop.
So it could be
while($row1 = $result1->fetch_assoc()) {
//assign vars here
$message .= "$name $tape_no $date_out $member_name\n";
}
$message = "$message
======================================================================
PLEASE DO NOT REPLY TO THIS MESSAGE, AS THIS MAILBOX IS NOT MONITORED
======================================================================";
//assign other variables
//mail()
Hope that helps.

Categories