Send Welcome Email Notification After User Register in Laravel 8
Tags: laravel, webdev, tutorial, programming
Published: 2021-10-22 17:24:37

**Laravel Welcome Email Notification** - in this article, I will share about how to send a welcome notification to the user after registration in Laravel 8, and in this article I will send the notification via email (Gmail).

##Step 1: Install Laravel

```
//via Laravel Installer
composer global require laravel/installer
laravel new laravel-welcome-email-notification

//via Composer
composer create-project laravel/laravel laravel-welcome-email-notification
```

In this first step, we need to install the [latest version of laravel](https://codelapan.com/tag/laravel) (currently version 8) which we will try to implement sending a welcome email notification after the user registers in laravel 8. To install laravel you can use laravel installer or use composer like the example above .

Please choose one method you want to use for laravel installation. From the two examples of laravel installation commands above, they will both generate a laravel project with the name laravel-welcome-email-notification.

##Step 2: Setup Database

```
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_welcome_email_notification
DB_USERNAME=root
DB_PASSWORD=
```

Next, create a new database to store sample data that we will use in this experiment. If you are using xampp as local development, please create a new database at localhost/phpmyadmin. Here I give an example, I created a new database with the name Laravel_welcome_email_notification. Then don't forget to also adjust the DB_DATABASE in the .env file as in the example above.

##Step 3: Setup Sender

```
MAIL_MAILER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=587
MAIL_USERNAME=mail@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=mail@gmail.com
MAIL_FROM_NAME="Codelapan"
```

Then in the third step, which is to setup the email sender. Because in this experiment, we will send email with gmail in [laravel 8](https://codelapan.com/tag/laravel8), so the .env file also needs to be adjusted like the example above. Before using gmail as an email sender in laravel, make sure "Less secure app access" in your google settings is in ON condition.

##Step 4: Install Laravel UI

```
composer require laravel/ui
php artisan ui bootstrap --auth
npm install && npm run dev
php artisan migrate
```

We need authentication features (register, login and logout) to implement sending a welcome email notification to the user after register. For that, here I give an example using the laravel ui package to create the authentication feature.

Please install the laravel ui package in our laravel project by running the commands as above sequentially.

*Before installing the laravel ui package, make sure you have entered the project directory by running the cd laravel-welcome-email-notification command in the terminal.

##Step 5: Generate Notification

```
php artisan make:notification WelcomeEmailNotification
```

Then, generate notification class using artisan command as above. After running the command, we now have a WelcomeEmailNotification.php file located in the app/Notifications directory.

##Step 6: Setup RegisterController

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

    $user->notify(new WelcomeEmailNotification());
        
    return $user;
}
```
Next, open the file in app/Http/Controllers/Auth/RegisterController.php. In the function create, change or add the code as above. With this code, after the user registers, it will send an email from the WelcomeEmailNotification class to the user's email.

##Step 7: Testing 1

![Laravel Email Notification](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z2sqnilynluoak3cc47m.PNG)
Okay, now let's do the first test. Please run your laravel project using the **php artisan serve** command, then try to register with an active email. After successfully register, an email will be sent to your email as shown above.

##Step 8: Custom Email Message
How do I change the text of the notification email message and display the user name? so easy, please see the explanation below.👇
**RegisterController.php**
```php
protected function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    $user->notify(new WelcomeEmailNotification($user));
        
    return $user;
}
```
First, open the RegisterController.php file and add the $user variable in the WelcomeEmailNotification for us to parse the data to email.

**WelcomeEmailNotification.php**
```php
use App\Models\User;
....
....
....
public function __construct(User $user)
{
    $this->user = $user;
}
.....
.....
.....
public function toMail($notifiable)
{
    return (new MailMessage)
                ->greeting('Hello, '.$this->user->name)
                ->line('Welcome to Codelapan.')
                ->action('Explore', url('/'))
                ->line('Thank you for using our application!');
}
```
Then open the WelcomeEmailNotification.php file, then change the code to be as above. With this code, we add a $user variable that is parsed from the RegisterController.php file to be able to display the user data that has been inputted during the register. Then, we also change the text on the first line and the text for the button.

##Testing 2
![Laravel Email Notification](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjruwbsscov42oph7do4.PNG)

Okay, now let's try again register a new user and of course also using an active email so that we can receive and see the results of the email notification. After successfully registering, there will be an email notification coming in again as shown above.

```
php artisan vendor:publish --tag=laravel-notifications
```
Don't like the format or appearance? We can customize the email notification template by publishing the notification package resource using the command above. After running the command as above, the email notification template is located in the resources/views/vendor/notifications directory.

##Step 10: Add Attachment
```php
public function toMail($notifiable)
{
    return (new MailMessage)
                ->greeting('Hello, '.$this->user->name)
                ->line('Welcome to Codelapan.')
                ->action('Explore', url('/'))
                ->line('Thank you for using our application!')
                ->attach('promotion.PNG');
}
```
In some cases, we may also need to add an attachment file in the welcome email notification, such as adding a file for proof of registration, promotional banners and others. Well, to add attachments in email notifications, we can use or add the attach method. An example of how to use it, can be seen as the code example above.

Alright, enough of this post, which has discussed a little about how to create or send a welcome email notification to users who have successfully registered in the Laravel 8 application. Good luck and see you in the next article. 👋 🚀

Don't forget to visit [Codelapan.com](https://codelapan.com)