public function postEmail(Request $request, Mailer $mailer)
 {
     $this->validate($request, ['template_id' => 'required|exists:email_template,id']);
     $departmentId = auth()->user()->department_id;
     if ($request->get('department_id')) {
         $departmentId = $request->get('department_id');
     }
     $users = [];
     if ($request->get('user_type')) {
         $users = array_merge(User::where('department_id', $departmentId)->where('user_type_id', $request->get('user_type'))->get()->toArray());
     }
     if ($request->get('users')) {
         $users = array_merge($users, User::whereIn('id', $request->get('users'))->get()->toArray());
     }
     $emailTemplate = EmailTemplate::find($request->get('template_id'));
     if (strpos($emailTemplate->body, '[name]')) {
         $template = new Template();
         foreach ($users as $user) {
             $body = $template->parser($emailTemplate->body, ['name' => $user['name']]);
             $mailer->send('layouts.partials.email', ['body' => $body], function ($message) use($emailTemplate, $user) {
                 $message->subject($emailTemplate->subject);
                 $message->to($user['email']);
             });
         }
     } else {
         $emails = array_column($users, 'email');
         $emails = array_unique($emails);
         $mailer->send('layouts.partials.email', ['body' => $emailTemplate->body], function ($message) use($emailTemplate, $emails) {
             $message->subject($emailTemplate->subject);
             $message->to($emails);
         });
     }
     return redirect()->action('Admin\\EmailTemplateController@getEmail')->with('success', 'emails-send');
 }
Example #2
0
 public function view_template(Request $request)
 {
     $tem = EmailTemplate::find($request->id);
     return $tem->content;
 }