laravel +mail.php 自带发送邮件配置

配置获取邮件服务smtp

https://jingyan.baidu.com/article/425e69e61e9178be15fc168a.html

.env新增配置

MAIL_DRIVER=smtp
MAIL_HOST=smtp.qq.com
MAIL_PORT=465
MAIL_USERNAME=***@qq.com
MAIL_PASSWORD=***(来源在https://jingyan.baidu.com/article/425e69e61e9178be15fc168a.html 配置完成后生成的秘钥放到这)
MAIL_FROM_NAME=自定义
MAIL_ENCRYPTION=ssl

发送邮件代码

        Mail::raw("email send content descript", function ($message) use ($to_email,$subject,$from)
        {
            $message->from(env("MAIL_USERNAME"), "来自哪描述");
            $message->to(132@163.com(邮箱地址));
            $message->subject("邮件主体描述");
        });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,你需要创建一个用户表来存储用户的信息,包括用户名、密码和邮箱等。可以使用Laravel自带的迁移工具来创建表。在命令行中输入`php artisan make:migration create_users_table --create=users`,然后打开创建的迁移文件,添加需要的字段。 ``` Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); ``` 接下来,需要创建一个用户模型,并将其关联到用户表。在命令行中输入`php artisan make:model User`,然后打开创建的用户模型文件,在类中添加以下代码: ``` class User extends Authenticatable { use Notifiable; protected $fillable = [ 'name', 'email', 'password', ]; protected $hidden = [ 'password', 'remember_token', ]; public function getEmailForPasswordReset() { return $this->email; } } ``` 然后,你需要创建一个控制器来处理注册和登录逻辑。在命令行中输入`php artisan make:controller AuthController`,然后打开创建的控制器文件,在类中添加以下代码: ``` class AuthController extends Controller { public function showRegistrationForm() { return view('auth.register'); } public function register(Request $request) { $this->validate($request, [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:6|confirmed', ]); $user = User::create([ 'name' => $request->name, 'email' => $request->email, 'password' => Hash::make($request->password), ]); Auth::login($user); return redirect('/home'); } public function showLoginForm() { return view('auth.login'); } public function login(Request $request) { $this->validate($request, [ 'email' => 'required|string|email', 'password' => 'required|string', ]); if (Auth::attempt(['email' => $request->email, 'password' => $request->password])) { return redirect()->intended('/home'); } return back()->withErrors(['email' => 'Email or password is incorrect.']); } public function logout() { Auth::logout(); return redirect('/login'); } } ``` 在这个控制器中,我们定义了四个方法:`showRegistrationForm()`用于显示注册表单,`register()`用于处理注册逻辑,`showLoginForm()`用于显示登录表单,`login()`用于处理登录逻辑,`logout()`用于退出登录。 最后,你需要创建两个视图文件,一个用于显示注册表单,一个用于显示登录表单。在`resources/views/auth`目录下分别创建`register.blade.php`和`login.blade.php`视图文件。在这两个文件中,你可以使用Laravel自带的表单生成器来创建表单。 注册表单: ``` <form method="POST" action="{{ route('register') }}"> @csrf <div> <label for="name">Name</label> <div> <input id="name" type="text" name="name" value="{{ old('name') }}" required autofocus> </div> @if ($errors->has('name')) <span> <strong>{{ $errors->first('name') }}</strong> </span> @endif </div> <div> <label for="email">Email</label> <div> <input id="email" type="email" name="email" value="{{ old('email') }}" required> </div> @if ($errors->has('email')) <span> <strong>{{ $errors->first('email') }}</strong> </span> @endif </div> <div> <label for="password">Password</label> <div> <input id="password" type="password" name="password" required> </div> @if ($errors->has('password')) <span> <strong>{{ $errors->first('password') }}</strong> </span> @endif </div> <div> <label for="password-confirm">Confirm Password</label> <div> <input id="password-confirm" type="password" name="password_confirmation" required> </div> </div> <div> <div> <button type="submit"> Register </button> </div> </div> </form> ``` 登录表单: ``` <form method="POST" action="{{ route('login') }}"> @csrf <div> <label for="email">Email</label> <div> <input id="email" type="email" name="email" value="{{ old('email') }}" required autofocus> </div> @if ($errors->has('email')) <span> <strong>{{ $errors->first('email') }}</strong> </span> @endif </div> <div> <label for="password">Password</label> <div> <input id="password" type="password" name="password" required> </div> @if ($errors->has('password')) <span> <strong>{{ $errors->first('password') }}</strong> </span> @endif </div> <div> <div> <button type="submit"> Login </button> </div> </div> </form> ``` 这样,你就可以在Laravel中实现用户Email注册和登录了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值