Such a 500-error will result in errors being written to the log file. You should check there to see more specifically what's failing.
500 (Internal Server Error) when trying to send an email from a form with an Ajax call
Hi guys, i would really appreciate some insights. I am currently working on a One Page Website which happens to have a section for a contact form.
I have already configured my "Local Development Environment" with the mail sender service from mailgun with the appropriate credentials which works perfectly and sends the
email after the form has been submitted as expected.
However, the issue i have is:
Whenever i enter the data in the form and submit on the website which is on my "Production Server" it fails and when i check the browser Dev console i get the 500 errors as follows :
POST http://websitedomain.com/contact 500 (Internal Server Error) .... contact_me.js
XHR failed loading: POST "http://websitedomain.com/contact" .... contact_me.js
Keep in mind that i'm using the same mailgun credentials and code base from the local Environment that works.
Please see a few files of interest:
web.php
Route::get('/', function () {return view('home'); });
Auth::routes();
Route::get('/dashboard', 'DashboardController@index');
Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
Route::post('contact', 'EmailController@contactUs');
Route::get('contact', 'EmailController@contactUs');
EmailController.php
class EmailController extends Controller {
public function contactUs(Request $request)
{
// These are the extracted data from contact form fields
$data = array(
'name' => $request->name,
'email' => $request->email,
'phone' => $request->phone,
'bodyMessage' => $request->message
);
//Sends an email to us with data from contact form
Mail::send('emails.contact', $data, function($message) use ($data){
$message->from($data['email'],$data['name']);
$message->to('[email protected]');
$message->subject("New Contact Us Message");
});
//Sends a confirmation email to the client
Mail::send('emails.contact_confirmation', $data, function($message) use ($data){
$message->from('[email protected]', 'Website Name');
$message->to($data['email'],$data['name']);
$message->subject("Message Receipt Confirmation");
});
}
}
contact_me.js
$(function() {
// Allows the csrf tokens for the form to be generated
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$("#contactForm input,#contactForm textarea").jqBootstrapValidation({
preventSubmit: true,
submitError: function($form, event, errors) {
// additional error messages or events
},
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var message = $("textarea#message").val();
$.ajax({
url: "./contact",
type: "Post",
data: {
name: name,
phone: phone,
email: email,
message: message
},
cache: false,
success: function() {
// Success message
},
error: function() {
// Fail message
},
});
},
});
});
I have spent quite a bit a time seeking similar problems... while trying to resolve this issue, any help is greatly appreciated, please let me know if you have any further questions or suggestions. Thanks
Illuminate/Mail recommends guzzlehttp '^6.0' - perhaps you should verify the version of that library you're pulling in your composer.json. (What does that line say there?)
vendor/laravel/framework/src/Illuminate/Mail/composer.json
"guzzlehttp/guzzle": "Required to use the Mailgun and Mandrill mail drivers (~6.0)."
Please or to participate in this conversation.