示例#1
0
 public function send(Request $request)
 {
     $validator = Validator::make($request->all(), ['contact_id' => 'required', 'to' => 'required', 'subject' => 'required', 'body' => 'required']);
     if ($validator->passes()) {
         if ($request->file('attachment')) {
             $file = $request->file('attachment');
             $destinationPath = 'uploads/' . str_random(8) . "-" . time();
             $filename = $file->getClientOriginalName();
             //$extension = $file->getClientOriginalExtension();
             $uploadSuccess = $request->file('attachment')->move($destinationPath, $filename);
             if ($uploadSuccess) {
                 $attachment = new Attachment();
                 $attachment->attachment_filename = $destinationPath . "/" . $filename;
                 $attachment->save();
             }
         }
         foreach ($request->input('contact_id') as $contact_id) {
             if ($contact_id > 0) {
                 $message_log = new Message();
                 $message_log->contact_id = $contact_id;
                 $message_log->user_id = auth()->user()->id;
                 $message_log->message_type_id = $this->message_type->where('message_type_name', 'LIKE', '%Email%')->get()->first()->id;
                 $message_log->school_id = $request->input('school_id');
                 if (isset($attachment)) {
                     $message_log->attachment_id = $attachment->id;
                 }
                 $contents = "";
                 $body = $request->input('body');
                 if (isset($attachment)) {
                     //$body .= "\n\nAttachment: ".$attachment->fileLink();
                 }
                 $body .= "\n\n____________________\n" . auth()->user()->username . "\nBrentwood University Counselling";
                 $contents .= "To: " . $request->input('to') . "\n";
                 $contents .= "From: " . auth()->user()->email . "\n";
                 $contents .= "Subject: " . $request->input('subject') . "\n\n";
                 $contents .= "Message: " . $body;
                 $message_log->contents = $contents;
                 $message_log->save();
             }
         }
         $body = preg_replace('/\\n{2,}/', "</p><p>", trim($body));
         $body = preg_replace('/\\n/', '<br>', $body);
         $body = "<p>{$body}</p>";
         $data = ['body' => $body];
         //$sent_to = $request->input('to');
         if (strpos($request->input('to'), ',') !== false) {
             $to_array = explode(',', $request->input('to'));
             foreach ($to_array as $sent_to) {
                 if (isset($attachment)) {
                     Email::sendEmail(trim($request->input('to')), $request->input('subject'), $data, $attachment);
                 } else {
                     Email::sendEmail(trim($request->input('to')), $request->input('subject'), $data);
                 }
                 $this->addEmailAddress($sent_to);
             }
         } else {
             if (isset($attachment)) {
                 Email::sendEmail(trim($request->input('to')), $request->input('subject'), $data, $attachment);
             } else {
                 Email::sendEmail(trim($request->input('to')), $request->input('subject'), $data);
             }
             $this->addEmailAddress($request->input('to'));
         }
         return redirect()->to('/')->with(['success' => 'Email to <span class="mono">' . $request->input('to') . '</span> sent']);
     } else {
         return redirect()->back()->withInput()->withErrors($validator->messages());
     }
 }