簡體   English   中英

如何在PHP中發送郵件之前向用戶發送響應

[英]How to send response to user before sending mail in php

我正在開發一個項目,有兩個過程。

  1. 發送郵件。
  2. 將響應發送回用戶。

我需要做所有這一切,但是要花很多時間。 所以現在我正在考慮使用多線程。 任何人都可以幫助如何在php中實現嗎?

我只需要盡快發送回復。 這是我的代碼,我創建了一個發送郵件的功能,並且在成功發送完完整的郵件后,我使用JSON編碼向用戶發送響應。

/ 郵件功能 /

public function emailSent($booking_id, $mailHeading, $smallHeading, $subject, $smallHeadingcustomer ,$status, $senttoestimator = true, $senttocustomer = true, $mailSent = false){
        if(!empty($booking_id)){
             $bookingData = $this->Cnc_model->getData('bookings', array('*'), array('id'=>$booking_id));

            if(!empty($bookingData)){
                if(!empty($bookingData[0]['estimator_id']) && $bookingData[0]['estimator_id'] != 0){
                    $name = $this->Cnc_model->getData('users',array('username', 'email'), array('user_id'=> $bookingData[0]['estimator_id']));   
                    $estimator_name = $name[0]['username'];
                    $estimator_mail = $name[0]['email']; 
                    }
                if($bookingData[0]['customer_id'] != 0){
                    $cname = $this->Cnc_model->getData('customers',array('name', 'office_contact_email'), array('id'=> $bookingData[0]['customer_id']));   
                    $customer_name = $cname[0]['name'];
                    $customer_mail = $cname[0]['office_contact_email'];
                }

                $fromEmail = FROM;
                $emailData = array(
                        "customeName" => $customer_name,
                        "estimator_name"=>$estimator_name,
                        "booking_title" => !empty($bookingData[0]['booking_title']) ? $bookingData[0]['booking_title']: 'No Title',
                        "booking_date" => date("d-m-Y",strtotime($bookingData[0]['booking_date'])),
                        "start_time" => date("h:i a",strtotime($bookingData[0]['start_time'])),
                        "end_time" => date("h:i a",strtotime($bookingData[0]['end_time'])),
                        "address" =>isset($bookingData[0]['location']) ? $bookingData[0]['location']: '',
                        "notes" => isset($bookingData[0]['notes']) && !empty($bookingData[0]['notes'])?$bookingData['notes']:'No Notes For this booking',
                        "status" => isset($status) && !empty($status)? $status:'',
                        "bookingId" => $bookingData[0]['id'],
                        'mainHeading'=> isset($mailHeading) && !empty($mailHeading)?$mailHeading:'',
                        'smallHeading'=>isset($smallHeading) && !empty($smallHeading)?$smallHeading:'',
                        'smallHeadingcustomer' => isset($smallHeadingcustomer) && !empty($smallHeadingcustomer)?$smallHeadingcustomer: '',
                    );
                if($bookingData[0]['status']!=3 && $bookingData[0]['status']!=0 || $mailSent == true ){
                  if(!empty($emailData) && $senttocustomer){   
                        $newBooking= $this->load->view('templates/email-template/new_booking', $emailData, TRUE);
                        $this->email->from($fromEmail); 
                        $this->email->to($customer_mail);  
                        $this->email->subject($subject);
                        $this->email->message($newBooking);
                        $this->email->send();
                    }
                    if(!empty($estimator_mail) && $senttoestimator){
                        $newEstimatorBooking = $this->load->view('templates/email-template/new_booking_estimator', $emailData, TRUE);
                        $this->email->from($fromEmail); 
                        $this->email->to($estimator_mail);  
                        $this->email->subject($subject);
                        $this->email->message($newEstimatorBooking);
                        $this->email->send();
                    }  
                }  
            }
        }
        else{
            return false;
        }
        return true;
    }  

/ 刪除預訂功能 /

public function deleteBooking($id){
     $where = array('id'=>$id); 
     $bookingData = $this->Cnc_model->getData('bookings', array('*'), array('id'=>$id)); 
     if(!empty($bookingData)){
        if(!empty($bookingData[0]['estimator_id']) && $bookingData[0]['estimator_id'] != 0){
                    $name = $this->Cnc_model->getData('users',array('username', 'email'), array('user_id'=> $bookingData[0]['estimator_id']));   
                    $estimator_name = $name[0]['username'];
                    $estimator_mail = $name[0]['email']; 
                    }

                if($bookingData[0]['customer_id'] != 0){
                    $cname = $this->Cnc_model->getData('customers',array('name', 'office_contact_email'), array('id'=> $bookingData[0]['customer_id']));   
                    $customer_name = $cname[0]['name'];
                    $customer_mail = $cname[0]['office_contact_email'];
                }
                $mailHeading = 'Booking Deleted';
                $smallHeading = 'Your Booking with '.$customer_name.' on '.$bookingData[0]['booking_date'].' has been deleted by admin.';
                $subject = 'Booking Deleted';
                $smallHeadingcustomer = 'Your Booking with '.$estimator_name.' on '.$bookingData[0]['booking_date'].' has been deleted by admin.';
                $status = 5;

/ *這里我正在呼叫發送電子郵件功能* /

                $result = $this->emailSent($id, $mailHeading, $smallHeading, $subject, $smallHeadingcustomer,$status);

/ *收到響應后,我發送對ajax請求的響應* /

              if($result){
                    $deleteBooking = $this->Cnc_model->rowsDelete('bookings', $where);
                    if($deleteBooking){
                        $this->logThis('Deleted', 'Booking', 'Booking Deleted', array('id' => $id));
                        $this->Cnc_model->addNotification($id,22,$this->auth_user_id,$bookingData[0]['estimator_id'],'Booking Deleted',$bookingData[0]['booking_title'].' deleted on '.date("m/d/Y").' at '.date('H:i a'));
                        $where = '';
                        $data['success'] = true; 
                        $data['message'] = 'Booking deleted successfully.';
                        echo json_encode($data);
                        die;     
                    }else{
                        $where = '';
                        $data['success'] = false; 
                        $data['message'] = 'Error while deleting booking.';
                        echo json_encode($data);
                       die;
                    }
               }  
            }
     else{
        $data['success'] = false; 
        $data['message'] = 'Error while deleting booking.';
        echo json_encode($data);
        die;  
     }
}

這取決於您想走多深。 理想的方法是運行RabbitMQ或Gearman服務器並設置一個“發送郵件”作業供它們處理。 提交作業非常快,並且可以使腳本完成並將響應發送回用戶,但是當然這將涉及設置更多的基礎結構,編寫/維護更多的代碼以處理該作業以及處理重試/錯誤狀態。

問題最少的方法可能是運行將中繼郵件的本地郵件服務器。 移交應該非常快,可以提供更好的“有效”響應(至少用戶會知道郵件已移交給本地郵件服務器)並允許腳本繼續進行。 如果電子郵件失敗,則由本地郵件服務器作為對管理員或“發件人”用戶的退回處理。

概述一種方式在這里將意味着第一個發送響應,則處理的電子郵件-這意味着它不可能報告故障給用戶。 不太理想,但這取決於您的情況-也許管理員可以處理失敗的消息並可以重新發送?

您可以使用ignore_user_abort(true); 設置客戶端斷開連接是否應導致腳本中止。 這將斷開您當前的連接,然后將響應發送到前端,但仍執行其余代碼。

 ignore_user_abort(true);

    ob_start();
    echo "success";

    $serverProtocole = filter_input(INPUT_SERVER, 'SERVER_PROTOCOL', FILTER_SANITIZE_STRING);
    header($serverProtocole.' 200 OK');
    header('Content-Encoding: none');
    header('Content-Length: '.ob_get_length());
    header('Connection: close');

    ob_end_flush();
    ob_flush();
    flush();

最初對API使用后端驗證,然后使用上述代碼發送響應,然后您可以編寫代碼向用戶發送電子郵件,這樣前端將獲得響應,腳本將執行直到遇到返回或死亡為止。 這種方法的問題在於,如果在發送電子郵件時出錯,則無法向前端發送響應。

否則,您可以在代碼中使用郵件隊列來稍后執行任務

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM