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

tako's avatar
Level 1

Can't send a mail from user email with api

I'm trying to make an API route to send a mail but I have this error : Connection could not be established with host :stream_socket_client().

mail.php

'smtp' => [
            'transport' => 'smtp',
            'host' => env('MAIL_HOST'),
            'port' => env('MAIL_PORT', 587),
            'encryption' => env('MAIL_ENCRYPTION', 'tls'),
            'username' => env('MAIL_USERNAME'),
            'password' => env('MAIL_PASSWORD'),
            'timeout' => null,
            'auth_mode' => env('MAIL_AUTH_MODE'),
        ],

.env

MAIL_MAILER=smtp
[email protected]
MAIL_PORT=2525
MAIL_USERNAME=me
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME="${APP_NAME}"

controller

    public function sendEmail()
    {
        $user = User::where('email', request()->input('email'))->first();
        $token = Password::getRepository()->create($user);
        $user->sendPasswordResetNotification($token);
    }

route

    Route::post('mail', [AuthentificationController::class, 'sendEmail']);

In postman I pass the mail adress in the body.

I don't know if I have to change some security settings somewhere.

0 likes
1 reply
a4ashraf's avatar
a4ashraf
Best Answer
Level 33

@tako

check your .env file setting and make sure it should be correct

and you need to create your cache

php artisan config:cache

or

php artisan optimize:clear

Please or to participate in this conversation.