I'm having a few issues with sending ajax to a email. I need to output the error json as at the moment if i leave a field blank or incorrect it returns "Error: Unprocessable Entity" The bigger issue i'm having is it's not sending emails and returns a 500 error but if I was to configure it around a bit and only send the name it works fine.
Mail.blade.php This is in a simple format
<h1></h1>
<h1></h1>
<h1></h1>
EmailController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Mail;
class EmailController extends Controller {
public function basic_email(Request $request){
$this->validate($request, [ 'name' => 'required', 'email' => 'required|email', 'message' => 'required' ]);
ContactUS::create($request->all());
Mail::send('emails.mail',
array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'message' => $request->get('message')
), function($message)
{
$message->from('test@test.com');
$message->to('test@test.com', 'Admin')->subject('Feedback');
});
return response()->json('success', 'Thanks for contacting us!');
}
}
/**
public function attachment_email(){
$data = array('name'=>"Virat Gandhi");
Mail::send('emails.mail', $data, function($message) {
$message->to('abc@gmail.com', 'Tutorials Point')->subject
('Laravel Testing Mail with Attachment');
$message->attach('C:\laravel-master\laravel\public\uploads\image.png');
$message->attach('C:\laravel-master\laravel\public\uploads\test.txt');
$message->from('xyz@gmail.com','Virat Gandhi');
});
echo "Email Sent with attachment. Check your inbox.";
}
*/
/**
* Display a listing of the myform.
*
* @return \Illuminate\Http\Response
public function myform()
{
return view('pages.contact');
}
/**
* Display a listing of the myformPost.
*
* @return \Illuminate\Http\Response
public function myformPost(Request $request)
{
$validator = Validator::make($request->all(), [
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required|email',
'address' => 'required',
]);
if ($validator->passes()) {
return response()->json(['success'=>'Added new records.']);
Mail::send('email.welcome', function ($message)
{
$message->to('blakeste@hotmail.com', 'Stephen')->subject('Welcome to Expertphp.in!');
});
}
return response()->json(['error'=>$validator->errors()->all()]);
}
*/
request.php
<?php
class ContactFormRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email',
'message' => 'required',
];
}
}
Contact.Blade.PHP
<html>
<head>
<title>Test</title>
<script src = "http://ift.tt/1yCEpkO">
</script>
<meta name="csrf-token" content="" />
<script>
function getMessage(){
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
var name = $('.namebox').val();
var email = $('.namebox2').val();
var message = $('.namebox3').val();
$.ajax({
type:'post',
url:'/sendbasicemail',
data: {_token: CSRF_TOKEN, name: name, email: email, message: message},
dataType: 'JSON',
success:function(data){
alert("DONE");
console.log(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown, request) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
console.log(name, email, message);
}
});
}
</script>
</head>
<ul>
@foreach($errors->all() as $error)
<li></li>
@endforeach
</ul>
{!! Form::open() !!}
<div class="form-group ">
{!! Form::label('Name:') !!}
{!! Form::text('name', '',['class'=>'namebox', 'placeholder'=>'Enter Name']) !!}
<span class="text-danger"></span>
</div>
<div class="form-group ">
{!! Form::label('Email:') !!}
{!! Form::text('email', '',['class'=>'namebox2', 'placeholder'=>'Enter Email']) !!}
<span class="text-danger"></span>
</div>
<div class="form-group ">
{!! Form::label('Message:') !!}
{!! Form::textarea('message', '',['class'=>'namebox3', 'placeholder'=>'Enter Message']) !!}
<span class="text-danger"></span>
</div>
{!! Form::close() !!}
<body>
<div id = 'msg'>This message will be replaced using Ajax.
Click the button to replace the message.</div>
<?php
echo Form::button('Replace',['onClick'=>'getMessage()']);
?>
</body>
</html>
web.php
Route::get('/ajax',function(){
return view('pages.contact');
});
Route::post('/sendbasicemail','EmailController@basic_email');
On a side note I have tried some outside so you may notice some differences
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire