コード例 #1
0
 public function pdf($id)
 {
     $hashId = \Jamesy\Miscellaneous::encryptId($id);
     $pdf = new SimpleHTMLToPDF();
     $link = URL::to('email/show/' . $hashId);
     $pdf->display($link);
 }
コード例 #2
0
 public function send()
 {
     $inputs = [];
     foreach (Input::all() as $key => $input) {
         $inputs[$key] = Jamesy\Sanitiser::trimInput($input);
     }
     $rules = Input::has('save_draft') ? $this->draftRules : $this->rules;
     $validation = Jamesy\MyValidations::validate($inputs, $rules);
     if ($validation != NULL) {
         return Redirect::back()->withErrors($validation)->withInput();
     } else {
         if (Input::has('save_draft')) {
             $draft = Input::has('was_draft') ? Draft::find(Input::get('was_draft')) : new Draft();
             $draft->user_id = $this->user->id;
             $draft->tag_id = $inputs['tag_id'];
             $draft->subject = $inputs['subject'];
             $draft->email_body = $inputs['email_body'];
             $draft->save();
             return Redirect::to('dashboard/emails/drafts');
         } else {
             $maxSendNum = 999;
             $email = new Email();
             $email->user_id = $this->user->id;
             $email->tag_id = $inputs['tag_id'];
             $email->from = Setting::getFromName($this->user) . " (" . Setting::getFromEmail($this->user) . ")";
             $email->reply_to = Setting::getFromName($this->user) . " (" . Setting::getReplyToEmail($this->user) . ")";
             $email->subject = $inputs['subject'];
             $email->email_body = $inputs['email_body'];
             $email->save();
             $maillists = Input::has('mail_lists') ? $inputs['mail_lists'] : [];
             $emails = Input::has('subscribers') ? $inputs['subscribers'] : [];
             $selectedSubs = Subscriber::getSelectedSubscribersForEmail($maillists, $emails);
             while (count($selectedSubs)) {
                 $data = ['email_body' => $inputs['email_body'], 'email_id' => \Jamesy\Miscellaneous::encryptId($email->id)];
                 $variables = [];
                 $tag = Tag::find($inputs['tag_id']);
                 $emailId = $email->id;
                 $numToSlice = count($selectedSubs) > $maxSendNum ? $maxSendNum : count($selectedSubs);
                 $subscribers = $numToSlice < count($selectedSubs) ? array_slice($selectedSubs, 0, $numToSlice) : $selectedSubs;
                 foreach ($subscribers as $subscriber) {
                     $variables[$subscriber['email']] = ['id' => $subscriber['id'], 'first_name' => $subscriber['first_name'], 'last_name' => $subscriber['last_name']];
                 }
                 $result = Mailgun::send('backend.emails.send_templates.main', $data, function ($message) use($subscribers, $inputs, $variables, $emailId, $tag) {
                     $message->from(Setting::getFromEmail($this->user), Setting::getFromName($this->user))->replyTo(Setting::getReplyToEmail($this->user), Setting::getFromName($this->user))->subject($inputs['subject']);
                     foreach ($subscribers as $subscriber) {
                         $message->to($subscriber['email'], $subscriber['first_name'] . ' ' . $subscriber['last_name']);
                     }
                     $message->recipientVariables($variables);
                     $message->tag($tag->name);
                 });
                 if (is_object($result)) {
                     if ($result->http_response_code == 200) {
                         foreach ($subscribers as $subscriber) {
                             $apicall_id = explode('<', explode('@', $result->http_response_body->id)[0])[1];
                             $analytic = new Analytic();
                             $analytic->subscriber_id = $subscriber['id'];
                             $analytic->email_id = $email->id;
                             $analytic->recipient = $subscriber['email'];
                             $analytic->apicall_id = $apicall_id;
                             $analytic->status = 'queued';
                             $analytic->save();
                         }
                     }
                 }
                 $selectedSubs = array_slice((array) $selectedSubs, $numToSlice);
             }
             if (Input::has('was_draft')) {
                 Draft::find(Input::get('was_draft'))->delete();
             }
             return Redirect::to('dashboard/emails/sent');
         }
     }
 }