model = $model; } public function update($data) { return $this->model; } public function sendAndStore($data){ if(isset($data['send_mail_to']) && is_array($data['send_mail_to'])) { foreach ($data['send_mail_to'] as $booking_id => $on) { $booking = Booking::find($booking_id); if ($booking->customer) { $message = $this->prepareContent($booking, $data['message']); $subject = $this->prepareContent($booking, $data['subject']); $customer_mail = $this->store($booking, $subject, $message); $this->sendMail($customer_mail); } } } } public function store($booking, $subject, $message){ $customer_mail = CustomerMail::create([ 'booking_id' => $booking->id, 'customer_id' => $booking->customer_id, 'lead_id' => $booking->lead_id, 'email' => $booking->customer->email, 'subject' => $subject, 'message' => $message, ]); return $customer_mail; } private function sendMail($customer_mail){ try{ Mail::to($customer_mail->email)->send(new MailSendInfo($customer_mail->subject, $customer_mail->message)); } catch(\Exception $e){ // Never reached $customer_mail->fail = true; $customer_mail->error = $e->getMessage(); $customer_mail->save(); return false; } $customer_mail->send = true; $customer_mail->sent_at = now(); $customer_mail->save(); return true; } private function prepareContent($booking, $content){ $first_name = $booking->customer->firstname; $last_name = $booking->customer->name; $country = $booking->travel_country_id ? $booking->travel_country->name : "-"; $program = $booking->travelagenda_id ? $booking->travel_agenda->name : "-"; $salutation = $booking->customer->salutation->name; $dear = $booking->customer->salutation_id == 1 ? 'geehrter' : 'geehrte'; $search = ['#geehrte/r#', '#Anrede#', '#Vorname#', '#Nachname#', '#Reiseland#', '#Programm#']; $replace = [$dear, $salutation, $first_name, $last_name, $country, $program, $salutation]; $content = str_replace($search, $replace, $content); return $content; } }