0

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%'>&nbsp;</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");
?>
  • Can you try to create minimal code example for reproduce? Currently you posted a lot of code and probably only 10 line is needed – dWinder May 28 '19 at 21:49
  • I posted the whole code i needed, I have posted the short version in answers, as I wasn't able to add it in comment here, which will send the email to learners. –  May 29 '19 at 14:05

2 Answers2

1

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?

Turbo
  • 124
  • 11
0

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%'>&nbsp;</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>";
        }
    }