用户在 Laravel 中注册时发送电子邮件

时间:2021-04-20 10:47:02

标签: laravel

当用户注册完成后,我想向用户发送一封电子邮件,上面写着“帐户创建成功”。我使用tailwindcss ui。您可以在下面看到控制器的图片。我想我应该在 RegisterController 中编写代码。因此,当有人注册时,系统会自动向新用户发送邮件。

Controllers

注册控制器

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Mail\WelcomeMail;
use Illuminate\Support\Facades\Mail;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\Models\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }

    
}

我应该在这里做什么?

2 个答案:

答案 0 :(得分:0)

您可以使用 Mail 门面和 ::send 方法发送电子邮件。下面是一个例子:

$to = 'receiver_name';
$email = 'receiver_address';
$mail_data = array(
   //here you can pass data to the email blade view
);
Mail::send('viewname', $mail_data, function ($message) use ($to, $email) {
            $message->to($email, $to)->subject('subject text');
            $message->from('Sender name');
        });

答案 1 :(得分:0)

您可以使用 notification 发送邮件。当他/她注册到系统时通知用户。在您的 RegisterController

use App\Notifications\WelcomeEmail;

protected function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    $user->notify(new WelcomeEmail($user));
    return $user;
}

现在使用 artisan 命令制作通知类

php artisan make:notification WelcomeEmail

通知类的内容是

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class WelcomeEmail extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($user)
    {
        $this->user = $user;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['mail'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
        ->view(
            'welcome_email', ['user' => $this->user]
        )
        ->from('support@yourcompany.com', 'Your Company Name')
        ->subject('Welcome Aboard');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

最后在views文件夹中制作一个blade文件welcome_email.blade.php

<!DOCTYPE html>
<html>

<head>
    <title></title>
</head
<body>
    <h1>Hello {{ $user->name }}, Account created successfully</h1>
</body>
</html>

如果不想使用通知,可以使用 Mail 门面发送邮件。

use Illuminate\Support\Facades\Mail;
protected function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    Mail::send('welcome_email', compact('user'), function ($message) use ($user) {
        $message->to($user->email, $user->name)->subject('Welcome Aboard');
        $message->from('support@yourcompany.com', 'Your Company Name');
    });
    return $user;
}
<块引用>

如果您在 env 文件中设置了 MAIL_FROM_ADDRESS,您可以跳过邮件发送功能中的 from 部分。