Mailable in Laravel to send email on signup for confirmation
you can use Laravel’s built-in Mail
class to send a confirmation email when a user signs up.
Here are the steps you can follow:
- Set up your mail settings in the
.env
file. You’ll need to provide your email service provider’s SMTP credentials:
MAIL_DRIVER=smtp
MAIL_HOST=your-smtp-host
MAIL_PORT=587
MAIL_USERNAME=your-email-username
MAIL_PASSWORD=your-email-password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your-email-address
MAIL_FROM_NAME=”${APP_NAME}”
2. Create a new Mailable class using the php artisan make:mail command:
php artisan make:mail SignupConfirmation
3. Open the newly created SignupConfirmation class in the app/Mail directory and add the necessary code:
user = $user;
}public function build()
{
return $this->view(’emails.signup_confirmation’)
->subject(‘Confirm your email address’);
}
}
In the above code, we are passing the user object to the constructor, which we can then use to populate the email content. We are also defining the email’s subject and the view that will be used to render the email.
4 Create a view for the email. In this example, we’ll create a file named signup_confirmation.blade.php in the resources/views/emails directory:
Welcome to our site, {{ $user->name }}!
Please click the link below to confirm your email address: