public function send_email($dummy)
 {
     $rules = array('from_name' => 'required|max:128', 'from_email' => 'required|email|max:255', 'subject' => 'required|max:128', 'emailbody' => 'required');
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         return Response::json(array('validation' => $validator->messages()->toArray()));
     } else {
         $from_name = Input::get('from_name');
         $from_email = Input::get('from_email');
         $selected = Input::get('to');
         $subject = Input::get('subject');
         $emailbody = Input::get('emailbody');
         $from = $from_name . ' (' . $from_email . ')';
         $recipients = Subscriber::whereIn('email', $selected)->where('active', '=', 1)->get();
         $email = new Email();
         $email->from = $from;
         $email->subject = $subject;
         $email->message = $emailbody;
         $email->save();
         $email_id = $email->id;
         $numrecipients = $recipients->count();
         $numsent = 0;
         foreach ($recipients as $key => $recipient) {
             $tracker = new Tracker();
             $tracker->subscriber_id = $recipient->id;
             $tracker->email_id = $email_id;
             $tracker->save();
             $tracker_id = $tracker->id;
             $tracker_url = URL::to('tracker/' . $tracker_id);
             $unsubscriber_url = URL::to('unsubscribe/' . $tracker_id);
             $subscriber = $recipient;
             $data = array('emailbody' => $emailbody, 'tracker' => $tracker_url, 'unsubscribe' => $unsubscriber_url, 'subscriber' => $subscriber);
             $to_email = $subscriber->email;
             $to_name = $subscriber->first_name . ' ' . $subscriber->last_name;
             $issent = Mail::send('emails.sub-emails', $data, function ($message) use($from_email, $from_name, $to_email, $to_name, $subject) {
                 $message->from($from_email, $from_name)->to($to_email, $to_name)->subject($subject);
             });
             if ($issent) {
                 $numsent += 1;
             } else {
                 $tracker->bounced = 1;
                 $tracker->save();
             }
         }
         if ($numsent == $numrecipients) {
             return Response::json(array('success' => 'Your email was successfully sent to <b>' . $numsent . '</b> subscribers out of the ' . $numrecipients . ' subscribers you selected. <b>Rejoice!</b>'));
         } else {
             return Response::json(array('success' => 'Your email was successfully sent to <b>' . $numsent . '</b> subscribers out of the ' . $numrecipients . 'All bounces have been logged.'));
         }
     }
 }
 public function store()
 {
     $name = Jamesy\Sanitiser::trimInput(Input::get('name'));
     $validation = Jamesy\MyValidations::validate(['name' => $name], $this->rules);
     if ($validation != NULL) {
         return Redirect::back()->withErrors($validation)->withInput();
     } else {
         if (Input::file('file')) {
             $fileValidation = Jamesy\MyValidations::validate(['file' => Input::file('file')], $this->fileSizeRules);
             if ($fileValidation != NULL) {
                 return Redirect::back()->withErrors($fileValidation)->withInput();
             } else {
                 $maillist = new Maillist();
                 $maillist->name = $name;
                 if (Input::get('active') == '0' || Input::get('active') == '1') {
                     $maillist->active = Input::get('active');
                 }
                 $maillist->save();
                 $input = Input::file('file');
                 $ext = pathinfo($input->getClientOriginalName(), PATHINFO_EXTENSION);
                 if ($ext != 'xlt' && $ext != 'xls' && $ext != 'csv') {
                     return Redirect::back()->withIssues('You attempted the import with an invalid file. File must be Excel or CSV')->withInput();
                 } else {
                     $import = new Jamesy\Imports($input, $this->subscriberRules);
                     $importResult = $import->getInsertArray();
                     if (is_array($importResult)) {
                         $totalNum = $importResult[0];
                         $duplicatesNum = $importResult[1];
                         $passedArr = $importResult[2];
                         $passedNum = count($passedArr);
                         $failedNum = (int) $totalNum - (int) $duplicatesNum - (int) $passedNum;
                         $timestamp = $importResult[3];
                         $existingEmails = $importResult[4];
                         $newPassedArr = [];
                         if ($maillist->active == 0) {
                             foreach ($passedArr as $subscriber) {
                                 $subscriber['active'] = 0;
                                 $newPassedArr[] = $subscriber;
                             }
                         } else {
                             $newPassedArr = $passedArr;
                         }
                         if (count($newPassedArr)) {
                             Subscriber::insert($newPassedArr);
                             $generalList = Maillist::find(1);
                             $generalList->touch();
                             $maillist->touch();
                             $subscribers = Subscriber::where('created_at', $timestamp)->where('updated_at', $timestamp)->get();
                             foreach ($subscribers as $subscriber) {
                                 $generalList->subscribers()->attach($subscriber->id);
                                 $maillist->subscribers()->attach($subscriber->id);
                             }
                         }
                         if (count($existingEmails)) {
                             $alreadySubs = Subscriber::whereIn('email', $existingEmails)->get();
                             if (count($alreadySubs)) {
                                 foreach ($alreadySubs as $subscriber) {
                                     $maillist->subscribers()->attach($subscriber->id);
                                     if ($maillist->active == 0) {
                                         $subscriber->active = 0;
                                         $subscriber->save();
                                     }
                                 }
                             }
                         }
                         $message = "New list created.";
                         $message .= "<br /><b>{$totalNum}</b> " . str_plural('row', $totalNum) . " found in excel file.";
                         if ($duplicatesNum) {
                             $message .= "<br /><b>{$duplicatesNum}</b> had duplicate email addresses.";
                         }
                         if ($passedNum == 1) {
                             $message .= "<br /><b>1</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails passed validation and was stored.";
                         } else {
                             $message .= "<br /><b>{$passedNum}</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails passed validation and were stored.";
                         }
                         if ($failedNum == 1) {
                             $message .= "<br /><b>1</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails failed validation (no first name, last name or bad email) and was NOT stored.";
                         } elseif ($failedNum > 1) {
                             $message .= "<br /><b>{$failedNum}</b> out of the <b>" . ($totalNum - $duplicatesNum) . "</b> with unique emails failed validation (no first name, last name or bad email) and were NOT stored.";
                         }
                         return Redirect::to('dashboard/lists')->withSuccess($message);
                     } else {
                         return Redirect::back()->withIssues($importResult);
                     }
                 }
             }
         } else {
             $maillist = new Maillist();
             $maillist->name = $name;
             if (Input::get('active') == '0' || Input::get('active') == '1') {
                 $maillist->active = Input::get('active');
             }
             $maillist->save();
             return Redirect::to('dashboard/lists')->withSuccess('New list created');
         }
     }
 }
 public function get_subscribers($dummy)
 {
     $selected_subs = Subscriber::whereIn('id', Input::get('subsarray'))->get();
     return Response::json($selected_subs);
 }
 public function bulk_destroy()
 {
     Subscriber::whereIn('id', Input::get('subscribers'))->delete();
     $destroyedNum = count(Input::get('subscribers'));
     return Redirect::to('dashboard/subscribers/trash')->withSuccess($destroyedNum . ' ' . str_plural('subscriber', $destroyedNum) . ' permanently deleted.');
 }