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

Lokedi's avatar

Problem sending email api

Hello, I try to use this for sending an email with api but I got this error

Error: Class 'App\Http\Controllers\ContactEmail' not found in file

My controller

public function contact(Request $request)
    {
        $data = $this->validate($request, [
            'name'       => 'required',
            'email'      => 'required|email',
            'phone'      => 'required|digits:10',
            'message'    => 'required',
        ]);

        Mail::to( config('mail.from.address') )
            ->send(new ContactEmail($request->only(['name', 'email', 'phone', 'message'])) );

        return response()->json(['sent' => true], 200);
    }

Mail Class

public $contact;

/**
 * Create a new message instance.
 *
 */
public function __construct( array $contact )
{
    $this->contact = $contact;
}

/**
 * Build the message.
 *
 * @return $this
 */
public function build()
{
    return $this->markdown('mail.contact');
}

Route::post('/contact', [AjaxController::class, 'contact']);

0 likes
1 reply
simkbaio's avatar

you call ContactEmail with missing right namespace. so, PHP is autoload ContactEmail from the current Class namespace.

Please or to participate in this conversation.