How to send welcome emails after user registration in Laravel?

VNC Digital Services
4 min readSep 26, 2019

In a recent project, we had a requirement to send a welcome email after user registration. So I started exploring on the internet and I stumbled upon sendgrid mail client.

If you don’t have a Sendgrid account you can get it from here

Laravel provides a clean API over the popular SwiftMailer library with drivers for SMTP, PHP’s mail, send email and more. For this example, we’ll be sending an email with SendGrid using the SMTP Driver. For more information, check out the docs for Laravel’s Mail interface.

Before you begin you’ve to make some modifications in your .env file.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.sendgrid.net
MAIL_PORT=587
MAIL_USERNAME=sendgrid_username
MAIL_PASSWORD=sendgrid_password
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="John Smith"
MAIL_FROM_ADDRESS=from@example.com

You can modify these depends on your requirements.

After modifying your .env file don’t forget to run following command as it’ll clear cache and your new configuration will apply to your application.

php artisan config:cache

Let’s create a Mailable class

php artisan make:mail WelcomeMail

(WelcomeMail is Mailable class name)
Above command will create a new file under app/Mail/WelcomeEmail.php and it should look something like this

In Laravel, each mail sent by your application has a mailable class. These classes are stored in app/Mail directory. Don’t worry if you don’t see this folder. After typing following command, a mail folder will automatically create in the app directory.

<phpnamespace App\Mail;use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class WelcomeMail extends Mailable
{
use Queueable, SerializesModels;
public $data;public function __construct($data)

$this->data = $data;
public function build()
{
$address = 'janeexampexample@example.com';
$subject = 'This is a demo!';
$name = 'Jane Doe';

return $this->view('emails.test')
->from($address, $name)
->cc($address, $name)
->bcc($address, $name)
->replyTo($address, $name)
->subject($subject)
->with([ 'message' => $this->data['message'] ]);

}
}

Creating email template

Since each email typically uses a Blade template to render its contents, you have the full power and convenience of the Blade templating engine when building your email’s HTML.

So, let’s create an email template under app/resources/views/emails/welcome.blade.php

<DOCTYPE html>
<html lang=”en-US”>
<head>
<meta charset=”utf-8">
</head>
<body>
<h2>Test Email</h2>
<p>{{ $message }}</p>
</body>
</html>

Sending an email

In my case I’ve used a UserController to store user details and also we are going to write the code for Mailable class.

<php namespace App\Http\Controllers;use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use App\Mail\WelcomeMail;
use App\User;
class UserController extends Controller
{

public function store(Request $request){

$request->validate([
‘name’ => [‘required’, ‘string’, ‘max:191’],
‘username’ => [‘required’, ‘unique:users’,’max:191'],
‘phone’=> [‘required’],
‘email’ => [‘required’, ‘string’, ‘email’, ‘max:191’,
‘unique:users’],
‘password’ => [‘required’, ‘string’, ‘min:6’,’max:191',
‘confirmed’],
]);
$userreg = new User([
‘name’ => $request->get(‘name’),
‘username’=> $request->get(‘username’),
‘phone’=> $request->get(‘email’),
‘password’ => Hash::make($request[‘password’]),
]);
$email = $request->get(‘email’);$data = ([
‘name’ => $request->get(‘name’),
‘email’ => $request->get(‘email’),
‘username’ => $request->get(‘username’),
‘phone’ => $request->get(‘phone’),
]);
Mail::to($email)->send(new WelcomeMail($data));

$userreg->save();

flash(‘User has been added!’,’success’)->important();
return back();}


}

Modifying our Mailable class

Now we have to modify our mailable classes to get the data from the controller. In my case, I’ve created $data array and we are passing the name, email, username, and phone (Here you can pass your desired values).

<phpnamespace App\Mail;use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\User;
class WelcomeMail extends Mailable
{
use Queueable, SerializesModels;
public $data;
/**
* Create a new message instance.

* @return void
*/
public function __construct($data)

$this->data = $data;
/**
* Build the message.

* @return $this
*/
public function build()
{
$subject = 'Welcome';
return $this->view('emails.welcome')->subject($subject);

}
}

Modifying our email template class

Now let’s get all the data into our view which is our welcome.blade.php file. Here I’m writing a normal HTML email template but you can get beautiful templates online. (If you want to know which website can create beautiful email templates just drop us a comment and will get back to you).

<DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
</head>
<body>
<h2> Hi {{$data['name']}}, we’re glad you’re here! Following are your account details: <br>
</h3>
<h3>Email: </h3><p>{{$data['email']}}</p>
<h3>Username: </h3><p>{{$data['username']}}</p>
<h3>Phone: </h3><p>{{$data['phone']}}</p></body>
</html>

In the above code I have called {{$data[‘name’]}} to get the name from Mailable class and I have repeated the same process with username and phone as well.

Conclusion

Sending an email with Laravel becomes easy if you have a mail service like Sendgrid. In fact, you can use Gmail to send email but then there are some steps you have to follow to achieve that. Anyway, hope you’ve learned something meaningful today. If you’ve any issue or query while setting this up just drop us a comment and will be right with you.

(This story is originally posted on our blog)

Sharing is caring!

--

--

VNC Digital Services

Our main goal is finding smart ways of using technology that will help build a better tomorrow for everyone, everywhere. Visit us at www.vncdigitalservices.com