public function postSendMail() { if (!\Input::has('subject') && !\Input::has('message')) { return \Response::json(['type' => 'danger', 'message' => 'Email data not complete.']); } $recipients = \NewsLetter::getAllRecipients(); if (count($recipients) > 0) { $recipientsTo = []; foreach ($recipients as $recipient) { $recipientsTo[$recipient->email] = ['email' => $recipient->email]; } try { $mg = new Mailgun(env('MAILGUN_PRIVATE_KEY')); $data = ['content' => \Input::get('message')]; $inliner = new EmailInliner('emails.newsletter', $data); $content = $inliner->convert(); $mg->sendMessage('programmechameleon.com', ['from' => '*****@*****.**', 'to' => implode(",", array_keys($recipientsTo)), 'subject' => \Input::get('subject'), 'html' => $content, 'text' => 'Programme Chameleon Text Message', 'recipient-variables' => json_encode($recipientsTo)]); return \Response::json(['type' => 'success', 'message' => 'Message successfully sent.']); } catch (\Exception $e) { return \Response::json(['type' => 'danger', 'message' => $e->getMessage()]); } } else { return \Response::json(['type' => 'danger', 'message' => 'No emails to send.']); } }