lingzhi. 邮件发送

<?php
/**
 * Bing
 * ticket 2060
 * Send email notification to employees when wo_speciality_code = 'OTHER'.
 */

require_once __DIR__ . '/../../../functions/_email_send.php';
require_once __DIR__ . '/../../../api/v1/vendor/autoload.php';

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// log setting.
$log = new Logger('email');
$month = date('Y-m');
// 这是当前文件夹下建立一个专门存日记的文件夹
$log->pushHandler(new StreamHandler(__DIR__ . '/log/email_' . $month . '.log', Logger::DEBUG));

$log->info('Start processing...', ['wo_id' => $wo_id]);

$DB = Flight::get('DB');

// get work order info.
$query = $DB->prepare("
    SELECT wo_id, wo_name, speciality_code
    FROM wo_list INNER JOIN mic_speciality 
        ON wo_list.wo_speciality_id = mic_speciality.speciality_id
    WHERE wo_id = :wo_id
");
$query->bindvalue(':wo_id', $wo_id, PDO::PARAM_INT);
if (!$query->execute()) {
    $log->error("failed to get work order info", [errorInfo($query)]);
}
$work_order = $query->fetch(PDO::FETCH_ASSOC);
$log->debug('work order: ', $work_order);

// When speciality_code = 'OTHER', fetch employee list, send email.
if ($work_order['speciality_code'] == 'OTHER') {
    $work_order_name = $work_order['wo_name'];

    $query = $DB->prepare("
        SELECT employee_email, employee_name 
        FROM wo_list_employee INNER JOIN admin_employee ae 
            ON wo_list_employee.employee_id = ae.employee_id
        WHERE wo_id = :wo_id;
    ");

    $query->bindValue('wo_id', $wo_id, PDO::PARAM_INT);
    if (!$query->execute()) {
        $log->error('Failed to fetch employee.', [errorInfo($query)]);
    }

    $employee_list = $query->fetchAll(PDO::FETCH_ASSOC);
    $log->debug('employee list: ', $employee_list);

    // send email to each employee.
    foreach ($employee_list as $employee) {
        $send_email = $employee['employee_email'];
        $send_email_name = $employee['employee_name'];
        $mail_subject = 'EAMic工单提醒: ' . $wo_id . $work_order_name;
        $content = "
      {$send_email_name}先生/女士,您好:<br><br>
      您有新的工单 <b> $wo_id :  $work_order_name </b>, 请登录系统查看。<br>
      点击 <a href='https://ctwuxeamic.atlascopco.com/'>阿特拉斯EAMic®系统</a> 查看详情。 <br><br>
      祝您开心每一天!<br>
      领值团队
       ";
        try {
            $result = send_email($send_email, $mail_subject, $content, $content);
        } catch (Exception $e) {
            $log->error('email sending error');
        }
    }
}

 

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

    }
}

 

posted @ 2022-07-11 14:46  王越666  阅读(16)  评论(0编辑  收藏  举报