public function storeContacts($campaign_id, Request $request)
 {
     set_time_limit(0);
     $date = \Carbon\Carbon::parse($request->input('dateTime'))->toDateTimeString();
     $file = $request->file('file');
     $campaign = Campaign::find($campaign_id);
     $contacts = [];
     $results = \Excel::selectSheetsByIndex(0)->load($file, function ($reader) use($campaign) {
         $reader->ignoreEmpty();
         $reader->each(function ($row) use($campaign) {
             if (isset($row['email']) && $row['email']) {
                 $email = $row['email'];
                 $email = trim($email);
                 if (!$this->checkEmail($email)) {
                     return false;
                 }
                 $contact = Contact::firstOrCreate(['email' => $email, 'client_id' => $campaign->client->id]);
                 if (isset($row['first_name'])) {
                     $contact->first_name = $row['first_name'];
                 }
                 if (isset($row['last_name'])) {
                     $contact->last_name = $row['last_name'];
                 }
                 if (isset($row['company'])) {
                     $contact->company = $row['company'];
                 }
                 if (isset($row['title'])) {
                     $contact->title = $row['title'];
                 }
                 if (isset($row['address'])) {
                     $contact->address = trim(str_replace("\n", ' ', $row['address']));
                 }
                 if (isset($row['city'])) {
                     $contact->city = $row['city'];
                 }
                 if (isset($row['state'])) {
                     $contact->state = $row['state'];
                 }
                 if (isset($row['zip'])) {
                     $contact->zip = $row['zip'];
                 }
                 $contact->save();
                 if (!$contact->bounced && !$contact->unsubscribe) {
                     array_push($this->contacts, $contact->id);
                 }
             }
         });
     });
     $contactCount = 0;
     foreach ($this->contacts as $contact) {
         if (!$campaign->contacts->contains($contact)) {
             $campaign->contacts()->attach($contact);
             $contactCount++;
         }
     }
     return redirect()->route('admin.campaigns.show', $campaign->id)->with('message', "{$contactCount} contacts added to Campaign");
 }
 public function store($touch_id, Request $request)
 {
     $date = \Carbon\Carbon::now()->toDateTimeString();
     $emails = explode(',', $request->input('email'));
     $count = 0;
     foreach ($emails as $email) {
         $email = trim($email);
         if (!$this->checkEmail($email)) {
             continue;
         }
         $touch = Touch::find($touch_id);
         $contact = Contact::firstOrCreate(['email' => $email, 'client_id' => $touch->campaign->client->id]);
         if ($contact->unsubscribe || $contact->bounced) {
             return redirect()->back()->with('message', "{$contact->email} is unreachable or has unsubscribed");
         }
         $email = Email::create(['subject' => $touch->subject, 'reply_to' => $touch->campaign->client->reply_to, 'from' => $touch->campaign->client->reply_to, 'send_on' => $date, 'template' => "emails.{$touch->template}", 'draft' => false, 'trackable' => false, 'campaign_id' => $touch->campaign->id, 'contact_id' => $contact->id, 'touch_id' => $touch->id]);
         $email->salted_id = bcrypt($email->id);
         $email->save();
         $count++;
     }
     return redirect()->back()->with('message', "{$count} test emails sent");
 }
 public function signupForward(Request $request)
 {
     $email = $request->input('email');
     if (!$this->checkEmail($email)) {
         return redirect()->back()->withInput()->with('error', "{$email} is not a valid email");
     }
     $campaign = Campaign::find($request->input('campaign_id'));
     $touch = $campaign->touchs()->first();
     $date = \Carbon\Carbon::now()->toDateTimeString();
     $contact = Contact::firstOrCreate(['email' => $email, 'client_id' => $touch->campaign->client->id]);
     if ($contact->unsubscribe || $contact->bounced) {
         return redirect()->back()->with('message', "{$contact->email} is unreachable or has unsubscribed");
     }
     $newEmail = Email::create(['subject' => $touch->subject, 'reply_to' => $touch->campaign->client->reply_to, 'from' => $touch->campaign->client->reply_to, 'send_on' => $date, 'template' => "emails.{$touch->template}", 'draft' => false, 'trackable' => false, 'campaign_id' => $touch->campaign->id, 'contact_id' => $contact->id, 'touch_id' => $touch->id]);
     $newEmail->salted_id = bcrypt($newEmail->id);
     $newEmail->save();
     return redirect()->back()->with('message', "We will contact {$email} right away");
 }