在生产中使用 localhost 的 Laravel 电子邮件验证链接

问题描述 投票:0回答:1

我已经处理一个问题很长时间了,但还没有找到解决方案。 我在生产中使用 Laravel 9,当用户注册时,会自动发送验证电子邮件,但到达后,会看到以下地址:

http://localhost/verify-email/3322/45a667c298ca4ffd2d8f3ea002d9a11c4391ff63?expires=1710354098&signature=1ffba796dc8f5368a064f735fce8888f11b9cfd1ca630444283ab11039a8bc00

自动发送(第一次)是用LOCALHOST发送的,但是当用户手动点击重发按钮时,发送正确。

我没有使用或配置队列。我该如何解决这个问题?

我已经尝试过:

php artisan optimize:clear

这是我的 .env 配置。

APP_NAME=somefun
APP_ENV=production
APP_KEY=base64:YUiSMZfKLyWPixk3guHIeYS259kX3FD6Yn82osjIIHI=
APP_DEBUG=false
APP_URL=http://somefun.com.mx

我找不到发布验证电子邮件文件(

Illuminate/Auth/Notifications/VerifyEmail.php
)以更改链接生成的方法。因为供应商发布上没有。

请帮忙。

php laravel email localhost email-verification
1个回答
0
投票

第一次发送通知时,它不会从

.env
获取通知,而是从传入的请求中解释它以解决它,我必须执行以下操作:

  1. 创建新通知:

    php artisan make:notification VerifyEmail

  2. 使其扩展原来的

    vendor\laravel\framework\src\Illuminate\Auth\Notifications\VerifyEmail.php

  3. 用其副本替换父函数,但包括修复的新行:

它保持这样:

app\Notifications\VerifyEmail.php

<?php

namespace App\Notifications;

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Config;
use Illuminate\Auth\Notifications\VerifyEmail as VerifyEmailNotification;

class VerifyEmail extends VerifyEmailNotification
{

    /**
     * Get the verification URL for the given notifiable.
     *
     * @param  mixed  $notifiable
     * @return string
     */
    protected function verificationUrl($notifiable)
    {
        if (static::$createUrlCallback) {
            return call_user_func(static::$createUrlCallback, $notifiable);
        }

        URL::forceRootUrl(config('app.url')); //<-------THIS FIXED

        return URL::temporarySignedRoute(
            'verification.verify',
            Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
            [
                'id' => $notifiable->getKey(),
                'hash' => sha1($notifiable->getEmailForVerification()),
            ]
        );
    }
}

  1. app\Models\User.php
    中,通过向其传递新通知的新实例来替换特征函数:
<?php

namespace App\Models;

use Exception;
use Stripe\Account;
use Stripe\StripeClient;
use Laravel\Cashier\Billable;
use Laravel\Sanctum\HasApiTokens;
use App\Notifications\VerifyEmail; //<-------Reference to new notification
use Spatie\Permission\Traits\HasRoles;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasApiTokens,  Notifiable, HasRoles, Billable;

    protected $guarded = [];

    //>>>>Replace the trait function by passing it a new instance of the new notification<<<<<<<
    public function sendEmailVerificationNotification()
    {
        $this->notify(new VerifyEmail);
    }
}

强制 url,我不知道这是否是一个好主意,因为我读到它可以做一些奇怪的事情,但目前我没有遇到任何不适当的行为。

© www.soinside.com 2019 - 2024. All rights reserved.