php - 在while循环中使用PHPMailer发送多封电子邮件

标签 php pdo phpmailer

我试图在 while 循环中发送多封电子邮件,使用以下代码,它不发送电子邮件通知并给出错误“ fatal error : Uncaught Error :在 C:\xampp\htdocs\clinic\paients\run_medications 中找不到类“PHPMailer”。 php:98 堆栈跟踪:#0 {main} 被抛出。虽然位置正确并且从另一个子目录发送单个电子邮件。

require_once('../includes/PHPMailer.php');
require_once('../includes/SMTP.php');
require_once('../includes/Exception.php');
$full_dt = date('Y-m-d H:i:s');
$dt = date('Y-m-d');
$dt_hr = date('H');
$check_timing = $con->prepare("SELECT medication, start_time, end_time FROM timings WHERE HOUR(end_time) = :end ");
$check_timing->bindparam(':end', $dt_hr);
$check_timing->execute();
$count1 = $check_timing->rowCount();
if($count1 > 0) {
while($rows = $check_timing->fetch(PDO:: FETCH_OBJ)) {
  $medication = $rows->medication;
  $start_time = $dt.' '. $rows->start_time;
  $end_time = $dt.' '.$rows->end_time;
  $timestamp = strtotime($end_time) + 60*60;
  $end_hour = date('Y-m-d H:i:s', $timestamp);
    $query = "SELECT a.location_code, b.* FROM patients a INNER JOIN medication b ON a.id = b.pat_id WHERE b.status != 2 AND b.directions = :med AND b.last_time NOT BETWEEN :start AND :end AND :crntime BETWEEN b.start_date AND b.end_date AND :crntime BETWEEN :end AND :end_hour";
    $statement = $con->prepare($query);
    $statement->bindparam(':med', $medication);
    $statement->bindparam(':start', $start_time);
    $statement->bindparam(':end', $end_time);
    $statement->bindparam(':crntime', $full_dt);
    $statement->bindparam(':end_hour', $end_hour);
    $statement->execute();
    $count = $statement->rowCount();
    if($count > 0) {
    while($row = $statement->fetch(PDO:: FETCH_OBJ)) {
            $drug_id = $row->drgid;
            $code = $row->location_code;
            $pat_id = $row->pat_id;
            $drug = $row->med_type.' - '.$row->drug.' ('.$row->strength.')';
            $dose = $row->dosage;
            $route = $row->route;
            $directions = $row->directions;
        
            $getUserName = "SELECT name FROM users WHERE FIND_IN_SET(location_code, :codes)>0 ";
            $runUser = $con->prepare($getUserName);
            $runUser->bindParam(':codes',$code);
            $runUser->execute();
            $check = $runUser->rowCount();
            if($check > 0) {
            $userRow = $runUser->fetch(PDO::FETCH_OBJ);
            $by = $userRow->name;
            }
        
            $insert_report = $con->prepare("
                INSERT INTO  `report` (`med_id`,`pat_id`,`dtime`,`responsible`,`status`) values(:m_id,:p_id,:time,:by,'3')");
            
                $insert_report->bindparam(':m_id',$drug_id);
                $insert_report->bindparam(':p_id',$pat_id);
                $insert_report->bindparam(':time',$end_time);
                $insert_report->bindparam(':by',$by);
                $insert_report->execute();
        
                $mail = new PHPMailer();
                $mail->isSMTP();
                $mail-> SMTPOptions = array (
                'ssl' => array (
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
                )
                );
                $mail->Host = "smtp.gmail.com";
                $mail->SMTPAuth = true;
                $mail->SMTPSecure = "tls";
                $mail->Port = "587";
                $mail->Username = "from@gmail.com";
                $mail->Password = "12345678";
                $mail->Subject = "Medication Report";
                $mail->setFrom("from@gmail.com",$by);
                $mail->isHTML(true);
                //Attachment
                    //$mail->addAttachment('img/attachment.png');
                //Email body
                $mail->Body = "<h3>Medication Report by ($by) </h3></br>
                                You are being alerted that <b>$name</b> has missed their $directions Medication $row->med_type. And you may need to take appropriate action..";
                $mail->addAddress($to_email['Email_add']);
                $mail->send();

      } // /while 

    }// if num_rows
  }//while close
}// if num_rows

最佳答案

您的 PHPMailerAutoload.php 文件在哪里?
你需要用这个导入它:

require_once('phpmailer/PHPMailerAutoload.php');
总体而言,您必须执行以下步骤:
  • https://github.com/PHPMailer/PHPMailer 下载包通过单击页面最右下方的“下载 ZIP”按钮。
  • 解压缩文件。
  • 将语言文件夹class.phpmailer.php、class.pop3.php、class.smtp.php和PHPMailerAutoload.php都上传到你服务器上的同一个目录中,我喜欢在服务器上创建一个名为phpmailer的目录来放置所有这些成。
  • 在您的项目中加载类:
    require_once('phpmailer/PHPMailerAutoload.php');

  • 完毕...

    关于php - 在while循环中使用PHPMailer发送多封电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69554045/

    相关文章:

    来自函数返回数组的 PHP PDO 参数

    javascript - PHP Mailer,在发送电子邮件时阻止整个服务器执行

    php - 邮件不使用 SMTP 通过 SSL 使用 PHPMailer 发送

    php - 制作一个新的sql表,其中一个字段从另一个表字段复制数据

    php - SQL 查询速度慢的股票

    php - ZF2 - 将数组绑定(bind)到单个数据库查询参数

    mysql - PHP7 和 pdo_mysql

    php - 如何获得两个时间戳之间的分钟差

    mysql - 使用 3 个连接在一个查询中更新 2 个不同的表?

    security - PHP Mailer 类 - 保护电子邮件凭据