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

ErikRobles's avatar

500 server error on submit and delete of contact form

Hello. I launched my laravel application to the live server after running extensive tests locally. Every route and link worked fine. Now that it is live, I am having a problem submitting the contact form and on the backend, deleting the form entry data. I get a 500 server error and the url route in the address bar reflects either the store route or the delete route respectively. I am not sure on these particular routes, what I did wrong because all my other backend routes, edit, delete, etc work just fine. If anyone else can spot what I might have missed, I would sure appreciate it. Thank you in advance. Here is my Code: Store and Delete Routes:

Route::post('contact-form2', [ContactTwoController::class, 'storeContactForm2'])->name('contact-form2.store');
    Route::get('delete_contact2/{id}', [ContactTwoController::class, 'deleteContactMessage2'])->name('delete_contact2');

My form tag

        <form class="contact-form pb-3" method="post" id="contactform" action="{{ route('contact-form2.store') }}">

My Contact Controller

<?php

namespace App\Http\Controllers;

use App\Models\ContactTwo;
use Illuminate\Http\Request;

class ContactTwoController extends Controller
{
    public function frontendTwoContactMessagesView() {
        $frontendTwoMessages = ContactTwo::all();
        return view('admin.contact_messages.frontend_two_messages_view', compact('frontendTwoMessages'));
    }

    protected function formResponse2() {
        $notification = array(
            'message' => 'Mensaje Enviado Exisotamente',
            'alert-type' => 'success'
        );
        return redirect()->to('inicio')->with($notification);
    }

    public function storeContactForm2(Request $request)
    {
        if ($request->faxonly) { 
            return $this->formResponse2(); 
        } 
         

        $request->validate([
            'name' => 'required',
            'mail' => 'required|email',
            'mensaje' => 'required',
        ]);

       

        $input = $request->all();

        
        ContactTwo::create($input);

        //  Send mail to admin
        \Mail::send('contact2Mail', array(
            'name' => $input['name'],
            'empresa' => $input['empresa'],
            'mail' => $input['mail'],
            'tel1' => $input['tel1'],
            'tel3' => $input['tel3'],
            'tel2' => $input['tel2'],
            'pais' => $input['pais'],
            'mensaje' => $input['mensaje'],
        ), function($message) use ($request){
            $message->from('emailaddress');
            $message->to('emailaddress', 'Admin')->subject($request->get('empresa'));
        });
        $notification = array(
            'message' => 'Mensaje Enviado Exisotamente',
            'alert-type' => 'success'
        );
        return Redirect()->route('contact_us')->with($notification);
    }

    public function ContactMessages2() {
        $contacts = ContactTwo::all();
        return view('admin.contacts', compact('contacts'));
    }

    public function deleteContactMessage2($id) {
        $contact = ContactTwo::find($id);
        $contact->delete();
        $notification = array(
            'message' => 'Message Successfully Deleted',
            'alert-type' => 'success'
        );
        return Redirect()->route('frontend_two_messages')->with($notification);
    }
}

By the way, I am receiving the data in the database and can see that data in the backend. But like I mentioned earlier, when I go to delete it, I get a 500 server error. when I go back and then refresh the page, the item shows as successfully deleted and is removed from the database.

So confused. Any help would be great. Thank you.

0 likes
15 replies
ErikRobles's avatar

@Snapey Thank you for your reply. I am looking at the logs which, excuse my ignorance, don't make much sense to me. Here they are:

#50 /home/lionmmyk/lionsfield/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(86): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#51 /home/lionmmyk/lionsfield/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle(Object(Illuminate\Http\Request), Object(Closure))
#52 /home/lionmmyk/lionsfield/vendor/fruitcake/laravel-cors/src/HandleCors.php(38): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#53 /home/lionmmyk/lionsfield/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fruitcake\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure))
#54 /home/lionmmyk/lionsfield/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php(39): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#55 /home/lionmmyk/lionsfield/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Http\Middleware\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
#56 /home/lionmmyk/lionsfield/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#57 /home/lionmmyk/lionsfield/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#58 /home/lionmmyk/lionsfield/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#59 /home/lionmmyk/public_html/index.php(52): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#60 {main}
"} 

Snapey's avatar

@ErikRobles you have only showed the end of the stack dump. Probably easiest if you delete the log file then create the error then view the file from the start, not just the last few lines

ErikRobles's avatar

@Snapey Thank you for your suggestion. I tried to post it but it is too large. So.... I must have a load of errors or what?

ErikRobles's avatar

@Snapey

[2021-10-24 17:07:47] local.ERROR: Connection could not be established with host server160.web-hosting.com :stream_socket_client(): unable to connect to ssl://server160.web-hosting.com:2525 (Connection refused) {"userId":1,"exception":"[object] (Swift_TransportException(code: 0): Connection could not be established with host server160.web-hosting.com :stream_socket_client(): unable to connect to ssl://server160.web-hosting.com:2525 (Connection refused) at /home/lionmmyk/lionsfield/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:262)
[stacktrace]

[2021-10-24 17:12:35] local.ERROR: Connection could not be established with host server160.web-hosting.com :stream_socket_client(): unable to connect to ssl://server160.web-hosting.com:2525 (Connection refused) {"userId":1,"exception":"[object] (Swift_TransportException(code: 0): Connection could not be established with host server160.web-hosting.com :stream_socket_client(): unable to connect to ssl://server160.web-hosting.com:2525 (Connection refused) at /home/lionmmyk/lionsfield/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:262)
[stacktrace]

[2021-10-24 17:12:45] local.ERROR: Connection could not be established with host server160.web-hosting.com :stream_socket_client(): unable to connect to ssl://server160.web-hosting.com:2525 (Connection refused) {"userId":1,"exception":"[object] (Swift_TransportException(code: 0): Connection could not be established with host server160.web-hosting.com :stream_socket_client(): unable to connect to ssl://server160.web-hosting.com:2525 (Connection refused) at /home/lionmmyk/lionsfield/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:262)
[stacktrace]

[2021-10-24 17:13:00] local.ERROR: Route [frontend_two_messages] not defined. {"userId":1,"exception":"[object] (Symfony\Component\Routing\Exception\RouteNotFoundException(code: 0): Route [frontend_two_messages] not defined. at /home/lionmmyk/lionsfield/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php:444)
[stacktrace]

[2021-10-24 17:20:13] local.ERROR: Connection could not be established with host server160.web-hosting.com :stream_socket_client(): unable to connect to ssl://server160.web-hosting.com:2525 (Connection refused) {"userId":1,"exception":"[object] (Swift_TransportException(code: 0): Connection could not be established with host server160.web-hosting.com :stream_socket_client(): unable to connect to ssl://server160.web-hosting.com:2525 (Connection refused) at /home/lionmmyk/lionsfield/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:262)
[stacktrace]

[2021-10-24 17:30:59] local.ERROR: Connection could not be established with host server160.web-hosting.com :stream_socket_client(): unable to connect to ssl://server160.web-hosting.com:2525 (Connection refused) {"userId":1,"exception":"[object] (Swift_TransportException(code: 0): Connection could not be established with host server160.web-hosting.com :stream_socket_client(): unable to connect to ssl://server160.web-hosting.com:2525 (Connection refused) at /home/lionmmyk/lionsfield/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php:262)
[stacktrace]

??? I notice a lot of reference to swiftmailer

Thank you @snapey for helping me out here. I am really trying to learn the ins and outs of Laravel and I appreciate your patience.

Snapey's avatar

@ErikRobles Could well be. Your local setup should be the same as the production. There is absolutely no justification for moving files around for shared hosting.

One possible reason is that you have a letter case issue. Check that your folder names are exactly the same as the class namespaces. ie, you cannot have a controller called Auth in a file called auth.php for instance.

Another issue might be that you need to run composer dump to rebuild the autoload map - particularly if you have moved classes.

Whats odd about your error though is that you are trying to call Auth in the Homecontroller without importing it, ie use Illuminate\Support\Facades\Auth; seems to be missing.

Look what is on line 10 of the Home Controller

ErikRobles's avatar

@Snapey This brings me to an important question. For security reasons, how does one go about hiding the .env from public view? If I could have everything in one folder, it would be so much easier.That is really the only reason I am using two different folders. I suppose something like this in the .htaccess file?

<Files .env>
    Order allow,deny
    Deny from all
</Files>
Snapey's avatar
Snapey
Best Answer
Level 122

@ErikRobles the way it is meant to be deployed is with all the files in the laravel project in one folder (or child folders) and ONLY the public folder accessible from the outside. This is achieved by setting the public folder the document root This is an important term to get to grips with.

This is NOT a problem to be solved by tinkering with .htaccess

1 like
ErikRobles's avatar

@Snapey Thank you for the info. I really appreciate it. Now begins the work of re-deploying my app correctly.

ErikRobles's avatar

@Snapey I just wanted to follow up on this. I found the problem. It had to do with the fact that I need to update my config mail.php to the mail of my host. Once done, all my errors went away and I was able to receive emails. I thank you as by sifting through the laravel log, I saw many references to Swift which I knew had to do with the mail function so that is where I focused. I did fix my delete function and now use forms so thank you very much for your assistance.

Snapey's avatar

Not your problem, but you broke a golden rule;

Route::get('delete_contact2/{id}', [ContactTwoController::class, 'deleteContactMessage2'])->name('delete_contact2');

NEVER delete with a GET route unless you want models deleted by search engine crawlers

1 like
ErikRobles's avatar

@Snapey Is there a better way to handle it as they are contact messages that we no longer needed.

Please or to participate in this conversation.