Send Custom Welcome Email on Registration in Laravel

Published on July 16, 2019 54 sec read

In this tutorial, we are going to learn how to send a custom email on user registration in Laravel.

Table of Contents

  1. Create an Email Template
  2. Set SMTP Credentials
  3. Update Registration Controller
  4. Let’s Test

Step 1 : Create an Email Template

Go to resources>views folder and create a blade file called welcome_email.blade.php. Then open the file and paste this demo template:

welcome_email.blade.php
Hello {{ $name }},<br><br>

Welcome to MyNotePaper.<br><br>

Thank You,<br>
MyNotepaper

Step 2 : Set SMTP Credentials

Open .env file and set your SMTP details. I using mailtrap for testing purpose.

.env
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=48664********
MAIL_PASSWORD=6cfa6********

Step 3 : Update Registration Controller

Navigate to app>Http>Controllers>Controller and open RegisterController.php. We are going to modify create() method to send a custom email on registration. Modify the controller like this:

RegisterController.php
use Illuminate\Support\Facades\Mail;

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

    // email data
    $email_data = array(
        'name' => $data['name'],
        'email' => $data['email'],
    );

    // send email with the template
    Mail::send('welcome_email', $email_data, function ($message) use ($email_data) {
        $message->to($email_data['email'], $email_data['name'])
            ->subject('Welcome to MyNotePaper')
            ->from('[email protected]', 'MyNotePaper');
    });

    return $user;
}

Step 4 : Lets’s Test

We have completed all the necessary tasks. Now run the project and test by signing up. I’ve received an email on registration:

We are done. Thanks for reading. 🙂

Author

Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development. If you enjoy my content, please consider supporting what I do!

Follow Buy me a coffeeBuy me a coffee

2 Replies to “Send Custom Welcome Email on Registration in Laravel”

Leave a Reply

Your email address will not be published. Required fields are marked *