慕容的工作随笔

慕容的工作随笔

记录学习和工作

【原创】Laravel 邮件发送

增加激活相关的用户表字段

php artisan make:migration add_activation_to_users_table --table=users

database/migrations/[timestamp]_add_activation_to_users_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('activation_token')->nullable();
$table->boolean('activated')->default(false);
});
}

public function down()
{
    Schema::table(&#039;users&#039;, function (Blueprint $table) {
        $table-&gt;dropColumn(&#039;activation_token&#039;);
        $table-&gt;dropColumn(&#039;activated&#039;);
    });
}

};

php artisan migrate

生成令牌

令牌是要在用户被创建之前生成,在模型中使用 creating 方法来监听模型被创建之前要进行的动作,而如果要在模型创建之后进行设置,则使用 created 方法。

app/Models/User.php

<?php

namespace App\Models;

use Illuminate\Support\Str;

class User extends Authenticatable
{
protected $hidden = [
'password', 'remember_token',
];

public static function boot()
{
    parent::boot();

    static::creating(function ($user) {
        $user-&gt;activation_token = Str::random(10);
    });
}

}

boot 方法会在用户模型类完成初始化之后进行加载。

更新模型工厂,将生成用户的激活状态设置为 true:

<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class UserFactory extends Factory
{
public function definition()
{
return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => now(),
'activated' => true,
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}

public function unverified()
{
    return $this-&gt;state(function (array $attributes) {
        return [
            &#039;email_verified_at&#039; =&gt; null,
        ];
    });
}

}

php artisan migrate:refresh --seed

发送邮件

在 Laravel 中,可以通过 Mail 接口中的 send 方法来发送邮件,代码如下:

$view = 'emails.confirm';
$data = compact('user');
$from = 'star@example.com';
$name = 'Star';
$to = $user->email;
$subject = "感谢注册 Weibo 应用!请确认你的邮箱。";

Mail::send($view, $data, function ($message) use ($from, $name, $to, $subject) {
$message->from($from, $name)->to($to)->subject($subject);
});

Mail 的 send 方法接收三个参数:

  • 第一个是视图名称
  • 第二个是要传给视图的数组
  • 第三个是接收邮件消息实例的闭包回调,可以在该回调中定义消息的发送者、接收者、邮件主题等信息

激活功能

<?php

namespace App\Http\Controllers;.

class UsersController extends Controller
{
public function __construct()
{

    $this-&gt;middleware(&#039;auth&#039;, [
        &#039;except&#039; =&gt; [&#039;show&#039;, &#039;create&#039;, &#039;store&#039;, &#039;index&#039;, &#039;confirmEmail&#039;]
    ]);
}

public function confirmEmail($token)
{
    $user = User::where(&#039;activation_token&#039;, $token)-&gt;firstOrFail();

    $user-&gt;activated = true;
    $user-&gt;activation_token = null;
    $user-&gt;save();

    Auth::login($user);
    session()-&gt;flash(&#039;success&#039;, &#039;恭喜你,激活成功!&#039;);
    return redirect()-&gt;route(&#039;users.show&#039;, [$user]);
}

}

评论 (0)

还没有评论,来做第一个吧。

发表评论

提交后需管理员审核通过后显示