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

PrinceDM's avatar

Laravel Breeze API - wrong host on verify-email url

Hello, can someone please help me on this one?

  • Laravel Version: 10.3.3
  • PHP Version: 8.1.11
  • Database Driver & Version: 10.8.3-MariaDB Homebrew

Description:

Get fresh installation of Laravel Breeze & Next.js / API, send verify-email the host in url is not FRONTEND_URL as expected.

Steps To Reproduce:

  1. Install latest Laravel Breeze API with Next.js as a front end.
  2. Create db and migrate.
  3. Make sure User model implements MustVerifyEmail contract.
  4. In .env file set MAIL_MAILER=log.
  5. Register a new user.
  6. Take a look at the verify email url inside the logs file, notice the host is the server and not the FRONTEND_URL as expected.
0 likes
1 reply
LaryAI's avatar
Level 58

It seems like the issue is with the url method in the VerifyEmail notification class. By default, it uses the url method from the Illuminate\Routing\UrlGenerator class, which generates URLs based on the current request. Since the request is coming from the server, it's using the server's hostname instead of the FRONTEND_URL.

To fix this, you can override the url method in the VerifyEmail notification class to use the FRONTEND_URL instead of the current request. Here's an example:

use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class VerifyEmail extends Notification
{
    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        $verificationUrl = $this->verificationUrl($notifiable);

        return (new MailMessage)
            ->subject('Verify Email Address')
            ->line('Please click the button below to verify your email address.')
            ->action('Verify Email Address', $verificationUrl)
            ->line('If you did not create an account, no further action is required.');
    }

    protected function verificationUrl($notifiable)
    {
        return str_replace(
            '{{id}}',
            $notifiable->getKey(),
            str_replace(
                '{{hash}}',
                $notifiable->getEmailForVerification(),
                config('app.frontend_url') . '/email/verify/{{id}}/{{hash}}'
            )
        );
    }

    public function url($route, $parameters = [], $absolute = true)
    {
        return config('app.frontend_url') . '/' . ltrim($route, '/');
    }
}

In this example, we're overriding the url method to always return the FRONTEND_URL instead of the current request. We're also using the config helper function to get the FRONTEND_URL from the .env file.

Make sure to update the config/app.php file to include the FRONTEND_URL:

'frontend_url' => env('FRONTEND_URL', 'http://localhost:3000'),

And update the .env file to include the FRONTEND_URL:

FRONTEND_URL=http://localhost:3000

With these changes, the verification URL should now use the FRONTEND_URL instead of the server's hostname.

Please or to participate in this conversation.