Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

maculus1's avatar

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

0 likes
9 replies
burlresearch's avatar

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.

1 like
maculus1's avatar

@burlresearch, thanks for the response...i went ahead and checked logs and came up with this error:

production.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Class 'GuzzleHttp\Client' not found in /var/www/laravel/vendor/laravel/framework/src/Illuminate/Mail/TransportManager.php:174

Not sure if you've come across that, its a bit strange being that i verified in composer.json that GuzzleHttp is already installed and also re-ran composer update...but i'm still digging...

burlresearch's avatar
Level 40

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)."
1 like
maculus1's avatar

@burlresearch , thanks for the update, when i accessed the composer.json located at

vendor/laravel/framework/src/Illuminate/Mail/composer.json. It suggested as follows:

"guzzlehttp/guzzle": "Required to use the Mailgun and Mandrill mail drivers (~5.3|~6.0)."

So i modified my composer.json file to pull in version 6.0 and it threw the same exception even when i tried with version 5.3.

Please see snippet from my composer.json file :

"require": { "php": ">=5.6.4", "laravel/framework": "5.3.*", "mews/captcha": "2.1.7", "guzzlehttp/guzzle": "~6.0" },

I'm pulling it the correct way right?

maculus1's avatar

@burlresearch, i accidentally selected the issue solved button for your last reply, please ignore that.

burlresearch's avatar

Yes - that looks like it should work. Typically when working with composer dependencies you can use the tool from the command line:

$ composer require guzzlehttp/guzzle

If you simply edited the composer.json file, then it looks like you've done it correctly, but you still have to update the composer dependencies, if you haven't done that, issue:

$ composer update

and, composer should go do it's thing.

If that doesn't work still - I see that you said you it was working on DEV, and not on PROD - so you may want to check your PHP modules on the server, to see if they differ from those on DEV: php -m should list them, see if there are differences?

Aside from that - post whatever error message you're seeing now in the logs.

maculus1's avatar

Yes the modules are similar, and also initially i ran:

$ composer require guzzlehttp/guzzle

Which gives the most recent version "^6.3" which doesnt work on PROD. So I modified the composer.json file to "~5.3" as suggest by /vendor....Mail/composer.json , then ran the the command:

$ composer update

But i do get the same error, please see stack trace below:

[2017-12-11 17:36:57] production.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Class 'GuzzleHttp\Client' not found in /var/www/laravel/vendor/laravel/framework/src/Illuminate/Mail/TransportManager.php: 174

Stack trace:

burlresearch's avatar

hmm - well we know what the error is, but not how to fix it... wierd.

I would probably do a recursive delete on the entire PROD vendor/ folder, and re-upload the entire folder from your 'known working' copy, from the local Environment that works.

maculus1's avatar

Ok, gonna give that a shot and see what happens.

Please or to participate in this conversation.