Я занимаюсь разработкой проекта, и в нем два процесса.
- Отправка почты.
- Отправка ответа пользователю.
Мне нужно сделать все это, но это занимает много времени.Так что теперь я думаю использовать многопоточность.Может кто-нибудь помочь, как добиться этого в 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;
}
}