PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/FormhandlerController.php

https://bitbucket.org/sydney-apps/web-divinemotions.co.uk-v0.0
PHP | 117 lines | 69 code | 17 blank | 31 comment | 8 complexity | f05258fff61c5d3215b1a51dd4d86e34 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Facades\Mail;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\Input;
  6. use Request;
  7. use App\Model\ContactFormResponse;
  8. use App\Model\GenericBaseResponse;
  9. use App\Model\BaseResponse;
  10. use Config;
  11. use App\Mail\Webform;
  12. use Illuminate\Support\Facades\View;
  13. class FormhandlerController extends Controller
  14. {
  15. /**
  16. * Show the application dashboard.
  17. *
  18. * @return \Illuminate\Http\Response
  19. */
  20. public function index()
  21. {
  22. // Getting all post data
  23. $a = array();
  24. if (Request::ajax()) {
  25. $data = Input::all();
  26. $name = $data['first_name'];
  27. if (iconv_strlen($name) < 2) {
  28. $errorResponse = new ContactFormResponse('f1', Config::get('constants.form.name'));
  29. array_push($a, $errorResponse);
  30. }
  31. // Field Email
  32. $email = $data['email'];
  33. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  34. $errorResponse = new ContactFormResponse('f3', Config::get('constants.form.email'));
  35. array_push($a, $errorResponse);
  36. }
  37. // Field Category
  38. // Field Message
  39. $msg = $data['message'];
  40. if (iconv_strlen($msg) < 10) {
  41. $errorResponse = new ContactFormResponse('f5', Config::get('constants.form.message'));
  42. array_push($a, $errorResponse);
  43. }
  44. if (count($a) > 1) {
  45. //print_r($message);die;
  46. $response = new GenericBaseResponse(Config::get('constants.status.validation_failure'), Config::get('constants.msg.validation_failure'), $a);
  47. return json_encode($response);
  48. } else {
  49. // To send HTML mail, the Content-type header must be set
  50. $headers[] = 'MIME-Version: 1.0';
  51. $headers[] = 'Content-type: text/html; charset=UTF-8';
  52. // Additional headers
  53. $headers[] = 'To: Admin <info@divine-healthcare.uk>';
  54. $headers[] = 'From: Cynosure Website <'.$email.'>';
  55. if (!isset($subject) || empty($subject)) {
  56. $subject = Config::get('constants.form.CONTACT_WEBMAIL');
  57. }
  58. try
  59. {
  60. /*Mail::send('emails.welcome', $data, function($message)
  61. {
  62. $message->from('us@example.com', 'Laravel');
  63. $message->to('info@sydneyapps.co.uk')->cc('mlambosydney@yahoo.com');
  64. $message->attach($pathToFile);
  65. });*/
  66. /* */
  67. $emailcontent = array(
  68. 'subject' => $subject,
  69. 'msg' => $data['message'],
  70. //'category' => $data['category'],
  71. 'name' => $data['first_name'],
  72. 'email'=> $data['email']
  73. );
  74. /*
  75. Mail::send('emails.contacthtml', $emailcontent, function($message) {
  76. $message->to('mlambosydney@yahoo.com', 'Receiver Name');
  77. $message->subject('This is the subject');
  78. });*/
  79. $view = View::make('emails.contacthtml', $emailcontent)->render();
  80. //TODO : REMOVE THE COMMENS
  81. // mail('info@divine-healthcare.uk', $subject, $view, implode("\r\n", $headers));
  82. $headers[] = 'MIME-Version: 1.0';
  83. $headerse[] = 'Content-type: text/html; charset=iso-8859-1';
  84. // Additional headers
  85. $headerse[] = 'To: <'.$email.'>';
  86. $headerse[] = 'From: Divine Healthcare <no-reply@divine-healthcare.uk>';
  87. //mail($email, 'We received your message below.', $view, implode("\r\n", $headerse));
  88. } catch (Exception $e) {
  89. $response = new BaseResponse(Config::get('constants.status.success'), 'An exception occured.');
  90. Log::critical($e->getMessage());
  91. return json_encode($response);
  92. }
  93. $response = new BaseResponse(Config::get('constants.status.success'), Config::get('constants.msg.success'));
  94. return json_encode($response);
  95. }
  96. }
  97. }
  98. }