<?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; use Monolog\Logger; use Monolog\Handler\StreamHandler; require_once __DIR__ . '/../../../../plugins/PHPMailer-6.1.7/src/Exception.php'; require_once __DIR__ . '/../../../../plugins/PHPMailer-6.1.7/src/PHPMailer.php'; require_once __DIR__ . '/../../../../plugins/PHPMailer-6.1.7/src/SMTP.php'; require_once __DIR__ . '/../../../../functions/_email_send.php'; require_once __DIR__ . '../../../../../api/v1/vendor/autoload.php'; // 这是记录日记 $log = new Logger('email'); $month = date('Y-m'); $log->pushHandler(new StreamHandler(__DIR__ . '/log/email_' . $month . '.log', Logger::DEBUG)); // 根据员工编码查询出设备主管 $equipment_supervisor = $DB->prepare(" SELECT employee_name,employee_email FROM admin_employee WHERE employee_code = :employee_code "); $employee_code = '00417046'; $str_employee_code = strval($employee_code); $equipment_supervisor->bindValue('employee_code', $str_employee_code); if (!$equipment_supervisor->execute()) { Flight::error(new RuntimeException(errorInfo($equipment_supervisor))); } elseif ($equipment_supervisor->rowcount() == 0) { Flight::notFound(); } else { $result = $equipment_supervisor->fetch(PDO::FETCH_ASSOC); // 获取设备主管 00417046的邮箱 $equipment_supervisor_a = $result['employee_email']; } // 另一个设备主管 $equipment_supervisor_b = $DB->prepare(" SELECT employee_name,employee_email FROM admin_employee WHERE employee_code = :employee_code "); $employee_code_b = '00416788'; $strval_employee_code = strval($employee_code_b); $equipment_supervisor_b->bindValue('employee_code', $strval_employee_code); if (!$equipment_supervisor_b->execute()) { Flight::error(new RuntimeException(errorInfo($equipment_supervisor_b))); } elseif ($equipment_supervisor_b->rowcount() == 0) { Flight::notFound(); } $result = $equipment_supervisor_b->fetch(PDO::FETCH_ASSOC); // 获取设备主管 00416788的邮箱 $equipment_supervisor_b = $result['employee_email']; // 这一步是获取工单延迟的wo_id $query = $DB->prepare(" SELECT COUNT(wo_list.wo_id) AS count_wo_id, admin_employee.employee_name, admin_employee.employee_email, wo_list.wo_id, wo_list.wo_name, asset_list.asset_code, asset_list.asset_name, asset_location.location_code, asset_location.location_name, wo_history.wo_responsible_name, wo_list.wo_target_time, wo_list.wo_status FROM wo_list INNER JOIN wo_list_employee ON wo_list.wo_id = wo_list_employee.wo_id INNER JOIN admin_employee ON wo_list_employee.employee_id = admin_employee.employee_id INNER JOIN asset_list ON wo_list.wo_responsible_id = asset_list.asset_responsible_id INNER JOIN asset_location ON asset_location.location_id = asset_list.location_id INNER JOIN wo_history ON wo_history.wo_id = wo_list.wo_id WHERE wo_list.wo_target_time <> '' AND TIMESTAMPDIFF(HOUR, NOW(), wo_list.wo_target_time) <= 24 AND wo_list.wo_status < 6 GROUP BY admin_employee.employee_id "); if (!$query->execute()) { Flight::error(new RuntimeException(errorInfo($query))); } elseif ($query->rowcount() == 0) { Flight::notFound(); } else { $row = $query->fetchAll(PDO::FETCH_ASSOC); // 发送的内容 $output = <<< TABLE <table border="1px solid #777" cellspacing="0" cellpadding="0" align="center" width="100%" height:"50px" > <caption style="font-size:1.8rem">工单信息</caption> <thead bgcolor="skyblue"> <th style=" width:50px">工单号</th> <th style=" width:50px">工单名称</th> <th style=" width:50px">资产编码</th> <th style=" width:50px">资产名称</th> <th style=" width:50px">设备</th> <th style=" width:50px">位置</th> <th style=" width:50px">负责人</th> <th style=" width:50px">目标时间</th> <th style=" width:50px">当前工单状态</th> </thead> TABLE; foreach ($row as $value) { // 工单的状态 if ($value['wo_status'] == 0) { $wo_status = '已创建'; } elseif ($value['wo_status'] == 1) { $wo_status = '等待备件'; } elseif ($value['wo_status'] == 2) { $wo_status = '等待外委'; } elseif ($value['wo_status'] == 3) { $wo_status = '已安排'; } elseif ($value['wo_status'] == 4) { $wo_status = '已搁置'; } elseif ($value['wo_status'] == 5) { $wo_status = '进行中'; } elseif ($itval['wo_status'] == 6) { $wo_status = '已完成'; } elseif ($value['wo_status'] == 7) { $wo_status = '已确认'; } $output .= "<tr><td >{$value['wo_id']}</td>"; $output .= "<td>{$value['wo_name']}</td>"; $output .= "<td>{$value['asset_code']}</td>"; $output .= "<td>{$value['asset_name']}</td>"; $output .= "<td>{$value['location_code']}</td>"; $output .= "<td>{$value['location_name']}</td>"; $output .= "<td>{$value['wo_responsible_name']}</td>"; $output .= "<td>{$value['wo_target_time']}</td>"; $output .= "<td>$wo_status</td>"; $output .= "</tr>"; } $output .= "</table>"; foreach ($row as $item) { // 发送的内容 $msg = $output; // 发送的主题 $email_subject = "您有" . $item['count_wo_id'] . "个工单即将延迟,请及时关注!"; try { // 这里是发送给工单负责人 send_email('yanbing910624858@163.com', $email_subject, $msg, $msg); // 同时发送至设备主管 send_email($equipment_supervisor_a, $email_subject, $msg, $msg); send_email($equipment_supervisor_b, $email_subject, $msg, $msg); } catch (Exception $e) { $error = $e->getMessage(); $log->error($error); } } }