HEX
Server: Apache/2.4.59 (Debian)
System: Linux keymana 4.19.0-21-cloud-amd64 #1 SMP Debian 4.19.249-2 (2022-06-30) x86_64
User: lijunjie (1003)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/html/wp-content/themes/ormedia/page-mailtest.php
<?php
//Template Name: mailtest

$statement = "SELECT a.*,b.name_zh,b.name_en FROM `keylab_calendar` as a left join `keylab_property` as b on a.property_id = b.property_id WHERE a.is_send = 0";
$results = $wpdb->get_results($statement);
foreach ($results as $item){
    $item->ann_list = unserialize($item->ann_list) ?: [];
}
$a_fcm_type = [
    'android_fcm_token',
    'ios_fcm_token'
];
//var_dump($results);

foreach($results as $value1){
    $args = [
        'subject' => "日曆事件提醒",
        'html' => "",
        'to' => []
    ];
    $day = $value1->advance_days;
    $time = substr($value1->time, 0, 10);
    $real_day = BetweenDays(date("Y-m-d"), $time);
    //普通日曆事件
    if($value1->cal_type == 0){
        if($real_day <= intval($day) && $value1->is_send == '0'){
            foreach($value1->ann_list as $value2){
                
                if(is_array($value2)){
                    $type = $value2['type'];
                    $id = $value2['id'];
                    $name_zh = $value2['user_login'];
                }else{
                    $type = $value2->type;
                    $id = $value2->id;
                    $name_zh = $value2->user_login;
                }
                $text = $value1->description;
                $build_name = $value1->name_zh;
                
                //email
                if($real_day < 0){
                    $args['html'] = "<h3> <a href=\"http://keylab.cc/admin/\">Keylab</a>日曆事件提醒 </h3>
                    <br /><p>尊敬的 $name_zh ,大廈 $build_name 的 <b>$text</b> 日曆公告 已过期</p>
                    <br /><p>請到 <a href=\"http://keylab.cc/admin/\">Keylab</a> 中查看詳情!</p>
                    ";
                }else{
                    $args['html'] = "<h3> <a href=\"http://keylab.cc/admin/\">Keylab</a>日曆事件提醒 </h3>
                    <br /><p>尊敬的 $name_zh ,大廈 $build_name 的 <b>$text</b> 日曆公告 還有 $real_day 天( $time ) 到期</p>
                    <br /><p>請到 <a href=\"http://keylab.cc/admin/\">Keylab</a> 中查看詳情!</p>
                    ";
                }
                    if(!isset($type) || $type == 0){
                        $temp_user_email = $wpdb->get_var($wpdb->prepare("select user_email from wp_users where ID = %d",$id));
                    }else{
                        $temp_user_email = $wpdb->get_var($wpdb->prepare("select email from keylab_users where user_id = %d",$id));
                    }
                $args['to'] = [ $temp_user_email ];
                send_mailjet_email($args);

                //FCM
                if(!isset($type) || $type == 0){
                    foreach($a_fcm_type as $value3){
                        $temp_token = get_user_meta($id, $value3);
                        if(!empty($temp_token)){
                            foreach($temp_token as $value){
                                echo "<br/>".$value."<br/>";
                                if($real_day < 0) {
                                    $fcm_msg = "尊敬的 $name_zh ,大廈 $build_name 的 $text 日曆公告 已過期 ( $time )";
                                }else {
                                    $fcm_msg = "尊敬的 $name_zh ,大廈 $build_name 的 $text 日曆公告 還有 $real_day 天( $time )到期";
                                }
                                var_dump(send_app_fcm($value, 'Keylab日曆事件提醒', $fcm_msg));
                            }
                        }
                    }
                }
            }
    
            $wpdb->update("keylab_calendar", [
                "is_send" => 1
            ]
            , array('id' => $value1->id));
        }
    }
    //保安員規則日曆事件
    if($value1->cal_type == 1){
        $temparr = explode(",", get_property_meta($value1->property_id, 'propman_day'));
        if(in_array($real_day, $temparr)  && $value1->is_send == '0'){
            foreach($value1->ann_list as $value2){
    
                if(is_array($value2)){
                    $type = $value2['type'];
                    $id = $value2['id'];
                    $name_zh = $value2['user_login'];
                }else{
                    $type = $value2->type;
                    $id = $value2->id;
                    $name_zh = $value2->user_login;
                }
                $text = $value1->description;
                $build_name = $value1->name_zh;


                //email
                if($real_day < 0){
                    $args['html'] = "<h3> <a href=\"http://keylab.cc/admin/\">Keylab</a>日曆事件提醒 </h3>
                    <br /><p>尊敬的 $name_zh ,大廈 $build_name 的 <b>$text</b> 日曆公告 已过期</p>
                    <br /><p>請到 <a href=\"http://keylab.cc/admin/\">Keylab</a> 中查看詳情!</p>
                    ";
                }else{
                    $args['html'] = "<h3> <a href=\"http://keylab.cc/admin/\">Keylab</a>日曆事件提醒 </h3>
                    <br /><p>尊敬的 $name_zh ,大廈 $build_name 的 <b>$text</b> 日曆公告 還有 $real_day 天( $time ) 到期</p>
                    <br /><p>請到 <a href=\"http://keylab.cc/admin/\">Keylab</a> 中查看詳情!</p>
                    ";
                }
                if(!isset($type) || $type == 0){
                    $temp_user_email = $wpdb->get_var($wpdb->prepare("select user_email from wp_users where ID = %d",$id));
                }else{
                    $temp_user_email = $wpdb->get_var($wpdb->prepare("select email from keylab_users where user_id = %d",$id));
                }
                $args['to'] = [ $temp_user_email ];
                echo $temp_user_email;
                send_mailjet_email($args);

                //FCM
                if(!isset($type) || $type == 0){
                    foreach($a_fcm_type as $value3){
                        $temp_token = get_user_meta($id, $value3);
                        if(!empty($temp_token)){
                            foreach($temp_token as $value){
                                echo "<br/>".$value."<br/>";
                                if($real_day < 0) {
                                    $fcm_msg = "尊敬的 $name_zh ,大廈 $build_name 的 $text 日曆公告 已過期 ( $time )";
                                }else {
                                    $fcm_msg = "尊敬的 $name_zh ,大廈 $build_name 的 $text 日曆公告 還有 $real_day 天( $time )到期";
                                }
                                var_dump(send_app_fcm($value, 'Keylab日曆事件提醒', $fcm_msg));
                            }
                        }
                    }
                }

                $wpdb->update("keylab_calendar", [
                    "is_send" => 1
                ]
                , array('id' => $value1->id));
            }
        }

        if(!in_array($real_day, $temparr)){
            $wpdb->update("keylab_calendar", [
                "is_send" => 0
            ]
            , array('id' => $value1->id));
        }
    }
    
}
//MPF外部事件
$statement2 = "SELECT a.*,b.name_zh,b.name_en,b.admin_wp_id,c.name_zh as username FROM `keylab_property_propman` as a left join `keylab_property` as b on a.property_id = b.property_id left join keylab_users as c on a.user_id = c.user_id WHERE a.MPF_file = 'N;' and a.propman_day != '0000-00-00'";
$results2 = $wpdb->get_results($statement2);

foreach($results2 as $value){

    $args = [
        'subject' => "MPF上傳提醒",
        'html' => "",
        'to' => []
    ];
    $tempday = get_property_meta($value->property_id, 'mpf_day');

    $real_day = BetweenDays($value->propman_day, date("Y-m-d"));

    if(!empty($tempday) && $real_day == intval($tempday) && $value->MPF_notifi_day != date("Y-m-d")){
        
        $build_name = $value->name_zh;
        
        $args['html'] = "<h3> <a href=\"http://keylab.cc/admin/\">Keylab</a>物管人員MPF提醒 </h3>
                    <br /><p>大廈 $build_name 的 $value->username MPF文件 尚未提交</p>
                    <br /><p>請到 <a href=\"http://keylab.cc/admin/\">Keylab</a> 中查看詳情!</p>
                    ";
        $temp_user_email = $wpdb->get_var($wpdb->prepare("select user_email from wp_users where ID = %d",$value->admin_wp_id));
        $args['to'] = [ $temp_user_email ];
        var_dump($args['to']);

        $wpdb->update("keylab_property_propman", [
            "MPF_notifi_day" => date("Y-m-d")
        ]
        , array('propman_id' => $value->propman_id));

        send_mailjet_email($args);

        //FCM
        foreach($a_fcm_type as $value){
            $temp_token = get_user_meta($value->admin_wp_id, $value, true);
            if(!empty($temp_token)){
                send_app_fcm($temp_token, 'Keylab物管人員MPF提醒', "大廈 $build_name 的 $value->username MPF文件 尚未提交");
            }
        }
    }
}

function BetweenDays($day1, $day2)
{
  $second1 = strtotime($day1);
  $second2 = strtotime($day2);

  return ($second2 - $second1) / 86400;
}