Mails in Laravel
In my recent blogs, I have discussed about Laravel authentication and sending mails to the newly registered user is a must have functionality for every application. So today I will be telling about how to send mail from laravel.
I will be telling about smtp driver but there are many drivers available in the market which can be used to send mails but most basic one is smtp. So let’s start !!!
If you haven’t read my previous blog post about laravel authentication, I would recommend you to have a look at it first then continue with this one.
Sending mails in Laravel is like adding a cherry to the cake. It’s so simple to send a mail and if you follow all the steps you will be able to send your first mail from your application.
Configurations
First of all you need to configure mail account credentials in .env file or you can even do that in config/mail.php file but I will recommend you to do that in the .env file
View File
Create a view file(html) that you want to send to the user that registers in your application. Create a folder of mails in your resources/views directory and inside that create a welcome.blade.php file
For simplicity, I have just added the Welcome text along with mobile number of user.
<p>Welcome :{{ $user->mobile }}</p>
Logic
Now, you will be wondering how we can get the user mobile number to send in the mail. For that you need to create a mail php file that will contain the actual logic of mail and the data to be passed in the template. Just run
php artisan make:mail WelcomeMail
A file will be created in App\Mail named WelcomeMail.php
We need to pass the user details to the blade file so just create a public variable named user in this file like this :
public $user;
To set this variable, we will be using it’s constructor like so :
public function __construct($user){
$this->user = $user;
}
One important thing :
You can pass the variable to the blade file in two ways one by creating public variable and the public variable will be accessible directly in the view file and the other way is to create a protected or private variable and sending the user variable using the with method . I will show you both approaches.
If you use the above method i.e creating a public variable then you don’t need to do anything just return the view file from the build method like this:
public function build(){ return $this->view('mails.welcome');}
If you don’t want to make variable public, you can pass it with the with method like :
return $this->with(['user'=>$this->user])->view('mails.welcome');
This time the user variable is not an object, it’s an array so use $user[‘mobile‘] in blade file.
Along with view , you can also specify with which email account you want to send the mail by chaining from method in return statement like this:
return $this->from('mail-id@gmail.com')->view('mails.welcome');
If you don’t specify from method, laravel will automatically take the mail which you have specified in your .env file (MAIL_FROM_ADDRESS variable)
Sending Mail
You can send a mail from any controller within your application using the Mail facade like this:
Mail::to($user->email) ->send(new WelcomeMail($user));
If you want to send it while a user registers to your application, you need to change the create method of App\Http\Controllers\Auth\RegisterController.php like this:
protected function create(array $data){ $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => Hash::make($data['password']), 'mobile'=>$data['mobile'], ]); Mail::to($user->email) ->send(new WelcomeMail($user)); return $user;}
That’s it…An email will be sent to the user’s email address who signs up.
I hop you liked the blog and if you , then please hit that clap icon and follow me for more such basic laravel blogs.
NOTE: All the code is pushed in this repo under branch “mail_to_regstered_users”